在我的 window.xaml 中,我有以下代碼:
xmlns:converters="clr-namespace:HMIPlc.Helpers"
<Window.Resources>
<ResourceDictionary>
<converters:ColorConverter x:Key="ColorOnChange"/>
</ResourceDictionary>
</Window.Resources>
<Rectangle Fill="{Binding Path=varUnit.InSimulation, Converter={StaticResource ColorOnChange}}"/>
我還想在字串“Yellow”或“Orange”中給函式一個值,這樣我就可以對不同顏色的不同矩形使用相同的函式。
Helpers 目錄中的我的 ColorConverter.cs 類:
public class ColorConverter : IValueConverter
{
public ColorConverter()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool tempBool = (bool)value;
if(tempBool == true)
{
return new SolidColorBrush(Colors.Orange);
} else
{
return new SolidColorBrush(Colors.White);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
這樣我就可以在我的 XAML 中確定顏色是橙色還是黃色。有什么好的方法可以做到這一點嗎?
uj5u.com熱心網友回復:
您可以將CommandParameter屬性設定為BrushorColor并在轉換器中轉換“引數”引數:
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Color color = (Color)parameter;
bool tempBool = (bool)value;
if (tempBool == true)
{
return new SolidColorBrush(color);
}
else
{
return new SolidColorBrush(color);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<Rectangle Fill="{Binding Path=varUnit.InSimulation, Converter={StaticResource ColorOnChange},
ConverterParameter={x:Static Colors.Orange}}"/>
uj5u.com熱心網友回復:
如果要系結多個值,請使用MultiValueConverter. 我在Brush這里使用更通用的型別,因此您不僅限于系結SolidColorBrush.
public class ColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length != 3 ||
!(values[0] is bool value) ||
!(values[1] is Brush brushTrue) ||
!(values[2] is Brush brushFalse))
return Binding.DoNothing;
return value ? brushTrue : brushFalse;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后使用 aMultiBinding并指定要系結的屬性以及畫筆。為了參考內置畫筆,您可以將靜態Brushes型別與x:Static.
<Rectangle>
<Rectangle.Fill>
<MultiBinding Converter="{StaticResource ColorOnChange}">
<Binding Path="varUnit.InSimulation"/>
<Binding Source="{x:Static Brushes.Orange}"/>
<Binding Source="{x:Static Brushes.White}"/>
</MultiBinding>
</Rectangle.Fill>
</Rectangle>
uj5u.com熱心網友回復:
當您想為轉換器提供附加值時,您可以:
a) 在轉換器上添加其他屬性,然后可以在 XAML 中分配這些屬性:
public class ColorConverter : IValueConverter
{
public Color BackupColor { get; set; }
public ColorConverter()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool tempBool = (bool)value;
if(tempBool == true)
{
return new SolidColorBrush(Colors.Orange);
} else
{
return new SolidColorBrush(Colors.White);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
...
<converters:ColorConverter x:Key="ColorOnChange" BackupColor="Yellow"/>
...
b) 利用通常是字串文字的 ConverterParameter:
<Rectangle Fill="{Binding Path=varUnit.InSimulation, Converter={StaticResource ColorOnChange}, CommandParameter='Yellow'}"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/384784.html
