954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

wpf Binding to a Listbox.SelectedItem.Content property within a datatemplate.

Hi All,

I'm trying to bind a textbox to a listbox's selected.Content properties.
The listbox displays the items as designed in the datatemplate.
But I can't get a textbox to display one/any of the selectedItems properties.

DataTemplate

<DataTemplate x:Key="dt_Afloeser" >
            <Border BorderBrush="Black" BorderThickness="0.5" CornerRadius="2" Margin="2,2,2,2">
                <DockPanel HorizontalAlignment="Center">
                    <TextBlock Name="navn" Text="{Binding Path=navn}" Margin="2,2,2,2" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="WrapWithOverflow" />
                    <TextBlock Width="20"/>
                    <TextBlock Name="telefonNr"  Text="{Binding Path=telefonNr}" Margin="2,2,10,2" Width="auto" HorizontalAlignment="Left"  VerticalAlignment="Center" TextWrapping="WrapWithOverflow" />
                </DockPanel>
            </Border>
        </DataTemplate>


The DataContext for the Listbox is set in code.

<ListBox Name="lb_Afloesere" ItemsSource="{Binding}" ItemTemplate="{StaticResource dt_Afloeser}" IsEnabled="True" />

This only shows namespace.Afloeser

<TextBox Text="{Binding ElementName=lb_Afloesere, Path=SelectedItem}" Name="tb_navn" Margin="0,0,0,0" Height="26" ></TextBox>

if I add ".Content" I get nothing, and in desperation I have tried "Content.XXX" and "Content/XXX" without any success.

Hope someone can point me in the right direction.

Thanks
- Henning M

hmortensen
Light Poster
36 posts since Aug 2009
Reputation Points: 23
Solved Threads: 2
 

Ok, as so many times before. When you keep on trying you find a solution.

First I had to set SelectedItem="{Binding Path=SelectedItem}" in the Listbox. Then in the TextBox I had to set the Path=SelectedItem.navn and everything worked :)

Hope this will help someone else ..

- Henning M.

Hi All,

I'm trying to bind a textbox to a listbox's selected.Content properties. The listbox displays the items as designed in the datatemplate. But I can't get a textbox to display one/any of the selectedItems properties.

DataTemplate

<DataTemplate x:Key="dt_Afloeser" >
            <Border BorderBrush="Black" BorderThickness="0.5" CornerRadius="2" Margin="2,2,2,2">
                <DockPanel HorizontalAlignment="Center">
                    <TextBlock Name="navn" Text="{Binding Path=navn}" Margin="2,2,2,2" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="WrapWithOverflow" />
                    <TextBlock Width="20"/>
                    <TextBlock Name="telefonNr"  Text="{Binding Path=telefonNr}" Margin="2,2,10,2" Width="auto" HorizontalAlignment="Left"  VerticalAlignment="Center" TextWrapping="WrapWithOverflow" />
                </DockPanel>
            </Border>
        </DataTemplate>

The DataContext for the Listbox is set in code.

<ListBox Name="lb_Afloesere" ItemsSource="{Binding}" ItemTemplate="{StaticResource dt_Afloeser}" IsEnabled="True" />

This only shows namespace.Afloeser

<TextBox Text="{Binding ElementName=lb_Afloesere, Path=SelectedItem}" Name="tb_navn" Margin="0,0,0,0" Height="26" ></TextBox>

if I add ".Content" I get nothing, and in desperation I have tried "Content.XXX" and "Content/XXX" without any success.

Hope someone can point me in the right direction.

Thanks - Henning M

hmortensen
Light Poster
36 posts since Aug 2009
Reputation Points: 23
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: