[英]Binding a property of a control within a 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>
我希望用戶(用戶控件的用戶)能夠設置'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並引用了用戶控件...我在做什么錯? (值得一提的是,當直接像這樣設置'text'屬性時:
MyUserControl.text = "Another text";
一切正常,因此我認為問題與綁定有關。
您沒有屬性,擁有my_text
的私有成員,WPF不會綁定到該屬性。
嘗試這個:
private string myText = "this is a text";
public string MyText
{
get
{
return myText;
}
set
{
myText = value;
}
}
如果您要更改文本並自動顯示更改,則可能應該在setter中實現INotifyPropertyChanged
。
您實現INotifyPropertyChanged
是正確的,但仍然缺少一些東西。
現在,主窗口的代碼如下所示:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private string _my_text;
string my_text
{
get { return _my_text; }
set
{
_my_text = value;
OnPropertyChanged("my_text");
}
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
MainGrid.DataContext = this;
MyUserControl.text = "This is a text";
my_text = "Another text";
}
}
但是,出現的唯一文本是“這是一個文本”,而不是“另一個文本”。
現在怎么了
您尚未將TextBox.Text
屬性連接到用戶控件中的依賴項屬性。 WPF將處理DependencyProperty
文本中的更改,而實際上不會通過控件的text屬性。 您應該按照以下方式定義控件,以將TextBox.Text
屬性綁定到依賴項屬性:
<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" x:Name="control">
<Grid>
<TextBox Name="TextBox1" Text="{Binding text, ElementName=control}"/>
</Grid>
</UserControl>
在后面的代碼中:
namespace simpleUserControl
{
public partial class UserControl1 : UserControl
{
public string text
{
get { return (string)GetValue(textProperty); }
set { SetValue(textProperty, value); }
}
public static readonly DependencyProperty textProperty = DependencyProperty.Register("text", typeof(string), typeof(UserControl1));
public UserControl1()
{
InitializeComponent();
}
}
}
然后在您的MainWindow中,您應該可以執行此操作,並且應該可以正常工作:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
MainGrid.DataContext = this;
my_text = "This is a text";
my_text = "Another text";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.