Home > Software engineering >  Display different values in ComboBox based on a conditional from converter
Display different values in ComboBox based on a conditional from converter

Time:12-10

I have a ComboBox which should display different values based on a condition.

If the SelectedValue's properties Name or Email is not null and is not empty I want to display those.

If above is not true I want to fallback and display the SelectedValue's Username, this is never null or empty.

This is what I have so far.

<ctrls:RadComboBoxExtended Grid.Row="0" Grid.Column="1" ItemsSource="{Binding CurrentValue.LIST_OF_USERS}" SelectedValue="{Binding CurrentValue.User}" focus:FocusExtension.IsFocused="{Binding Focuspoint}">
    <ctrls:RadComboBoxExtended.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Visibility>
                    <MultiBinding Converter="{StaticResource MultiNullOrEmptyStrToVisibility}" ConverterParameter="1">
                        <Binding Path="Name"/>
                        <Binding Path="Email"/>
                    </MultiBinding>
                </TextBlock.Visibility>
                <Run Text="{Binding Name}"/>
                <Run Text="("/><Run Text="{Binding Email}"/><Run Text=")"/>
            </TextBlock>
        </DataTemplate>
    </ctrls:RadComboBoxExtended.ItemTemplate>
</ctrls:RadComboBoxExtended>

Above XAML displays all users which have a Name and Email correctly.

How would I go about displaying the "else branch" for this? As of now the users which do not meet the condition of my converter is blank in the ComboBox. How can I display those?

CodePudding user response:

Simplest way is to surround TextBlock with StackPanel or Grid and put also another TextBlock there, setting ConverterParameter="10" and handling it so, that only one of TextBlocks will be visible.
Another possibility is to use ItemTemplateSelector to define which DataTemplate should be used if RadComboBoxExtended has such a property.

<ctrls:RadComboBoxExtended Grid.Row="0" Grid.Column="1" ItemsSource="{Binding CurrentValue.LIST_OF_USERS}" SelectedValue="{Binding CurrentValue.User}" focus:FocusExtension.IsFocused="{Binding Focuspoint}">
    <ctrls:RadComboBoxExtended.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock>
                    <TextBlock.Visibility>
                        <MultiBinding Converter="{StaticResource MultiNullOrEmptyStrToVisibility}" ConverterParameter="1">
                            <Binding Path="Name"/>
                            <Binding Path="Email"/>
                        </MultiBinding>
                    </TextBlock.Visibility>
                    <Run Text="{Binding Name}"/>
                    <Run Text="("/><Run Text="{Binding Email}"/><Run Text=")"/>
                </TextBlock>
                <TextBlock Text="{Binding Username}">
                    <TextBlock.Visibility>
                        <MultiBinding Converter="{StaticResource MultiNullOrEmptyStrToVisibility}" ConverterParameter="10">
                            <Binding Path="Name"/>
                            <Binding Path="Email"/>
                        </MultiBinding>
                    </TextBlock.Visibility>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </ctrls:RadComboBoxExtended.ItemTemplate>
</ctrls:RadComboBoxExtended>

CodePudding user response:

I would put the choice between name email vs username in the ViewModel. (One of ViewModel responsibilities is to provide data for View in a convenient format)

public string SelectionText => String.IsNullOrEmpty(Name) && String.IsNullOrEmpty(Email) 
                               ? Username 
                               : $"{Name} ({Email})" ;

much shorter DataTemplate and MultiValueConverter is not required

<ctrls:RadComboBoxExtended Grid.Row="0" Grid.Column="1" ItemsSource="{Binding CurrentValue.LIST_OF_USERS}" SelectedValue="{Binding CurrentValue.User}" focus:FocusExtension.IsFocused="{Binding Focuspoint}">
    <ctrls:RadComboBoxExtended.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding SelectionText}"/>
        </DataTemplate>
    </ctrls:RadComboBoxExtended.ItemTemplate>
</ctrls:RadComboBoxExtended>
  • Related