wpf 自定义控件依赖属性绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11896619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom Control Dependency Property Binding
提问by Jason Butera
I am going insane trying to get this to work with even the most basic example. I cannot for the life of me get binding to work. Here is a super easy example that is not working for me. I MUST be doing something incorrect.
试图让它与最基本的例子一起工作,我会发疯。我不能终生受工作约束。这是一个对我不起作用的超级简单示例。我一定是做错了什么。
My Custom Control in my control library assembly:
我的控件库程序集中的自定义控件:
public class TestControl : Control
{
public static readonly DependencyProperty TestPropProperty =
DependencyProperty.Register("TestProp", typeof(string), typeof(TestControl), new UIPropertyMetadata(null));
public string TestProp
{
get { return (string)GetValue(TestPropProperty); }
set { SetValue(TestPropProperty, value); }
}
static TestControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(typeof(TestControl)));
}
}
And its XAML template:
及其 XAML 模板:
<Style TargetType="{x:Type local:TestControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TestControl}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel>
<TextBlock Text="Testing..." />
<Label Content="{Binding TestProp}" Padding="10" />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Here's the XAML consuming the control in a wpf window with a reference to my control library:
这是在 wpf 窗口中使用控件的 XAML,并引用了我的控件库:
<Grid>
<ItemsControl Name="mylist">
<ItemsControl.ItemTemplate>
<DataTemplate>
<my:TestControl TestProp="{Binding Path=Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
And here's the code behind:
这是背后的代码:
public partial class Test2 : Window
{
public class TestObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
private int _id;
public int id
{
get { return _id; }
set { _id = value; OnPropertyChanged("id"); }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; OnPropertyChanged("Name"); }
}
}
public Test2()
{
InitializeComponent();
mylist.ItemsSource = new TestObject[]
{
new TestObject(){ id = 1, Name = "Tedd" },
new TestObject(){ id = 2, Name = "Fred" },
new TestObject(){ id = 3, Name = "Jim" },
new TestObject(){ id = 4, Name = "Hyman" },
};
}
}
Running this example gives me four instances of the control, however I only see the "Testing..." TextBlock on each. My label is never bound. What am I misunderstanding and doing incorrectly?
运行此示例为我提供了四个控件实例,但是我只能在每个实例上看到“Testing...”TextBlock。我的标签从不绑定。我误解了什么和做错了什么?
采纳答案by Clemens
You haven't set the proper binding source. You would either have to set RelativeSource:
您尚未设置正确的绑定源。您要么必须设置RelativeSource:
<Label Content="{Binding TestProp, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
Or use TemplateBinding:
或使用TemplateBinding:
<Label Content="{TemplateBinding TestProp}"/>

