【问题标题】:Bound Textbox Not Updating on Change in Codebehind绑定文本框不更新代码隐藏的变化
【发布时间】:2015-10-21 03:03:27
【问题描述】:

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

DataContext类:

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

【问题讨论】:

  • 你的 ViewModel 和 Settings-class 是否实现了INotifyPropertyChanged
  • 如果您实现ICommand,您可以将您的按钮绑定到视图模型中的命令。然后可以直接设置文本框绑定属性,而不是设置文本框文本属性。
  • 请包含函数Settings.Initialize()的代码
  • 你的 ViewModelEntity 没有像 Dave 提到的那样实现 INotifyPropertyChanged
  • @Dave 和 Icepickle 成功了。非常感谢你,我快疯了,而且已经很晚了。再次感谢! ps - Settings.Initialize 加载称为默认设置的设置的静态实例。

标签: c# .net wpf wcf-binding


【解决方案1】:

Settings.defaultSettings 从未被赋值。所以数据绑定没有任何用处。

Settings.Initialize() 的密码丢失。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多