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

发布于 2024-10-21 02:02:00 字数 2396 浏览 1 评论 0原文

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

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜中书 2024-10-28 02:02:01

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

试试这个:

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

    set
    {
        myText = value;
    }
}

如果您想更改文本并自动显示更改,您可能应该在 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:

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

    set
    {
        myText = value;
    }
}

And you should probably implement INotifyPropertyChanged in the setter, if you want to alter the text and display the changes automatically.

泛泛之交 2024-10-28 02:02:01

您关于实现 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";  
    }
 }

然而,出现的唯一文本是“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:

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

And yet, the only text that appears is "This is a text" and not "Another text".

What's wrong now?

野稚 2024-10-28 02:02:01

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

You haven't connected up the TextBox.Text property to your dependency property in the user control. Changes in the DependencyProperty 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 the TextBox.Text property to your dependency property:

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

In the code-behind:

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();
    }
}
}

Then in your MainWindow you should be able to do this and it should work:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
    MainGrid.DataContext = this;
    my_text = "This is a text";
    my_text = "Another text";  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文