WPF ItemsControl 派生自定义控件帮助

发布于 2024-11-04 11:25:08 字数 1347 浏览 0 评论 0原文


我想编写一个 ItemsControl 派生自定义控件。这部分是出于需要,部分是作为学习练习 - 请不要建议我使用 Style、DataTemplate、ControlTemplate、ListBox 等...即,请不要质疑这种需要 - 只要假设它是真实的。
我在网上搜索并发现了很多有用的 ItemControl 信息,但没有明确的示例。 当我在 VS 中创建新的自定义控件时,我得到了几乎空的代码和带有

默认 Generic.xaml(此处最少要添加什么?):

<Style TargetType="{x:Type local:MyItemsControlDerivedClass}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyItemsControlDerivedClass}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">        

                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I would like to write a ItemsControl derived custom control. This is partially from need and partially as a learning exercise - please don't suggest I Style, DataTemplate, ControlTemplate a ListBox etc... I.e. please don't question the need - just assume its genuine.
I've trolled the web and found lots of useful ItemControl info but no clear cut examples.
When I create I new Custom Control in VS I get practically empty code behind and Generic.xaml with a <Style> block where its possible to set ControlTemplates, DataTemplates, Presenters etc via <Setter Property="Template"> etc. But what is the minimum xaml/code needed here to get a control that will bind to an ObservableCollection to ItemsSoruce to display as a list? Put another way: whats the canoical form of a ItemsControl derived custom control?
Do I need an ItemsPresenter? Do I have to specify a stack pannel in the ControlTemplate? Do I have to set TargetType on the <Setter Property="ItemTemplate">? etc.
Spoon feeding prefered eg saying: its easy and I just need to intergrate the DataTemplate over the vector space of item control containers with respect to the panel presenter yada yada... aint a great help.
Further info: The control is a display only orientated ie there is no concept of selected item etc.
Thanks in advance!

Default Generic.xaml (whats minimum to add here?):

<Style TargetType="{x:Type local:MyItemsControlDerivedClass}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyItemsControlDerivedClass}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">        

                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油饼 2024-11-11 11:25:08

只需看一下默认样式(遵循默认WPF 主题 链接):

例如 ListBox:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ListBox}">
            <Border Name="Bd"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    SnapsToDevicePixels="true"
                    Padding="1">
                <ScrollViewer Padding="{TemplateBinding Padding}"
                              Focusable="false">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </ScrollViewer>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled"
                         Value="false">
                    <Setter TargetName="Bd"
                            Property="Background"
                            Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                </Trigger>
                <Trigger Property="IsGrouping"
                         Value="true">
                    <Setter Property="ScrollViewer.CanContentScroll"
                            Value="false"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

Just take a look at the default styles (follow the Default WPF Themes link):

e.g. ListBox:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ListBox}">
            <Border Name="Bd"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    SnapsToDevicePixels="true"
                    Padding="1">
                <ScrollViewer Padding="{TemplateBinding Padding}"
                              Focusable="false">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </ScrollViewer>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled"
                         Value="false">
                    <Setter TargetName="Bd"
                            Property="Background"
                            Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                </Trigger>
                <Trigger Property="IsGrouping"
                         Value="true">
                    <Setter Property="ScrollViewer.CanContentScroll"
                            Value="false"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>
忘年祭陌 2024-11-11 11:25:08

按照 HB 建议添加默认样式即可工作并使控件可用(项目显示)。删除 ItemsPresenter(ScrollViewer 内)会破坏控件(无内容显示)。这篇文章解释了发生了什么:
http://drwpf.com/blog/2009/ 05/12/itemscontrol-l-is-for-lookless/
本质上,ControlTemplate 必须具有:
a) ItemPresenter 或
b) IsItemsHost 属性设置为 true 的面板。
也就是说,您需要添加到麦粒肿的最低限度是一个带有以下任一内容的 ControlTemplate:


Adding the default style as H.B suggests works and renders the control usable (items display). Removing the ItemsPresenter (inside the ScrollViewer) breaks the control (no content display). This post explains whats going on:
http://drwpf.com/blog/2009/05/12/itemscontrol-l-is-for-lookless/
Essentially the ControlTemplate must have either:
a) an ItemPresenter OR
b) a panel with IsItemsHost property set true.
I.e the bare minimum you need to add to the stye is a ControlTemplate with either:
<StackPanel IsItemsHost="True" />
OR
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文