【问题标题】:C# WPF/DataGrid: Is there something that TextBox offers that a UserControl doesn'tC# WPF/DataGrid:TextBox 是否提供了 UserControl 没有的东西
【发布时间】:2019-07-17 11:17:24
【问题描述】:

我有一个用户控件,我想用它来编辑 DataGrid 中的数据。 UserControl 具有我用于绑定的 TextProperty。编辑单元格时,会显示 TextProperty 的内容。但是当离开编辑模式时,我的项目的 TextPropery 没有更新。

在这里我找到了解决方案:C# Wpf Editing Datagrid does not update it's itemsource

对于 TextBox,此绑定有效。

factoryTextBox.SetBinding(TextBox.TextProperty, new Binding("Title"));

但对于我的 CustomTextBox,它仅适用于将模式设置为双向模式。

factoryTextBox.SetBinding(CustomTextBox.TextProperty, new Binding("Title") {
  Mode = BindingMode.TwoWay // <--
});

为什么我的用户控件需要 TwoWay,而 TextBox 不需要。我错过了什么吗?

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    文本框的 textproperty 为其设置了元数据,使其默认为双向绑定。 你想要一些非常粗略的东西:

      public static readonly DependencyProperty TextProperty =
       DependencyProperty.Register(
         "TextProperty",
         typeof(string),
         typeof(CustomTextBox),
         new FrameworkPropertyMetadata(string.empty
     , FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多