RomanKovalev
RomanKovalev

Reputation: 859

How to set the dependency value

I have some control like

 <ListBox ItemsSource="{Binding tests, UpdateSourceTrigger=PropertyChanged}"
                                     ItemTemplate="{StaticResource TestTemplate}" />

When user cliked on some element of the ListBox I needed to get data element of this ListBoxItem element in other control and propagate it with data template of other element. How do it properly? Example:

Source:

 <ListBox ItemsSource="{Binding tests, UpdateSourceTrigger=PropertyChanged}"
                                     ItemTemplate="{StaticResource TestTemplate}" />

Target:

<TextBlock Text="{Binding Path=name}" />

Where Text in TextBox bind on same data element of selected ListBox item

UPDATE: How make some control whose content bind to SelectedItem and described by static data template like this:

<DataTemplate x:Key="TestTemplate">
        <TextBlock Text="{Binding Path=name}"/>
</DataTemplate>

Resolve with:

 <ContentPresenter
                    HorizontalAlignment="Stretch"
                    Content="{Binding ElementName=tests_flat, Path=SelectedItem}"
                                  ContentTemplate="{StaticResource TestInfoTemplate}">
                </ContentPresenter>

Upvotes: 0

Views: 60

Answers (2)

Jason Ridge
Jason Ridge

Reputation: 1868

Source

<ListBox x:Name="myListBox" ItemsSource="{Binding tests, UpdateSourceTrigger=PropertyChanged}"
                                 ItemTemplate="{StaticResource TestTemplate}" />

Target

<TextBlock Text="{Binding ElementName=myListBox path=SelectedItem}" />

Upvotes: 1

Clemens
Clemens

Reputation: 128097

You could either bind directly to the SelectedItem property (if your items are strings) or set the SelectedValuePath of your ListBox and bind to the SelectedValue property.

Upvotes: 1

Related Questions