简体   繁体   English

WPF PropertyChanged 事件未触发/更新文本框

[英]WPF PropertyChanged event not firing/updating textbox

I'm still relatively new to Data Binding in wpf, but despite plunging through all the articles and posts here and elsewhere about what could be wrong, I still have not found a solution.我对 wpf 中的数据绑定还比较陌生,但尽管仔细阅读了此处和其他地方的所有文章和帖子,了解可能出现的问题,但我仍然没有找到解决方案。 The code below is the prevalent information parsed out of my larger files.下面的代码是从我的大文件中解析出来的普遍信息。

I have made sure everything else is working, including adding a property to retrieve the protected parameter options to ensure options.FullPath is in fact getting set/changed on the Browse button's Click event.我已确保其他一切正常,包括添加一个属性来检索受保护的参数options以确保options.FullPath实际上是在浏览按钮的Click事件上设置/更改的。 I attempted to subscribe to the PropertyChanged event in the main window with the line test.PropertyChanged += ShowMessage;我试图通过行test.PropertyChanged += ShowMessage;订阅主 window 中的PropertyChanged事件; , ShowMessage being a method that triggers a MessageBox with text in it. , ShowMessage是一种触发带有文本的MessageBox的方法。 I tried multiple variations on the OnPropertyChanged method from hardcoding it within the calling method to what is displayed here.我尝试了OnPropertyChanged方法的多种变体,从在调用方法中对其进行硬编码到此处显示的内容。 I even tried setting options to a default value of "" just in case it was being weird about that.我什至尝试将options设置为默认值"" ,以防万一这很奇怪。 No luck on anything, and I have no way to acquire C#6 at the moment, so it may very well be that what I have works with the right language updates, but I just can't tell since it doesn't trigger.没有任何运气,我目前没有办法获得 C#6,所以很可能是我所拥有的与正确的语言更新一起工作,但我无法判断,因为它不会触发。

Any help or insight would be greatly appreciated!任何帮助或见解将不胜感激!

EDIT: All of the below code is house within the same namespace.编辑:以下所有代码都位于同一命名空间中。

Object Class: Object Class:

public class EEOptionSet: INotifyPropertyChanged
{
    public EEOptionSet()
    {
    }
        
    public event PropertyChangedEventHandler PropertyChanged;

    private string _fullPath;

    public string FullPath
    {
        get { return _fullPath; }
        set
        {
            if (value != _fullPath)
            {
                _fullPath = value;
                OnPropertyChanged();
            }
        }
    }

    protected void OnPropertyChanged([CallerMemberName] string name = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

}

Main window's code behind:主窗口背后的代码:

public partial class window : Window
{
    protected EEOptionSet options = new EEOptionSet();

    private void BrowseFiles(object sender, RoutedEventArgs e)
    {
        options.FullPath = "Test";
    }
}

Textbox and Button instances in the xaml of my main window (extraneous properties like Grid placement, Alignment, etc removed for brevity):我的主要 window 的 xaml 中的文本框和按钮实例(为简洁起见,删除了诸如网格放置、Alignment 等无关属性):

<TextBox x:Name="FullPathText" Text="{Binding (options.FullPath), Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" Focusable="False"/>
<uc:ButtonExt x:Name="Browse" Content="..." Click="BrowseFiles"/>

NOTE: I have also tried:注意:我也试过:

Text="{Binding options.FullPath, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Path=options.FullPath, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Path=(_currentOptionSet.FullPath), Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"

As well as without the IsReadOnly and Focusable properties.以及没有IsReadOnlyFocusable属性。

  1. PropertyPath (this is the type of the Binding.Path property) can only be set by the path expression to the public property of the source. PropertyPath(这是 Binding.Path 属性的类型)只能通过路径表达式设置为源的公共属性。 And your variable options is a protected field.你的变量options是一个受保护的领域。

  2. If the source is not explicitly specified in the Binding (there are three ways of setting it: Source, ElementName and RelativeSource), then the Data Context of the element in which the binding is set is used for the source.如果绑定中未明确指定源(有三种设置方式:Source、ElementName 和 RelativeSource),则设置绑定的元素的数据上下文将用作源。 You did not specify the source in any of these four ways.您没有以这四种方式中的任何一种方式指定来源。

An example of setting the Data Context and its use.设置数据上下文及其使用的示例。
Written from assumption:从假设写:

  • the EEOptionSet and the OptionSet classes - are one and the same, and you just made a mistake when copying the code; EEOptionSetOptionSet类 - 是同一个类,您只是在复制代码时犯了一个错误;
  • the EEOptionSet class is declared in the same namespace as your window . EEOptionSet class 在与您的window相同的命名空间中声明。
    <Window.DataContext>
        <local:EEOptionSet/>
    <Window.DataContext>
    protected readonly EEOptionSet options;
    public window()
    {
        InitializeComponent();

        options = (EEOptionSet) DataContext;
    }
    <TextBox Text="{Binding FullPath, Mode=OneWay}"
             IsReadOnly="True"
             Focusable="False"/>

You cannot bind to a protected field.您不能绑定到受保护的字段。

Set the DataContext of the window to your field:将 window 的DataContext设置为您的字段:

public partial class window : Window
{
    protected OptionSet options = new OptionSet();

    public window()
    {
        InitializeComponent();
        DataContext = options;
    }

    private void BrowseFiles(object sender, RoutedEventArgs e)
    {
        options.FullPath = "Test";
    }
}

...and remove "options" from the binding path(s) in the XAML markup: ...并从 XAML 标记中的绑定路径中删除“选项”:

Text="{Binding FullPath, UpdateSourceTrigger=PropertyChanged}"

Alternatively, make options a public property of the window and set the DataContext the the window itself:或者,将options设置为 window 的公共属性,并将DataContext设置为 window 本身:

public partial class window : Window
{
    public OptionSet options { get; private set; }

    public window()
    {
        InitializeComponent();
        options =  = new OptionSet();
        DataContext = this;
    }

    private void BrowseFiles(object sender, RoutedEventArgs e)
    {
        options.FullPath = "Test";
    }
}

Then you should keep the binding path as-is:那么你应该保持绑定路径不变:

Text="{Binding options.FullPath, UpdateSourceTrigger=PropertyChanged}"

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

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