运行时会访问两个转换器,但访问第二个转换器Convert()以将值变为红色,但永远不会访问Convert Back来禁用menuItem
<Style>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource MultiValueConverterColour}" >
<Binding ElementName="upperLimitDefault" Path="Text" />
<Binding ElementName="upperLimitMaximum" Path="Text" />
<Binding ElementName="upperLimitMinimum" Path="Text" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="TextBox.Foreground" Value="{Binding ElementName=menuItem, Path=IsEnabled, Mode=TwoWay, Converter={StaticResource myColorErrorConverter}, UpdateSourceTrigger=PropertyChanged}" />
</DataTrigger>
</Style.Triggers>
</Style>
两个转换器:
MultiValue:
public class MultiValueConverterErrorColour : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (null != values)
{
string y = (String)values[0];
string y2 = (String)values[1];
string y3 = (String)values[2];
if (string.IsNullOrEmpty(y) || string.IsNullOrEmpty(y2) || string.IsNullOrEmpty(y3))
{ return false; }
var value = int.Parse(y, System.Globalization.NumberStyles.Integer);
var value2 = int.Parse(y2, System.Globalization.NumberStyles.Integer);
var value3 = int.Parse(y3, System.Globalization.NumberStyles.Integer);
// int limit2 = int.Parse(limit, System.Globalization.NumberStyles.Integer);
if (value >= value3 && value2 >= value && value2 >= value3)
{
return false;
}
return true;
}
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
ValueConverter:
public class myColorErrorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
value = "Red";
return value as string;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (null != value)
{
string x = value.ToString();
if (x == "#FF000000")
{ return false; ; }
}
return true;
}
}
更新:经过几天的研究,我得出的结论是,这是不可能的,我将使用对所有文本框的多重绑定并检查前景颜色来绑定我的menuItem 。