【发布时间】:2010-12-23 17:17:43
【问题描述】:
我有一个自定义控件,里面有一个 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="{Binding Source=CustControl,Path=CustNo}"
不工作。
不适用于 Path=CustNoProperty:
Text="{Binding Source=CustControl,Path=CustNoProperty}"
【问题讨论】:
标签: c# wpf data-binding xaml custom-controls