WPF - How to set Grid's children in a style?
How can I do something like t开发者_如何学Pythonhis?
<Style TargetType="Grid">
<Setter Property="Children">
<Setter.Value>
...
</Setter.Value>
</Setter>
</Style>
I know Children in read-only and this gives me "Writable property expected" on Children.
Thanks
You can't because Panel.Children
is not a DependencyProperty
. You almost certainly want to use an ItemsControl
with a customized ItemsPanel
. However, without more information I couldn't really say for sure.
You can use the ContentControl instead of Grid, and set its Template/ContentTemplate property with ControlTemplate/DataTemplate containing your Grid
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
...
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ContentControl.Style>
</ContentControl>
or
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
...
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ContentControl.Style>
</ContentControl>
精彩评论