0

I used the following code to preselect some rows in a listbox:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
       <Setter Property="IsSelected" Value="{Binding f_selected, Mode=OneWay}" />
    </Style>
</ListBox.ItemContainerStyle>

The value of f_selected in the code can be only true or false, but on the DB the values are y/n. I've used a trick to convert y/n to true/false by using an object, but higher heads have asked me to work only with y/n in the objects. Is there any way to work with a string instead of a bool or to do the conversion in the XAML or the viewmodel?

Thanks for the help and as always sorry for the bad english.

  • Do you know the why of the requirement that only strings should be used. That might give away how this should be implemented. – Emond Apr 05 '19 at 14:49

2 Answers2

0

Note: mm8's answer using DataTrigger's is great if you're developing a WPF application.

In WPF and UWP, you can create a custom implementation of the IValueConverter interface to achieve this with almost the same code. Basically, it will transform your string input to a boolean depending on the rules you define.

WPF:

public class StringToBooleanConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString().Equals("y");
    }

    // This is not really needed because you're using one way binding but it's here for completion
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(value is bool)
        {
            return value ? "y" : "n";
        }
    }
}

UWP:

The above code is exactly the same except for the last parameter in Convert and ConvertBack methods:

public object Convert(object value, Type targetType, object parameter, string language) { }

public object ConvertBack(object value, Type targetType, object parameter, string language) { }

The following is more or less the same for both WPF and UWP. You can use the converter in XAML to convert from string to bool:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
       <Setter Property="IsSelected" Value="{Binding f_selected, Mode=OneWay, Converter={StaticResource StringToBooleanConverter}}" />
    </Style>
</ListBox.ItemContainerStyle>

Also, remember to introduce the converter in the beginning:

<Window.Resources>
    <local:YesNoToBooleanConverter x:Key="StringToBooleanConverter" />
</Window.Resources>
Timo Salomäki
  • 7,099
  • 3
  • 25
  • 40
0

In WPF you could use a DataTrigger:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding f_selected}" Value="y">
                <Setter Property="IsSelected" Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

In UWP you could accomplish the same thing using a DataTriggerBehavior.

mm8
  • 163,881
  • 10
  • 57
  • 88