gpt4 book ai didi

WPF ItemsControl 派生的自定义控件帮助

转载 作者:行者123 更新时间:2023-12-04 06:27:24 50 4
gpt4 key购买 nike

我想写一个 ItemsControl 派生的自定义控件。这部分是出于需要,部分是作为学习练习 - 请不要建议 I Style、DataTemplate、ControlTemplate 和 ListBox 等......即请不要质疑这种需要——只要假设它是真实的。
我浏览了网络,发现了很多有用的 ItemControl 信息,但没有明确的例子。
当我在 VS 中创建新的自定义控件时,我得到了实际上是空的代码和带有 <Style> 的 Generic.xaml可以通过 <Setter Property="Template"> 设置 ControlTemplates、DataTemplates、Presenter 等的 block 等等。但是这里需要的最小 xaml/代码是多少才能获得一个控件,该控件将绑定(bind)到一个 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>

最佳答案

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

关于WPF ItemsControl 派生的自定义控件帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5872968/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com