如何使用WPF中的用户控件实现数据绑定? [英] How to achieve databinding with a user control in WPF?
问题描述
我缺少什么?
XAML
<! - 用户控制 - >
< TextBox Text ={Binding Path = TheText}/>
<! - Window - >
< WpfApplication1:SomeControl TheText ={Binding Path = MyStringProp}/>
C#
//用户控制----
public partial class SomeControl:UserControl
{
public DependencyProperty TheTextProperty = DependencyProperty
。注册(TheText,typeof(string),typeof(SomeControl));
public string TheText
{
get
{
return(string)GetValue(TheTextProperty);
}
set
{
SetValue(TheTextProperty,value);
}
}
public SomeControl()
{
InitializeComponent();
DataContext = this;
}
}
//窗口----
public partial class Window1:Window
{
private readonly MyClass _myClass;
public Window1()
{
InitializeComponent();
_myClass = new MyClass();
_myClass.MyStringProp =Hallo Welt;
DataContext = _myClass;
}
}
public class MyClass //:DependencyObject
{
// public static DependencyProperty MyStringPropProperty = DependencyProperty
// .Register (MyStringProp,typeof(string),typeof(MyClass));
public string MyStringProp {get;组; }
// {
// get {return(string)GetValue(MyStringPropProperty); }
// set {SetValue(MyStringPropProperty,value); }
//}
}
最好的问候
Oliver Hanappi
PS:我试图在我的用户控件上实现INotifyPropertyChanged接口,但没有帮助。
您要将TextBox的 Text
属性绑定回 TheText
它所在的UserControl的属性,对吧?所以你需要告诉绑定在哪里的财产生活。有几种方法可以执行此操作(可以使用FindAncestor使用RelativeSource进行操作),但最简单的方法是将UserControl设置为XAML中的名称,并使用元素绑定进行绑定:
< UserControl ...
x:Name =me/>
< TextBox Text ={Binding TheText,ElementName = me}/>
< / UserControl>
现在您的TextBox将反映您分配(或绑定)到您的SomeControl.TheText 属性 - 您不需要更改任何其他代码,尽管您可能希望在底层MyClass对象上实现INotifyPropertyChanged,以便绑定知道属性何时更改。
I'm fairly new to WPF and I have some problems getting databinding to work as I want. I've written a user control which contains a TextBox whose Text-Property I want to bind to a property of my UserControl, which I want to bind again to something else.
What am I missing?
XAML
<!-- User Control -->
<TextBox Text="{Binding Path=TheText}" />
<!-- Window -->
<WpfApplication1:SomeControl TheText="{Binding Path=MyStringProp}" />
C#
// User Control ----
public partial class SomeControl : UserControl
{
public DependencyProperty TheTextProperty = DependencyProperty
.Register("TheText", typeof (string), typeof (SomeControl));
public string TheText
{
get
{
return (string)GetValue(TheTextProperty);
}
set
{
SetValue(TheTextProperty, value);
}
}
public SomeControl()
{
InitializeComponent();
DataContext = this;
}
}
// Window ----
public partial class Window1 : Window
{
private readonly MyClass _myClass;
public Window1()
{
InitializeComponent();
_myClass = new MyClass();
_myClass.MyStringProp = "Hallo Welt";
DataContext = _myClass;
}
}
public class MyClass// : DependencyObject
{
// public static DependencyProperty MyStringPropProperty = DependencyProperty
// .Register("MyStringProp", typeof (string), typeof (MyClass));
public string MyStringProp { get; set; }
// {
// get { return (string)GetValue(MyStringPropProperty); }
// set { SetValue(MyStringPropProperty, value); }
// }
}
Best Regards
Oliver Hanappi
PS: I've tried to implement the INotifyPropertyChanged interface on my user control, but it did not help.
You want to bind the Text
property of your TextBox back to the TheText
property of the UserControl it lives in, right? So you need to tell the binding where the property lives. There's a couple of ways to do this (you can do it with a RelativeSource using FindAncestor) but the easiest way is to give the UserControl a "name" in the XAML and bind using element binding:
<UserControl ...
x:Name="me" />
<TextBox Text="{Binding TheText,ElementName=me}" />
</UserControl>
Now your TextBox will reflect the value you've assigned (or bound) to your "SomeControl.TheText" property - you needn't change any of your other code, although you'll probably want to implement INotifyPropertyChanged on your underlying MyClass object so that the binding knows when the property has changed.
这篇关于如何使用WPF中的用户控件实现数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!