【问题标题】:Databinding not working for user control - PropertyChanged always null数据绑定不适用于用户控件 - PropertyChanged 始终为空
【发布时间】:2015-03-06 21:59:06
【问题描述】:

我遇到了数据绑定问题。我的测试应用程序如下所示:

有一个主窗口:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:WpfApplication2"
        x:Name="main"
        Title="MainWindow" >
    <StackPanel >
        <Button Content="Button" Click="Button_Click"/>
        <Controls:UserControl1 />
    </StackPanel>
</Window>

还有一个用户控件:

    <UserControl x:Class="WpfApplication2.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 x:Name="uc"
                 >
        <Grid >
            <TextBox Width="40" Text="{Binding ElementName=main, 
Path=Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </Grid>
    </UserControl>

我想点击主窗口上的按钮来更新用户控件中文本框的值:

MainWindow文件代码:

namespace WpfApplication2
{

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private string _status;
        public string Status
        {
            get { return _status; }
            set
            {
                if (value != _status)
                {
                    _status = value;
                    RaisePropertyChanged("Status");
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Status == "one")
                Status = "two";
            else
                Status = "one";
        }
    }
}

以及UserControl的代码:

namespace WpfApplication2
{
        public partial class UserControl1 : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, e: new PropertyChangedEventArgs(propertyName));
        }

        public UserControl1()
        {
            InitializeComponent();
        }
    }
}

我不明白为什么它不起作用,但 PropertyChanged 始终为空。这个例子是我能想象到的最简单的形式......

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    据我所知,您正在尝试使用 ElementName 绑定访问父窗口,这是不可能的。但是,您可以使用相对源绑定来获取父窗口:

    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Status}" ... />
    

    后续编辑:

    您的子用户控件应如下所示:

    <UserControl 
             ...
             x:Name="usr">
       <Grid>
          <TextBlock Text="{Binding Message, ElementName=usr}"/>
       </Grid>
    </UserControl>
    

    然后您需要创建一个名为“Message”的依赖项属性(这只是一个示例,我不确定您要如何调用此属性)。

    public partial class YourUserControl: UserControl
    {
        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(YourUserControl), new PropertyMetadata(""));
    
        public UserControl1()
        {
            InitializeComponent();
        }
    }
    

    然后,当您在父用户控件中声明它时,只需将 Message 属性的绑定设置为您需要在父用户控件中绑定的任何属性:

    <YourNamespace:YourUserControl Message="{Binding PropertyName, ElementName=elementName}" />
    

    【讨论】:

    • 谢谢 - 这有助于测试应用程序 - 在我的实际项目中,我引用了一个用户控件 - 我尝试使用 AncestorType=UserControl,但它没有帮助。我应该在那里做什么?
    • 没问题,你是说在用户控件中有用户控件吗?你需要参考父母吗?
    • 是的@Mike,基本上是为了布局整洁,我有一个窗口,其中包含一个大的用户控件(类似于标签页)。此标签页包含一个网络浏览器和一个用户控件 - 状态条。在浏览器的导航事件中,我想在 statusStrip 上显示一条消息。因此,MainWindow 包含一个父用户控件,该控件包含一个子用户控件。我想用父用户控件上定义的方法更新子用户控件上的文本框。
    • 好的,在这种情况下,您将需要不同的方法。解决方案是在子用户控件上创建依赖属性,然后在父用户控件中声明用户控件时,只需使用 ElementName 绑定将依赖属性绑定到父用户控件上的属性。呸,这有意义吗?要我举个例子吗?
    • 嗯,是的,如果你能做到,我将不胜感激!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2014-06-15
    • 2015-08-26
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多