【问题标题】:WPF ItemsControl derived custom control helpWPF ItemsControl 派生的自定义控件帮助
【发布时间】:2011-05-03 16:58:26
【问题描述】:


我想写一个 ItemsControl 派生的自定义控件。这部分是出于需要,部分是作为学习练习 - 请不要建议 I Style、DataTemplate、ControlTemplate 和 ListBox 等......即请不要质疑需求 - 只需假设它是真实的。
我浏览了网络,发现了很多有用的 ItemControl 信息,但没有明确的例子。 当我在 VS 中创建新的自定义控件时,我得到了几乎空的代码和带有<Style> 块的 Generic.xaml,它可以通过<Setter Property="Template"> 等设置 ControlTemplates、DataTemplates、Presenter 等。但是最小的 xaml/code 是多少需要在这里获得一个控件,该控件将绑定到一个 ObservableCollection 到 ItemsSoruce 以显示为一个列表? 换句话说:ItemsControl 派生的自定义控件的规范形式是什么?
我需要 ItemsPresenter 吗?是否必须在 ControlTemplate 中指定堆栈面板?我必须在<Setter Property="ItemTemplate"> 上设置 TargetType 吗?等等
勺子喂食更喜欢例如说:它很容易,我只需要在项目控制容器的向量空间上集成 DataTemplate 相对于面板演示者 yada yada ......不是一个很大的帮助。 更多信息:该控件仅面向显示,即没有选定项目等的概念。 提前致谢!

默认 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>

【问题讨论】:

    标签: wpf custom-controls itemscontrol


    【解决方案1】:

    看看default styles(点击Default WPF Themes链接):

    例如列表框:

    <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>
    

    【讨论】:

    • 感谢 HB - 这对添加代码有效并删除它的一些部分直到它失败有很大帮助,这让我找到了基本部分(请参阅我自己的答案)。
    【解决方案2】:

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

    &lt;ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;

    【讨论】:

      猜你喜欢
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多