简体   繁体   中英

Display different values in ComboBox based on a conditional from converter

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?

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 TextBlock s 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>

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>

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