dba
dba

Reputation: 1175

WPF ListBox Conditional DisplayMemberPath

I bind a Collection (of MyClass) to a listbox. Via DisplayMemberPath I can set the Property to be displayed. So far so good. Is there an Option to use a Condition which prop shall be displayed? My goal is to display an alternative Prop Value, if the main Prop is Empty... Can this be achieved through DisplayMemberPath or do need some styling of the ListBoxItems...?

I'd prefer a XAML-Solution though :)

BR,D

Upvotes: 0

Views: 476

Answers (1)

adminSoftDK
adminSoftDK

Reputation: 2092

Something like this may work, you set your displaymemberpath to something, but if that property is null, then your displaymemberpath takes something else instead

<ComboBox>
    <ComboBox.Style>
        <Style TargetType="ComboBox">
            <Setter Property="DisplayMemberPath" Value="FirstOption">    
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding FirstOption}" Value="{x:Null}">
                    <Setter Property="DisplayMemberPath" Value="SecondOption"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
</ComboBox>

Upvotes: 1

Related Questions