【发布时间】:2020-04-02 10:08:37
【问题描述】:
我想在 WPF 中构建一个自定义控件,但我不知道该使用什么。我想实现以下目标:
所以有两个不同的文本块(标题和提示)以及图标的路径。
我需要在一个视图中多次使用此控件,但 itemssource 不是列表(因此我无法获取 itemscontrol 和数据模板)。
我将如何做到最好?
【问题讨论】:
我想在 WPF 中构建一个自定义控件,但我不知道该使用什么。我想实现以下目标:
所以有两个不同的文本块(标题和提示)以及图标的路径。
我需要在一个视图中多次使用此控件,但 itemssource 不是列表(因此我无法获取 itemscontrol 和数据模板)。
我将如何做到最好?
【问题讨论】:
public string Header {
get => (string) GetValue(HeaderProperty);
set => SetValue(HeaderProperty, value);}
public static readonly DependencyProperty HeaderProperty = // note naming - <PropName>+Property
DependencyProperty.Register(nameof(Header), typeof(string), typeof(YourControl), new FrameworkPropertyMetadata(""));
对您要公开的每个属性重复此代码
这样您就可以使用<YourControl Header="text" /> 或<YourControl Header={Binding SomeProp} /> 在XAML 中绑定属性
<TextBlock Text={Binding Header} />
【讨论】: