gpt4 book ai didi

c# - 将自定义控件内的 TextBlock 绑定(bind)到同一自定义控件的依赖属性

转载 作者:太空狗 更新时间:2023-10-30 00:02:22 25 4
gpt4 key购买 nike

我有一个自定义控件,里面有一个 TextBlock:

<Style TargetType="{x:Type local:CustControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustControl}">
<Border Background="Blue"
Height="26"
Width="26" Margin="1">

<TextBlock x:Name="PART_CustNo"
FontSize="10"
Text="{Binding Source=CustControl,Path=CustNo}"
Background="PaleGreen"
Height="24"
Width="24"
Foreground="Black">
</TextBlock>

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

并且这个自定义控件有一个依赖属性:

    public class CustControl : Control
{
static CustControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustControl), new FrameworkPropertyMetadata(typeof(CustControl)));
}

public readonly static DependencyProperty CustNoProperty = DependencyProperty.Register("CustNo", typeof(string), typeof(CustControl), new PropertyMetadata(""));

public string CustNo
{
get { return (string)GetValue(CustNoProperty); }
set { SetValue(CustNoProperty, value); }
}

}

我希望在自定义控件的每个实例中将“CustNo”属性的值传输到 TextBlock 的“Text”属性中。但是我的:

Text="{Binding Source=CustControl,Path=CustNo}"

不工作。

不适用于 Path=CustNoProperty:

Text="{Binding Source=CustControl,Path=CustNoProperty}"

最佳答案

你需要一个TemplateBinding,比如

<TextBlock
Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CustNo}" />

关于c# - 将自定义控件内的 TextBlock 绑定(bind)到同一自定义控件的依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1854952/

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