绑定的文本框在代码隐藏更改时不更新

本文关键字:更新 隐藏 代码 文本 绑定 | 更新日期: 2023-09-27 18:05:40

我从另一个项目复制了这段代码,不知道为什么它不工作。我的可观察集合工作很好绑定和更新,但我的文本框没有改变。我有一个按钮单击,让用户选择一个目录(DirectoryBrowse()方法),然后将该值分配给绑定到文本框的数据上下文的属性。PropertyChanged总是空的,我不知道为什么!初始绑定工作得很好,只需注意我在代码隐藏中更改了值。我已经在这方面做得太久了,但任何帮助都会很感激!

DataContext class:

[Serializable]
public class Settings : ViewModels.ViewModelEntity
{
    public static Settings defaultSettings { get; set; }
    private string _ExportDir;
    public string ExportDir
    {
        get { return this._ExportDir; }
        set
        {
            if (this._ExportDir != value)
            {
                this._ExportDir = value;
                this.NotifyPropertyChanged("ExportDir");
            }
        }
    }
    private string _LastRunTime;
    public string LastRunTime
    {
        get { return this._LastRunTime; }
        set
        {
            if (this._LastRunTime != value)
            {
                this._LastRunTime = value;
                this.NotifyPropertyChanged("LastRunTime");
            }
        }
    }
    private string _TSCertPath;
    public string TSCertPath
    {
        get { return this._TSCertPath; }
        set
        {
            if (this._TSCertPath != value)
            {
                this._TSCertPath = value;
                this.NotifyPropertyChanged("TSCertPath");
            }
        }
    }
    public ObservableCollection<Map> Brokers { get; set; }
    public ObservableCollection<Account> Accounts { get; set; }
    public List<Holiday> Holidays { get; set; }
    public bool RefreshHolidays { get; set; }
    public string ProxyServer { get; set; }
    public string ProxyPort { get; set; }
    public string ProxyUsername { get; set; }
    public string ProxyPassword { get; set; }
    public bool TSProd { get; set; }
    public string TSTriad { get; set; }
    public string TSPassword { get; set; }
    public string TSCertPassword { get; set; }
    public Settings()
    {
        this.Brokers = new ObservableCollection<Map>();
        this.Accounts = new ObservableCollection<Account>();
    }
}
Xaml:

<TextBlock TextWrapping="Wrap" Text="File Export Path*"/>
<TextBox TextWrapping="Wrap" Text="{Binding Path=ExportDir, Mode=TwoWay}" />
<Button x:Name="btnBrowseExportDir" Content="..." Click="btnBrowseExportDir_Click"/>

后台代码:

public MainWindow()
{
    InitializeComponent();
    Settings.Initialize();
    this.DataContext = Settings.defaultSettings;
    string[] args = Environment.GetCommandLineArgs();
    if (args.Contains("create"))
    {
        this.Close();
    }
}
private string DirectoryBrowse()
{
    CommonOpenFileDialog dialog = new CommonOpenFileDialog();
    dialog.IsFolderPicker = true;
    CommonFileDialogResult result = dialog.ShowDialog();
    if (result.ToString().ToUpper() == "OK")
    {
        if (!Directory.Exists(dialog.FileNames.First()))
        {
            this.lblStatus.Text = "Invalid directory selected";
            return string.Empty;
        }
        else
        {
            return dialog.FileNames.First();
        }
    }
    else
    {
        this.lblStatus.Text = "Invalid directory selected";
        return string.Empty;
    }
}
private void btnBrowseExportDir_Click(object sender, RoutedEventArgs e)
{
    Settings.defaultSettings.ExportDir = DirectoryBrowse();
}

ViewModelEntity:

public class ViewModelEntity
{
    public event PropertyChangedEventHandler PropertyChanged;
    public virtual void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

绑定的文本框在代码隐藏更改时不更新

Settings.defaultSettings从未被赋值。所以数据绑定没有什么可做的。

缺少Settings.Initialize()的代码

@Dave和@Icepickle向我展示了我错过的东西,没有实现INotifyPropertyChanged!