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

StackOverflow https://stackoverflow.com/questions/246792

  •  05-07-2019
  •  | 
  •  

我已经覆盖了窗口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>

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

Content =&quot; {TemplateBinding DisplayMemberPath}&quot;

抱怨: 在类型'ListBoxItem'

上找不到静态成员'DisplayMemberPathProperty'

有人能指出我正确的方向吗?

有帮助吗?

解决方案

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

以防有人想知道代码:

Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DisplayMemberPath}"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top