wpf 通过 DataTrigger 设置的 TextBox 的文本不更新模型中的属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16397570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:44:11  来源:igfitidea点击:

TextBox's Text set through DataTrigger not updating the property value in Model

wpfdatatrigger

提问by DareToExplore

I am new to WPF and I want to clear a textBox's value if a check box is unchecked. I tried doing that through data triggers.

我是 WPF 的新手,如果未选中复选框,我想清除文本框的值。我尝试通过数据触发器来做到这一点。

Below is the code:

下面是代码:

<TextBox Text="{Binding Path=Amount,Mode=TwoWay}">
                    <TextBox.Style>
                        <Style>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=IsSelected}" Value="false">
                                    <Setter Property="TextBox.Text" Value="{x:Null}"></Setter>
                                </DataTrigger>  
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox> 

My checkbox's value is set in "IsSelected" property of My Model. Here, if the checkbox is unchecked, then the text's updated value, which is {x:Null} in this case, is not reflecting in "Amount" property of my Model. Because of this the text never seems to be changed on the UI. The "Amount" earlier set value is getting set again in the TextBox because of binding

我的复选框的值在我的模型的“IsSelected”属性中设置。在这里,如果未选中该复选框,则文本的更新值(在本例中为 {x:Null})不会反映在我的模型的“Amount”属性中。因此,UI 上的文本似乎永远不会改变。由于绑定,“Amount”先前设置的值在 TextBox 中再次设置

Any help is appreciated. Let me know in case you need any more information or clarification Thanks.

任何帮助表示赞赏。如果您需要更多信息或澄清,请告诉我谢谢。

回答by Viv

In such cases I normally prefer ViewModel / Model doing the "clear" part of the functionality,

在这种情况下,我通常更喜欢 ViewModel / Model 做功能的“清晰”部分,

hence in your case I'd normally do something like:

因此,在您的情况下,我通常会执行以下操作:

public bool IsSelected {
  get {
    return _isSelected;
  }

  private set {
    if (value == _isSelected)
      return;

    RaisePropertyChanging(() => IsSelected);
    _isSelected = value;
    RaisePropertyChanged(() => IsSelected);

    if (_isSelected == false)
      Amount = string.Empty
  }
}

That way the View does not hold responsibility for any logic and thus doesn't need the DataTriggerat all

这样的视图并不适用于任何逻辑的责任,因此并不需要DataTrigger在所有

Update:

更新:

The Problem with your code is when you set the Textwith a Binding in the TextBoxit takes precedence over the value you set in the Stylefor the Text property. You can check this by using this:

您的代码的问题在于,当您Text使用 Binding设置TextBox它时,它优先于您在StyleText 属性中设置的值。您可以使用以下方法进行检查:

<TextBox>
  <TextBox.Style>
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="Text"
              Value="{Binding Path=Amount,
                              Mode=TwoWay}" />
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsSelected}"
                      Value="false">
          <Setter Property="Text"
                  Value="{x:Null}" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </TextBox.Style>
</TextBox>

This will now clear the Text when the CheckBoxis checked, however it will not be updating your Binding(Amount) since essentially your Binding is only active when the CheckBoxis selected.

这将在CheckBox选中时清除文本,但是它不会更新您的 Binding( Amount),因为本质上您的 Binding 仅在CheckBox选择时才处于活动状态。