简体   繁体   中英

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. For example you have 2 buttons that will determine what color the ellipse is. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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