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

时间:2008-10-29 13:30:57

标签: wpf binding listbox

我已经覆盖了窗口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 =“{TemplateBinding DisplayMemberPath}”

抱怨: 在类型'ListBoxItem'

上找不到静态成员'DisplayMemberPathProperty'

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

1 个答案:

答案 0 :(得分:0)

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

以防有人想知道代码:

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