【问题标题】:WPF: Binding to the Binding modeWPF:绑定到绑定模式
【发布时间】:2018-12-16 19:44:24
【问题描述】:

在 WPF 中,我尝试使用数据绑定来定义 BindingMode。

<controls:BoolToStringConverter x:Key="BoolToStringConverter" FalseValue="OneWay" TrueValue="TwoWay" />
<TextBox Text="{Binding MyText, Mode="{Binding Path=IsWriteable, Converter={StaticResource BoolToStringConverter}}" />

另外,我尝试使用 System.Windows.Data.BindingMode 枚举作为属性“MyBindingMode”的数据类型并绑定到它,但它也不起作用

<TextBox Text="{Binding MyText, Mode={Binding Path=MyBindingMode}}" />

这种绑定有没有可能,或者有什么合适的方法来实现这一点?

编辑:

实际上,在我的上下文中,它不是 TextBox,而是 DataGridTextColumn。我尝试绑定IsReadOnly="{Binding IsOnlyReadable}",但这对DataGridTextColumn 没有影响。将其固定为 IsReadOnly=True 有效,但将其绑定到始终为 true 的属性不会产生此效果。

终于解决了,我用它来绑定 DataGridColumn:https://stackoverflow.com/a/27465022/9758687

【问题讨论】:

  • 你想达到什么目的?如果想让TextBox不接受输入,绑定IsWriteable到Textbox的IsReadOnly属性&lt;TextBox Text="{Binding MyText, Mode=TwoWays}" IsReadOnly="{Binding IsWriteable, Mode=OneWay, Converter={StaticResource BoolToNegativeConverter}}" /&gt;

标签: c# wpf data-binding wpfdatagrid datagridtextcolumn


【解决方案1】:

看看这个链接。这似乎是您正在寻找的: .Net v4 DataGridTextColumn.IsReadOnly seems to be faulty

这是可能解决您的问题的代码:

  <DataGridTextColumn>  
   <DataGridTextColumn.CellStyle>
   <Style>
     <Setter Property="UIElement.IsEnabled" Value="{Binding IsOnlyReadable}" />
   </Style>
  </DataGridTextColumn.CellStyle>
 </DataGridTextColumn>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2011-08-25
  • 1970-01-01
  • 2015-07-04
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多