
<Grid x:Name="LayoutRoot" Background="White">
<Button >
<Button.Content >
<StackPanel>
<TextBlock Text="ほにゃらら" />
<Rectangle Width="50" Height="50" Fill="Red" />
</StackPanel>
</Button.Content>
</Button>
</Grid>
こんなボタンを作って、プロパティ一覧からContentの「値をリソースに抽出」すると自動で↓みたいな感じになる。
<UserControl.Resources>
<StackPanel x:Key="Content1">
<TextBlock Text="ほにゃらら" />
<Rectangle Fill="Red" Height="50" Width="50" />
</StackPanel>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="{StaticResource Content1}"></Button>
</Grid>
この状態でデザイナは正常に表示されるんだけど、実行時に例外が発生する。
XamlParseException
なんかボタンのスタイルを眺めてたら、ContentTemplateにリソースを適用すればいいみたい。
ContentTemplateにはDataTemplateを指定する。
<UserControl.Resources>
<DataTemplate x:Key="Content1">
<StackPanel>
<TextBlock Text="ほにゃらら" />
<Rectangle Fill="Red" Height="50" Width="50" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Button ContentTemplate="{StaticResource Content1}"></Button>
</Grid>
これでContentとStyleを別々に指定できるので、Contentだけの使い回しができるようになった。
0 件のコメント:
コメントを投稿