0
votes

I have the following xaml:

<ListView x:Name="SomeClass_SomeListProperty">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <WrapPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                    <TextBlock Text="{Binding Type}"/> <!--This is a string-->
                    <TextBlock Text=": "/>
                    <TextBlock Text="{Binding Number}"/> <!--This is a long-->
                </WrapPanel>
                <Grid Grid.Column="1" HorizontalAlignment="Center">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Image Stretch="None"
                      Source="{Binding BoolThatDependsOnType, 
                      Converter={StaticResource BoolToImageConverter}, 
                      ConverterParameter='large'}" />
                    <Button Grid.Column="1" Content="Settings"
                      cal:Message.Attach="MethodCallThatDependsOnType"/>
                </Grid>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Can I somehow bind the bool "BoolThatDependsOnType" depending on the value of Type? The problem I'm having is that the image source is set depending on if a certain bool is true or false. I want to select which bool to bind to depending on if Type is "type1", "type2" or "type3" for the particular list item.

1
Use a MultiBinding with an appropriate multi-binding converter.Clemens
Don't know how I've missed this, looks like the right thing for me, will try it out! @ClemensYousif Touma
I don't think this will quite work unless I am missing something. If Type is "type1" I need the Image Source to bind to the bool property IsType1Ok in the view model (which is then converted to a bitmap). Is this even possible? @ClemensYousif Touma
In the MultiBinding you would bind to Type and all the boolean properties, and in the converter you would select the appropriate boolean value depending on the value of Type.Clemens
All right, I was going to try that but it felt like a bad solution to include all properties "just in case". Guess i'm lucky there's only 3 of them in this caseYousif Touma

1 Answers

0
votes

With inspiration from @Clemens I came to the following solution using a IMultiValueConverter and MultiBinding (and no the variable and method names are not the actual ones used :-) ).

public class TypeAndMultipleBoolsToImageConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values == null) return new BitmapImage(new Uri("some/path.png", UriKind.Relative));
            bool isOk;
            switch (((string) values[0]).ToLower(culture))
            {
                case "type1":
                    isOk = (bool) values[1];
                    break;
                case "type2":
                    isOk = (bool) values[2];
                    break;
                case "type3":
                    isOk = (bool) values[3];
                    break;
                default:
                    return new BitmapImage(new Uri("/some/path.png", UriKind.Relative));
            }
            return ConvertBoolToImage(isOk);
        }

        private static BitmapImage ConvertBoolToImage(bool isOk)
        {
            return isOk
                ? new BitmapImage(new Uri("/some/ok/path.png", UriKind.Relative))
                : new BitmapImage(new Uri("/some/not/ok/path.png", UriKind.Relative));
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

<Image Stretch="None">
    <Image.Source>
        <MultiBinding Converter="{StaticResource TypeAndMultipleBoolsToImageConverter}">
            <Binding Path="Type"/>
            <Binding Path="DataContext.IsType1Ok"
                     RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}"/>
            <Binding Path="DataContext.IsType2Ok"
                     RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}"/>
            <Binding Path="DataContext.IsType3Ok"
                     RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}"/>
        </MultiBinding>
    </Image.Source>
</Image>