Elisabeth
Elisabeth

Reputation: 21206

Show custom DisplayMember in WPF ComboBox

this is my ComboBox:

WeeklyStartDate and WeekNumber I want to display in the DisplayMember property. But WPF says I can not use DisplayMember as I use already an ItemTemplate...

How can I display with the below code a custom DisplayMember when I clicked on an item in the ComboBox ?

At the moment it is using the ToString() method to render the DisplayMember showing XXX.ViewModel.WeeklyDateViewModel which is the namespace+type.

<ComboBox    
    Style="{StaticResource ComboBoxStyle1}"
    AlternationCount="2"
    FontSize="16"
    VerticalContentAlignment="Center"
    Width="150" 
    IsEditable="True"
    SelectedItem="{Binding SelectedWeeklyDateViewModel,Mode=TwoWay}"
    ItemContainerStyle="{StaticResource alternateColor}"         
    ItemsSource="{Binding WeeklyDatesList}">
        <ComboBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel VirtualizingStackPanel.VirtualizationMode="Recycling" />
            </ItemsPanelTemplate>
        </ComboBox.ItemsPanel>                    
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="100" Foreground="blue" Text="{Binding WeekStartDate,Mode=TwoWay, StringFormat='yyyy-MM-dd'}" />
                    <TextBlock Text=" " />
                    <TextBlock Width="100" Foreground="Red" Text="{Binding WeekNumber}"  />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

Upvotes: 0

Views: 2421

Answers (1)

benPearce
benPearce

Reputation: 38333

Quick and dirty method: override ToString on your WeeklyDateViewModel to return the string you want to display

Upvotes: 1

Related Questions