在尝试将ListBox绑定到ListBoxItem的内容时,如何从ListBox访问DisplayMemberPath?

时间:2021-12-17 21:13:14

I have overridden the windows ListBox in order to display an image and a piece of text in each ListBoxItem, but I need to filter the contents of the text displayed I was hoping to do this by accessing the DisplayMemberPath of the actual ListBox however I can't get it working.

我已经覆盖了窗口ListBox,以便在每个ListBoxItem中显示图像和一段文本,但我需要通过访问实际ListBox的DisplayMemberPath来过滤显示的文本内容我希望这样做但是我可以'让它工作。

<Setter Property="ItemContainerStyle">
        <Setter.Value>
            <!-- Simple ListBoxItem - This is used for each Item in a ListBox. The item's content is placed in the ContentPresenter -->
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid SnapsToDevicePixels="true">
                                <Border x:Name="Border">
                                    <Grid Height="40">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Image
                                            Source="{Binding Path=ThumbnailImage}"
                                            Height="30"
                                            Width="30"
                                            Grid.Column="0"/>

                                        <Label 
                                            x:Name="Text"
                                            Content="{TemplateBinding DisplayMemberPath}"
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                            VerticalContentAlignment="Center"
                                            HorizontalAlignment="Stretch"
                                            Grid.Column="1"
                                            Height="40"/>
                                    </Grid>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter Property="FontWeight" Value="Bold" TargetName="Text"/>
                                    <Setter Property="Foreground" Value="White" TargetName="Text"/>
                                    <Setter Property="Background" Value="Blue" TargetName="Border"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>

This is the code I am using for my style and this was the line I can't get working:

这是我用于我的风格的代码,这是我无法工作的代码:

Content="{TemplateBinding DisplayMemberPath}"

It complains with: Cannot find the static member 'DisplayMemberPathProperty' on the type 'ListBoxItem'

它抱怨:在类型'ListBoxItem'上找不到静态成员'DisplayMemberPathProperty'

Can anyone point me in the right direction?

谁能指出我正确的方向?

1 个解决方案

#1


0  

Its ok, I have got the value from the ListBox now, all i need to do is convert it to take the property i need from the dataobject in the itemssource.

没问题,我现在从ListBox获得了值,我需要做的就是将它转换为从itemssource中的dataobject获取我需要的属性。

Just in case anyone wanted to know the code:

以防有人想知道代码:

Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DisplayMemberPath}"

#1


0  

Its ok, I have got the value from the ListBox now, all i need to do is convert it to take the property i need from the dataobject in the itemssource.

没问题,我现在从ListBox获得了值,我需要做的就是将它转换为从itemssource中的dataobject获取我需要的属性。

Just in case anyone wanted to know the code:

以防有人想知道代码:

Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DisplayMemberPath}"