[英]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.