在用户控件中绑定控件的属性

时间:2011-03-06 15:10:03

标签: c# wpf xaml data-binding user-controls

出于这个问题的目的,我定义了一个非常简单的用户控件:

<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";

一切正常,因此我认为问题与绑定有关。)

3 个答案:

答案 0 :(得分:0)

您没有属性,您有my_text的私有成员,WPF不会绑定它。

试试这个:

private string myText = "this is a text";
public string MyText
{
    get
    {
        return myText;
    }

    set
    {
        myText = value;
    }
}

如果你想改变文本并自动显示更改,你应该在setter中实现INotifyPropertyChanged

答案 1 :(得分:0)

你是否正确实施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";  
    }
 }

然而,出现的唯一文字是“这是一个文本”,而不是“另一个文本”。

现在怎么了?

答案 2 :(得分:0)

您尚未将TextBox.Text属性连接到用户控件中的依赖项属性。 DependencyProperty文本中的更改将由WPF处理,并且实际上不会通过控件的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";  
}