【问题标题】:How do you access the DisplayMemberPath from a ListBox when trying to bind it to the content of the ListBoxItem?尝试将 ListBox 绑定到 ListBoxItem 的内容时,如何从 ListBox 访问 DisplayMemberPath?
【发布时间】:2010-09-19 19:04:07
【问题描述】:

我已经覆盖了 Windows 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”

谁能指出我正确的方向?

【问题讨论】:

    标签: wpf binding listbox


    【解决方案1】:

    没关系,我现在已经从 ListBox 中获取了值,我需要做的就是将其转换为从 itemssource 中的数据对象中获取我需要的属性。

    以防万一有人想知道代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多