1

Within a ListView control, how to use the DisplayMemberPath property when I want to change the ItemContainerStyle?

I used a ListView control in the ControlTemplate of my control and the DisplayMemberPath property is set via Binding from outside of control.

<ListView ItemsSource="{TemplateBinding ItemsSource}"
          DisplayMemberPath="{TemplateBinding DisplayMemberPath}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Item:" />
                            <TextBlock Text="{Binding}" />
                            <TextBlock Text=", " />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Is it possible to solve this in XAML? How is it solved in the original ControlTemplate?


I try to solve it with a MultiValueConverter, where I bind the Collection Item and DisplayMemberPath.

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource NameToValueConverter}">
            <Binding />
            <Binding Path="DisplayMemberPath" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ListView}}"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

Is it necessary? Or is it the display value already resolved by the original ListView control?

1 Answer 1

2

Is it possible to solve it by XAML?

No, you use either a DisplayMemberPath or an ItemTemplate/ItemContainerStyle but not both.

Also, the DisplayMemberPath property is supposed to be set to a string that specifies a name of a property of the underlying data class. It is not a dependency property that you can bind to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.