开发者

Accessing ListBox DisplayMemberPath data value in SelectedItem template

I am attempting to create a generic ListBox control to customize edit in place as well as other features.

In the example below, I want to bind the "Text" property of the ListBox "selected item" to the data value of the DisplayMemberPath in the viewed structure. Such XAML binding expression would replace the question marks in the code (Text="{Binding ????????????????").

Using a ContentPresenter instead of binding the text works for display purposes, but I have not been able to bind to the Text component used on the presenter. An alternative to finding the binding expression is to be able to get the Text content from the ContentPresenter.

I can think of a number of ways to accomplish this through code behind, but I am looking for a XAML solution if such thing exists.

I appreciate any ideas. I am almost sure there is a trivial answer to this, but after spending a couple days on it, I admit a nudge in the right direction would greatly help me.

<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>


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

That should work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜