Dynamic content of a Grid

て烟熏妆下的殇ゞ 提交于 2019-12-12 03:36:16

问题


My data model has a property of the enumeration type. I wonder if there is way to place dynamically a user control based on the value of the enumeration type?

I am currently investigating in the following direction:

<Grid Name ="AdjustmentsArea" DockPanel.Dock ="Right" MinWidth ="100" Visibility ="Collapsed" >
    <ContentControl DataContext ="{Binding AjustmentView}">
        <Style TargetType ="model:AjustmentViews">
            <Style.Triggers>
                <DataTrigger Binding ="{Binding}"  Value ="Settings">
                    /// is it possible in principle to point a user control using a Setter ??? 
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl>
</Grid>

May be also I am on a wrong path. But I would like to know(learn) if it is possible to implement this requiremet over dynamic create an instance of a user control, but not using hide/show exised element approach. What would you recommend?


回答1:


you can set different template depending on trigger binding value

<ContentControl DataContext ="{Binding AjustmentView}">
<ContentControl.Style>
<Style TargetType ="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value ="Settings">
   <Setter Property="Template">
      <Setter.Value>
          <ControlTemplate> <!--template with UserControl here--> </ControlTemplate>
      </Setter.Value>
   </Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>

e.g. WPF Slider uses this approach when Orientation changes (Horizontal or Vertical)



来源:https://stackoverflow.com/questions/36469504/dynamic-content-of-a-grid

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