[英]Custom control with both ContentPresenter and ItemsPresenter
I have built a custom control that has two regions for content, looking like this: 我建立了一个自定义控件,该控件具有两个内容区域,如下所示:
+-------------+
| L | |
| o | Main |
| g | Content |
| o | |
+---+---------+
| Interaction |
+-------------+
I did this by inheriting from Control
and adding two dependency properties MainContent
and UserInteractions
. 我是通过继承
Control
并添加两个依赖项属性MainContent
和UserInteractions
。 When using the control, I do something like this: 使用控件时,我会执行以下操作:
<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>
The corresponding control template looks, slightly simplified, like this: 相应的控件模板看起来略有简化,如下所示:
<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>
Now, I want to allow several elements in the UserInteractions
region. 现在,我想在
UserInteractions
区域中允许几个元素。 However, ContentPresenter
only allows a single element. 但是,
ContentPresenter
仅允许单个元素。 From reading, it seems I need to use ItemsPresenter
, but that this only works if I inherit from ItemsControl
? 通过阅读,似乎我需要使用
ItemsPresenter
,但这仅在我继承自ItemsControl
吗?
I tried setting the type of the UserInteractions
dependency property to ObservableCollection<UIElement>
(most articles I found just set it to object
, so that was what I had), and this works in-so-far that there is no compilation error with several children, but on rendering it only displays (Collection)
, not the actual buttons. 我尝试将
UserInteractions
依赖项属性的类型设置为ObservableCollection<UIElement>
(我发现的大多数文章都将其设置为object
,这就是我所拥有的),并且这种方法在没有编译错误的情况下可以正常工作。子级,但在渲染时仅显示(Collection)
,而不显示实际按钮。
What am I missing? 我想念什么?
Provided that UserInteractions
is a collection of UI elements, this should work out of the box: 假设
UserInteractions
是UI元素的集合,则应立即使用:
<ItemsControl ItemsSource="{Binding UserInteractions, RelativeSource={...}}"/>
If the UserInteraction objects are pure data object, you would also have to define the ItemTemplate
: 如果UserInteraction对象是纯数据对象,则还必须定义
ItemTemplate
:
<ItemsControl ItemsSource="{Binding UserInteractions, RelativeSource={...}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.