简体   繁体   English

更改属性时,C#XAML数据绑定无效

[英]C# XAML Databinding has no effect when property is changed

Okay I've been wracking my brain a lot about this one, I'm missing something, I just can't figure out what. 好吧,我一直在为这个问题大肆渲染我的脑子,我错过了一些东西,我无法弄清楚是什么。 Ultimately I'm trying to set databinding so I can update values to be shown on the fly, but for the life of me, it's not working. 最终我正在尝试设置数据绑定,以便我可以更新值以便在运行中显示,但对于我的生活,它不起作用。

The XAML is: XAML是:

<TextBox x:Name="textBox" HorizontalAlignment="Left" 
    Height="37" Margin="85,38,0,0" TextWrapping="Wrap" 
    Text="{Binding Path=TBBind}" VerticalAlignment="Top" 
    Width="121" />

Note that I have the {Binding Path=TBBind} set. 请注意,我设置了{Binding Path=TBBind}

The code behind is: 背后的代码是:

using System.ComponentModel;
using System.Windows;

namespace Databinding_Practice_2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            TBBind = "test";
        }

        private string _tBBind;

        public string TBBind
        {
            get { return _tBBind; }
            set
            {
                if (value != _tBBind)
                {
                    _tBBind = value;
                    OnPropertyChanged("TBBind");
                }
            }

        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            MessageBox.Show("OnPropertyChanged triggered");
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}

Help me obi-w.... oh wait, help me anyone! 帮帮我......哦等等,帮帮我吧!

Assuming that you are trying to use the MVVM pattern (which stands for Model-View-ViewModel): 假设您正在尝试使用MVVM模式(代表Model-View-ViewModel):

Your MainWindow is the View. 您的MainWindow是视图。

You should create another class to be the View Model, like this: 您应该创建另一个类作为视图模型,如下所示:

public class MainWindowViewModel : INotifyPropertyChanged
{
    public MainWindowViewModel()
    {
        TBBind = "test";
    }

    private string _tBBind;

    public string TBBind
    {
        get { return _tBBind; }
        set
        {
            if (value != _tBBind)
            {
                _tBBind = value;
                OnPropertyChanged("TBBind");
            }
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string property)
    {
        MessageBox.Show("OnPropertyChanged triggered");
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

Your MainWindow code behind will become like this after removing all ViewModel related stuff to the MainWindowViewModel class: 在将所有ViewModel相关内容移除到MainWindowViewModel类之后,您的MainWindow代码将变为这样:

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }

}

Now, you should link the View with the ViewModel, there are many ways to do this. 现在,您应该将View与ViewModel链接起来,有很多方法可以做到这一点。 Here is one of them: 这是其中之一:

In the XAML of MainWindow, have the following inside the Window element: 在MainWindow的XAML中,在Window元素中包含以下内容:

<Window.DataContext>
    <wpfApplication5:MainWindowViewModel />
</Window.DataContext>

<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="37" Margin="85,38,0,0" TextWrapping="Wrap" Text="{Binding TBBind}" VerticalAlignment="Top" Width="121" />
</Grid>

Please note that WpfApplication5 is the name of the namespace in my WPF project. 请注意, WpfApplication5是我的WPF项目中命名空间的名称。 This will probably be different in your case. 在您的情况下,这可能会有所不同。

Try: 尝试:

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
    TBBind = "test";
}

The difference here sets the critical DataContext property. 这里的区别设置了关键的DataContext属性。 This is the cornerstone of the MVVM pattern, which you are implementing here. 这是MVVM模式的基石,您将在此处实现。 You should consider separating the View Model responsibility into another class, and then setting the View's DataContext to an instance of that class, but the approach you have taken here works for simple cases. 您应该考虑将View Model责任分离到另一个类,然后将View的DataContext设置为该类的实例,但您在此处采用的方法适用于简单的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM