MatthewHall3D
MatthewHall3D

Reputation: 63

Bind to display member of combo box in item template

I have a combo box where I set the display member property.

<dxe:ComboBoxEdit EditValue="{Binding MyEditVale}" ItemsSource="{Binding MyListOfObjects}" DisplayMember="{Binding MyDisplayMember}" ItemTemplate="{StaticResource ColoredTemplate}"/>

However, I want to use the display member property in the item template.

 <DataTemplate x:Key="ColoredTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <StackPanel Grid.Column="0" ToolTip="This entry has been made obsolete please select another option" Background="Red" Visibility="{Binding IsObsolete, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                <TextBlock  Grid.Column="1" Text="{*I want to bind to the display member here*}" Margin="2,0,0,0"/>
            </Grid>
        </DataTemplate>

I am having a hard time getting this to work. For testing purposes I have been using this code.

<TextBlock  Grid.Column="1" Text="{Binding Name}" Margin="2,0,0,0"/>

The object I am binding to does have a Name property but there could be other instances where this is not available.

Is there any way to use/bind directly the display member property in the item template?

Upvotes: 0

Views: 2209

Answers (3)

T N
T N

Reputation: 406

MyListOfObjects must be a collection of objects and 'Name' must be a property of the object item, then it should work. I took a code snippet from this post (with ListBox but it should be same for Combobox).

<ListBox Grid.Row="0" ItemsSource="{Binding SelectedFiles}" Grid.IsSharedSizeScope="True">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="col0"></ColumnDefinition>
                    <ColumnDefinition SharedSizeGroup="col1"></ColumnDefinition>
                    <ColumnDefinition SharedSizeGroup="col2"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Name}" Margin="10,0"></TextBlock>
                <TextBlock Grid.Column="1" Text="{Binding Length}" Margin="10,0"></TextBlock>
                <TextBlock Grid.Column="2" Text="{Binding LastAccessTime}" Margin="10,0"></TextBlock>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

SelectedFiles is just a list of FileInfo

public List<FileInfo> SelectedFiles
{
    get;
    set;
}

Upvotes: 0

Nitin Purohit
Nitin Purohit

Reputation: 18578

See if this helps you

<TextBlock  Grid.Column="1" Text="{Binding DisplayMember, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dxe:ComboBoxEdit}}}" Margin="2,0,0,0"/>

Upvotes: 0

Anton Kedrov
Anton Kedrov

Reputation: 1767

You can use either DisplayMemberPath or ItemTemplate, but not both at the same time.

DisplayMemberPath

<ComboBox ItemsSource="{Binding Path=MyListOfObjects}" DisplayMemberPath="Name"/>

ItemTemplate

<ComboBox ItemsSource="{Binding Path=MyListOfObjects}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <StackPanel Grid.Column="0" ToolTip="This entry has been made obsolete please select another option" Background="Red" Visibility="{Binding IsObsolete, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                <TextBlock  Grid.Column="1" Text="{Binding Path=Name}" Margin="2,0,0,0"/>
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Upvotes: 1

Related Questions