WPF - How to set Grid's children in a style?

拟墨画扇 提交于 2019-12-25 03:37:29

问题


How can I do something like this?

<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


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/3533586/wpf-how-to-set-grids-children-in-a-style

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!