在用户控件中绑定控件的属性
为了这个问题的目的,我定义了一个非常简单的用户控件:
<UserControl x:Class="simpleUserControl.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300"
Width="300">
<Grid>
<TextBox Name="TextBox1"/>
</Grid>
</UserControl>
我希望(用户控件的)用户能够设置“TextBox1”的“Text”属性,所以我定义了一个属性(将其命名为“ text')获取和设置 TextBox1.Text:
namespace simpleUserControl
{
public partial class UserControl1 : UserControl
{
public string text
{
get { return TextBox1.Text; }
set { TextBox1.Text = value; }
}
public static readonly DependencyProperty textProperty = DependencyProperty.Register("text", typeof(string), typeof(UserControl1));
public UserControl1()
{
InitializeComponent();
}
}
}
现在,当使用用户控件时,我想将此“text”属性绑定到某个字符串对象:
<Window x:Class="WpfApplication33.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:simple_user_control="clr-namespace:simpleUserControl;assembly=simpleUserControl"
Title="Window1"
Height="300"
Width="300"
Loaded="Window_Loaded">
<Grid Name="MainGrid">
<simple_user_control:UserControl1 Name="MyUserControl">
<simple_user_control:UserControl1.text>
<Binding Path="my_text"/>
</simple_user_control:UserControl1.text>
</simple_user_control:UserControl1>
</Grid>
</Window>
以及后面的代码:
namespace WpfApplication33
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
string my_text = "this is a text";
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MainGrid.DataContext = this;
}
}
}
但这由于某种原因不起作用......我不明白为什么,因为我已经设置了 DataContext 并引用了用户控件...我做错了什么? (值得一提的是,当像这样直接设置“文本”属性时:
MyUserControl.text = "Another text";
一切正常,因此我认为问题与绑定有关)。
For the purpose of this question, I have defined a very simple user control:
<UserControl x:Class="simpleUserControl.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300"
Width="300">
<Grid>
<TextBox Name="TextBox1"/>
</Grid>
</UserControl>
I want the user (of the user control) to be able to set the 'Text' property of 'TextBox1', so I defined a property (named it 'text') that gets and sets TextBox1.Text:
namespace simpleUserControl
{
public partial class UserControl1 : UserControl
{
public string text
{
get { return TextBox1.Text; }
set { TextBox1.Text = value; }
}
public static readonly DependencyProperty textProperty = DependencyProperty.Register("text", typeof(string), typeof(UserControl1));
public UserControl1()
{
InitializeComponent();
}
}
}
Now, when using the user control , I want to bind this 'text' property to some string object:
<Window x:Class="WpfApplication33.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:simple_user_control="clr-namespace:simpleUserControl;assembly=simpleUserControl"
Title="Window1"
Height="300"
Width="300"
Loaded="Window_Loaded">
<Grid Name="MainGrid">
<simple_user_control:UserControl1 Name="MyUserControl">
<simple_user_control:UserControl1.text>
<Binding Path="my_text"/>
</simple_user_control:UserControl1.text>
</simple_user_control:UserControl1>
</Grid>
</Window>
and the code behind:
namespace WpfApplication33
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
string my_text = "this is a text";
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MainGrid.DataContext = this;
}
}
}
But that doesn't work for some reason... I don't understand why, because I have set the DataContext and put a reference to the user control... What am I doning wrong?
(It is worth mentioning that when setting 'text' property directly like this:
MyUserControl.text = "Another text";
everything works fine, and therefore I think that that the problem has something to do with the binding).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有属性,您有一个
my_text
的私有成员,WPF 不会绑定到它。试试这个:
如果您想更改文本并自动显示更改,您可能应该在 setter 中实现 INotifyPropertyChanged 。
You don't have a property, you have a private member for
my_text
and WPF won't bind to it.Try this:
And you should probably implement
INotifyPropertyChanged
in the setter, if you want to alter the text and display the changes automatically.您关于实现 INotifyPropertyChanged 的说法是正确的,但仍然缺少一些东西。
主窗口的代码现在如下所示:
然而,出现的唯一文本是“This is a text”而不是“Another text”。
现在怎么了?
You were right about implementing
INotifyPropertyChanged
, but There is still something missing.The code of the main window now looks like this:
And yet, the only text that appears is "This is a text" and not "Another text".
What's wrong now?
您尚未将
TextBox.Text
属性连接到用户控件中的依赖项属性。DependencyProperty
文本中的更改将由 WPF 处理,实际上不会通过控件的文本属性。您应该按如下方式定义控件,以将TextBox.Text
属性绑定到您的依赖项属性:在代码隐藏中:
然后在 MainWindow 中您应该能够执行此操作并且它应该可以工作:
You haven't connected up the
TextBox.Text
property to your dependency property in the user control. Changes in theDependencyProperty
text will be handled by WPF and won't actually go through your control's text property. You should define your control as follows to bind theTextBox.Text
property to your dependency property:In the code-behind:
Then in your MainWindow you should be able to do this and it should work: