[英]Custom DataGrid control with ControlTemplate and ContentPresenter
[英]Custom control with both ContentPresenter and ItemsPresenter
我建立了一个自定义控件,该控件具有两个内容区域,如下所示:
+-------------+
| L | |
| o | Main |
| g | Content |
| o | |
+---+---------+
| Interaction |
+-------------+
我是通过继承Control
并添加两个依赖项属性MainContent
和UserInteractions
。 使用控件时,我会执行以下操作:
<controls:ScreenControl>
<controls:ScreenControl.MainContent>
<TextBlock>Some content goes here</TextBlock>
</controls:ScreenControl.MainContent>
<controls:ScreenControl.UserInteractions>
<Button>Do something</Button>
</controls:ScreenControl.UserInteractions>
</controls:InstallerScreenControl>
相应的控件模板看起来略有简化,如下所示:
<Style TargetType="{x:Type controls:ScreenControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:ScreenControl}">
<DockPanel Width="530">
<DockPanel DockPanel.Dock="Top">
<!-- Some static content -->
<ContentPresenter Content="{Binding MainContent, RelativeSource={...}}"
DataContext="{Binding DataContext, RelativeSource={...}}"/>
</DockPanel>
<DockPanel DockPanel.Dock="Bottom" Background="LightGray" Height="50">
<StackPanel DockPanel.Dock="Right" Orientation="Horizontal" HorizontalAlignment="Right">
<ContentPresenter Content="{Binding UserInteractions, RelativeSource={...}}"
DataContext="{Binding DataContext, RelativeSource={...}}" />
</StackPanel>
</DockPanel>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在,我想在UserInteractions
区域中允许几个元素。 但是, ContentPresenter
仅允许单个元素。 通过阅读,似乎我需要使用ItemsPresenter
,但这仅在我继承自ItemsControl
吗?
我尝试将UserInteractions
依赖项属性的类型设置为ObservableCollection<UIElement>
(我发现的大多数文章都将其设置为object
,这就是我所拥有的),并且这种方法在没有编译错误的情况下可以正常工作。子级,但在渲染时仅显示(Collection)
,而不显示实际按钮。
我想念什么?
假设UserInteractions
是UI元素的集合,则应立即使用:
<ItemsControl ItemsSource="{Binding UserInteractions, RelativeSource={...}}"/>
如果UserInteraction对象是纯数据对象,则还必须定义ItemTemplate
:
<ItemsControl ItemsSource="{Binding UserInteractions, RelativeSource={...}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.