简体   繁体   English

多个绑定作为转换器WPF MVVM的参数

[英]Multiple bindings as parameter for converter WPF MVVM

I am trying to change the fill brush of an ellipse. 我正在尝试更改椭圆的填充笔刷。 It didn't work, so as a quick fix i did some manual labor: 它没有用,所以作为快速解决方案,我做了一些体力劳动:

<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}}"/>

As you can see i have basically just created layers, and used hide and show algorithm. 如您所见,我基本上只是创建了图层,并使用了隐藏和显示算法。

Ideally all these binding attributes would go into one converter doing the logic and returning one brush for one ellipse. 理想情况下,所有这些绑定属性都将进入一个转换器,执行逻辑并为一个椭圆返回一个画笔。

Ideally something like this is what i want: 理想情况是我想要这样的东西:

<Ellipse Height="55" Width="55" Grid.Column="1" Grid.Row="1" Fill="{Binding Model.LossOfXE,Model.FunctionFailed....... Converter={StaticResource attrsToBrushConverter}}"/>   

Assuming that the color of the ellipse changes from multiple conditions you can use the IMultiValueConverter interface. 假设椭圆的颜色在多个条件下发生变化,则可以使用IMultiValueConverter接口。 For example you have 2 buttons that will determine what color the ellipse is. 例如,您有2个按钮将确定ellipse颜色。 So you can code your converter to look something like this 因此,您可以对转换器进行编码,使其看起来像这样

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();
    }
}

Then in your xaml you can implement 然后在您的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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM