繁体   English   中英

Xamarin.Forms DataTrigger不起作用

[英]Xamarin.Forms DataTrigger not working

我试图在Xamarin.Forms的控件上设置DataTrigger,但无法使其正常工作。

我在ViewModel中有属性,可以对Bos bool IsValid执行OnPropertyChange执行

我努力了:

Xaml中的DataTrigger:

<customControls:NumericTextBox
    Grid.Row="0" Grid.Column="2"
    Text="{Binding StringValue, Mode=TwoWay}"
    IsEnabled="{Binding IsEditable}"
    XAlign="End">
  <customControls:NumericTextBox.Style>
    <Style TargetType="customControls:NumericTextBox">
      <Style.Triggers>
        <DataTrigger TargetType="customControls:NumericTextBox" Binding="{Binding IsValid}" Value="true">
          <Setter Property="TextColor" Value="Red"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </customControls:NumericTextBox.Style>
</customControls:NumericTextBox>

获取异常: The Property TargetType is required to create a Xamarin.Forms.DataTrigger object.

DataTrigger处于控制状态:

_item = new DataTrigger(typeof(NumericTextBox));
_item.Binding = new Binding("IsValid",BindingMode.Default,new NegativeBooleanConverter());
_item.Value = true;
Setter s = new Setter();
s.Property = TextColorProperty;
s.Value = Color.Red;
_item.Setters.Add(s);
this.Style.Triggers.Add(_item);

异常: Exception has been thrown by the target of an invocation.

我也尝试过更改行: this.Style.Triggers.Add(_item); this.Triggers.Add(_item); 这并没有引发异常,但是没有奏效。

在这最后的尝试中,它甚至命中了转换器,但没有更改Control的TextColor。

难道我做错了什么? 如何处理?

我遇到了同样的问题。 解决方法是,可以使用ViewModel中的TextColor属性绑定到Color属性,而不是使用DataTrigger。 然后,可以在IsValid setter中根据该值设置颜色。

public bool IsValid
{
    get { return _isValid; }
    set
    {
        _isValid = value;
        MyNewTextColorProperty = _isValid ? Color.Blue : Color.Red;

        OnPropertyChanged();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM