多个绑定作为转换器 WPF MVVM 的参数
Multiple bindings as parameter for converter WPF MVVM
我正在尝试更改椭圆的填充画笔。它没有用,所以作为快速修复,我做了一些体力劳动:
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="DarkGray"
Visibility="{Binding Model.TransitLow, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="White"
Visibility="{Binding Model.IndicationHigh, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="DarkGray"
Visibility="{Binding Model.IndicationLow, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="White"
Visibility="{Binding Model.High, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="DarkGray"
Visibility="{Binding Model.Low, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="Red"
Visibility="{Binding Model.FeedbackError, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="Red"
Visibility="{Binding Model.FunctionFailed, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="Red"
Visibility="{Binding Model.LossOfXE, Converter={StaticResource BoolToVisibilityConverter}}"/>
如您所见,我基本上只是创建了图层,并使用了隐藏和显示算法。
理想情况下,所有这些绑定属性都将进入一个转换器执行逻辑并为一个椭圆返回一个画笔。
理想情况下,这就是我想要的:
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="{Binding Model.LossOfXE,Model.FunctionFailed....... Converter={StaticResource attrsToBrushConverter}}"/>
假设椭圆的颜色在多个条件下发生变化,您可以使用 IMultiValueConverter
界面。例如,您有 2 个按钮可以确定 ellipse
是什么颜色。所以你可以把你的转换器编码成这样
public class EllipseColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
bool btn1 = (bool)values[0];
bool btn2 = (bool)values[1];
if (btn1 && !btn2)
return Brushes.Red;
else if (btn2 && !btn1)
return Brushes.Blue;
else if (btn1 && btn2)
return Brushes.Pink;
else
return Brushes.Black;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后在你的 xaml
中你可以实现
<Window.Resources>
<local:EllipseColorConverter x:Key="ellipseColorConvert"/>
</Window.Resources>
<Ellipse Grid.Row="0" Width="100" Height="100">
<Ellipse.Fill>
<MultiBinding Converter="{StaticResource ellipseColorConvert}">
<Binding ElementName="btn1" Path="IsPressed"/>
<Binding ElementName="btn2" Path="IsPressed"/>
</MultiBinding>
</Ellipse.Fill>
</Ellipse>
<Button Grid.Row="1" Content="button 1" x:Name="btn1"/>
<Button Grid.Row="2" Content="button 2" x:Name="btn2"/>
我正在尝试更改椭圆的填充画笔。它没有用,所以作为快速修复,我做了一些体力劳动:
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="DarkGray"
Visibility="{Binding Model.TransitLow, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="White"
Visibility="{Binding Model.IndicationHigh, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="DarkGray"
Visibility="{Binding Model.IndicationLow, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="White"
Visibility="{Binding Model.High, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="DarkGray"
Visibility="{Binding Model.Low, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="Red"
Visibility="{Binding Model.FeedbackError, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="Red"
Visibility="{Binding Model.FunctionFailed, Converter={StaticResource BoolToVisibilityConverter}}"/>
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="Red"
Visibility="{Binding Model.LossOfXE, Converter={StaticResource BoolToVisibilityConverter}}"/>
如您所见,我基本上只是创建了图层,并使用了隐藏和显示算法。
理想情况下,所有这些绑定属性都将进入一个转换器执行逻辑并为一个椭圆返回一个画笔。
理想情况下,这就是我想要的:
<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="{Binding Model.LossOfXE,Model.FunctionFailed....... Converter={StaticResource attrsToBrushConverter}}"/>
假设椭圆的颜色在多个条件下发生变化,您可以使用 IMultiValueConverter
界面。例如,您有 2 个按钮可以确定 ellipse
是什么颜色。所以你可以把你的转换器编码成这样
public class EllipseColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
bool btn1 = (bool)values[0];
bool btn2 = (bool)values[1];
if (btn1 && !btn2)
return Brushes.Red;
else if (btn2 && !btn1)
return Brushes.Blue;
else if (btn1 && btn2)
return Brushes.Pink;
else
return Brushes.Black;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后在你的 xaml
中你可以实现
<Window.Resources>
<local:EllipseColorConverter x:Key="ellipseColorConvert"/>
</Window.Resources>
<Ellipse Grid.Row="0" Width="100" Height="100">
<Ellipse.Fill>
<MultiBinding Converter="{StaticResource ellipseColorConvert}">
<Binding ElementName="btn1" Path="IsPressed"/>
<Binding ElementName="btn2" Path="IsPressed"/>
</MultiBinding>
</Ellipse.Fill>
</Ellipse>
<Button Grid.Row="1" Content="button 1" x:Name="btn1"/>
<Button Grid.Row="2" Content="button 2" x:Name="btn2"/>