简体   繁体   English

使用ContentPresenter和ItemsPresenter进行自定义控件

[英]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并添加两个依赖项属性MainContentUserInteractions 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.

相关问题 带有 ControlTemplate 和 ContentPresenter 的自定义 DataGrid 控件 - Custom DataGrid control with ControlTemplate and ContentPresenter 在自定义控件中交换ContentPresenter内容 - Swapping ContentPresenter content in custom control 将 ContentPresenter 用于自定义控件 (Thumb) - Using a ContentPresenter for a custom control (Thumb) ContentPresenter不以样式显示自定义控件 - ContentPresenter not displaying Custom Control in Style NullReferenceException,带有自定义UserControl中ContentPresenter中的引用控件 - NullReferenceException with referenced Control in ContentPresenter from custom UserControl C# MVVM 使用 ContentPresenter 传递 ObservableCollection 并在自定义控件中绑定 - C# MVVM Pass through ObservableCollection with ContentPresenter and Bind in Custom Control 如何使用ContentPresenter在自定义控件中显示BitmapImage? - How can I use a ContentPresenter to display a BitmapImage in a custom control? 模板控制中的ContentPresenter无法正常工作 - ContentPresenter in Templated Control not working 如何在自定义ItemsControl的ItemsPresenter中设置项目样式? - How to style items in the ItemsPresenter of a custom ItemsControl? 在ContentPresenter子控件中设置属性 - Set property in ContentPresenter child control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM