在SelectedItem模板中访问ListBox DisplayMemberPath数据值

时间:2011-02-09 20:39:30

标签: wpf xaml binding

我正在尝试创建一个通用的ListBox控件来自定义编辑以及其他功能。

在下面的示例中,我想将ListBox“selected item”的“Text”属性绑定到查看结构中DisplayMemberPath的数据值。这种XAML绑定表达式将替换代码中的问号(Text =“{Binding ????????????????”)。

使用ContentPresenter而不是绑定文本可用于显示目的,但我无法绑定到演示者上使用的Text组件。找到绑定表达式的另一种方法是能够从ContentPresenter获取Text内容。

我可以想出通过代码实现这一目标的多种方法,但是如果存在这样的话,我正在寻找一个XAML解决方案。

我很欣赏任何想法。我几乎肯定对此有一个微不足道的答案,但在花了几天之后,我承认在正确的方向上轻推会对我有很大的帮助。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <XmlDataProvider x:Key="NobelLaureatesInPhysics"
                     XPath="/NobelLaureatesInPhysics">
        <x:XData>
            <NobelLaureatesInPhysics xmlns="">
                <NobelLaureate>
                    <ID>1</ID>
                    <Name>Wilhelm Röntgen</Name>
                    <AwardDate>12/10/1901</AwardDate>
                </NobelLaureate>
                <NobelLaureate>
                    <ID>2</ID>
                    <Name>Hendrik Lorentz</Name>
                    <AwardDate>12/10/1902</AwardDate>
                </NobelLaureate>
                <NobelLaureate>
                    <ID>3</ID>
                    <Name>Pieter Zeeman</Name>
                    <AwardDate>12/10/1902</AwardDate>
                </NobelLaureate>
            </NobelLaureatesInPhysics>
        </x:XData>
    </XmlDataProvider>

    <ControlTemplate x:Key="ItemTemplate"
                     TargetType="ListBoxItem">
        <TextBlock Foreground="Black">
             <ContentPresenter />
        </TextBlock>
    </ControlTemplate>

    <ControlTemplate x:Key="SelectedItemTemplate"
                     TargetType="ListBoxItem">
        <TextBox Background="Black"
                 Foreground="White"
                 Text="{Binding ????????????????"/>
    </ControlTemplate>

    <Style TargetType="{x:Type ListBoxItem}"
           x:Key="ContainerStyle">
        <Setter Property="Template"
                Value="{StaticResource ItemTemplate}" />
        <Style.Triggers>
            <Trigger Property="IsSelected"
                     Value="True">
                <Setter Property="Template"
                        Value="{StaticResource SelectedItemTemplate}" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="TestListBoxStyle"
           TargetType="{x:Type ListBox}">
        <Setter Property="ItemContainerStyle"
                Value="{DynamicResource ContainerStyle}" />
    </Style>
</Window.Resources>

<Grid>
    <ListBox Style="{DynamicResource TestListBoxStyle}"
             ItemsSource="{Binding Source={StaticResource NobelLaureatesInPhysics}, XPath=NobelLaureate}"
             DisplayMemberPath="Name"/>
</Grid>

1 个答案:

答案 0 :(得分:0)

{Binding Path=DisplayMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, Type={x:Type ListBox}}

那应该有用

相关问题