![](/img/trans.png)
[英]How to pass collection to binding converter parameter in silverlight
[英]Binding Converter how to pass a parameter to function
在我的 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}}"/>
我还想在字符串“黄色”或“橙色”中为 function 提供一个值,因此我可以将相同的 function 用于具有不同 Z62848E3CE5804AA975513A79222 的不同矩形。
我的 ColorConverter.cs class 在 Helpers 目录中:
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 中确定颜色是橙色还是黄色。 有什么好的方法可以做到这一点吗?
当您想为转换器提供附加值时,您可以:
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();
}
}```
```xaml
...
<converters:ColorConverter x:Key="ColorOnChange" BackupColor="Yellow"/>
...
b)利用通常是字符串文字的 CommandParameter :
<Rectangle Fill="{Binding Path=varUnit.InSimulation, Converter={StaticResource ColorOnChange}, CommandParameter='Yellow'}"/>
如果要绑定多个值,请使用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();
}
}
然后使用MultiBinding
并指定要绑定的属性以及画笔。 为了引用内置画笔,您可以使用带有x:Static
的 static Brushes
类型。
<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>
您可以将CommandParameter
属性设置为Brush
或Color
并在转换器中转换“参数”参数:
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}}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.