选项卡更改时的 WPF 组合框文本绑定(bind)问题

标签 wpf mvvm wpf-controls binding

我正在一个遵循 MVVM 的 WPF 应用程序中工作。

我在 Combobox 的 text 属性中使用了绑定(bind),并且此组合位于选项卡内。

每当我切换选项卡时,Combobox 的 text 属性 cahnged 都会被触发,并且 text 设置为 string.Empty。

最佳答案

如果您不希望文本为空,您可以尝试:

看法<ComboBox Text={Binding ComboxText} ... />
从评论编辑:<ComboBox Text={Binding ComboxText, TargetNullValue=SomeValue} ... />
View 模型

/*INotifyPropertyChanged property*/
private string comboxText;
public string ComboxText 
{  get { return comboxText; }  
   set { 
      if (value != comboxText)  
      {// value changed ->  
         if (!string.IsNullOrWhiteSpace(value))   
         {// value not null, empty, whitespace ->  
            comboxText = value;
         }  
         /*INPC code*/
      }  
   }
}

关于选项卡更改时的 WPF 组合框文本绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4792468/

相关文章:

c# - Xamarin Forms Exrin Framework将容器推为模态?

wpf - 放置在 DataGrid.DataGridTemplateColumn 中的 UserControl 中 PopupMenu 内的文本框永远不会获得 KeboardFocus?

c# - 将每个对象的特定变量显示到组合框中

wpf - 如何绑定(bind)到 TreeView 的子分支或叶子(MVVM 样式)?

c# - 如何在 WPF 中捕获从子组件到父组件的激活命令?

c# - 在WPF中禁用控件的选择背景效果

c# - 将标志枚举绑定(bind)到包含复选框的列表框

wpf - 将 Textbox IsFocused 绑定(bind)到 Popup IsOpen 以及附加条件

unit-testing - 使代码可单元测试的最佳方法

c# - WPF TabControl 启动时没有选定的项目