Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is caused by the two-way binding. When you select an item in <code>ListBox</code> 1, it sets the <code>SelectedItem</code> property on <code>ListBox</code> 2. This "overwrites" the <code>Binding</code> set on <code>ListBox2.SelectedItem</code>. If you want, you can verify in <a href="http://blois.us/Snoop/" rel="nofollow noreferrer">Snoop</a>.</p> <p>As for how to achieve your goal, you should use a collection view and the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.issynchronizedwithcurrentitem.aspx" rel="nofollow noreferrer"><code>IsSynchronizedWithCurrentItem</code></a> property. Both <code>ListBox</code>es should bind to the same collection view and be synchronized with the current item. As a result, selecting an item in one <code>ListBox</code> will force the other <code>ListBox</code> to synchronize with that selected item.</p> <p>Here's the minimum XAML you need to make this happen:</p> <pre><code>&lt;Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"&gt; &lt;Window.Resources&gt; &lt;Style TargetType="ListBox"&gt; &lt;Style.Setters&gt; &lt;Setter Property="Width" Value="150"/&gt; &lt;Setter Property="Margin" Value="4"/&gt; &lt;/Style.Setters&gt; &lt;/Style&gt; &lt;!-- define the XML data source as a resource --&gt; &lt;XmlDataProvider x:Key="TestData" XPath="/Books"&gt; &lt;x:XData&gt; &lt;Books xmlns=""&gt; &lt;Book&gt; &lt;Title&gt;Book 1&lt;/Title&gt; &lt;Author&gt;Mister 1&lt;/Author&gt; &lt;/Book&gt; &lt;Book&gt; &lt;Title&gt;Book 2&lt;/Title&gt; &lt;Author&gt;Mister 2&lt;/Author&gt; &lt;/Book&gt; &lt;Book&gt; &lt;Title&gt;Book 3&lt;/Title&gt; &lt;Author&gt;Mister 3&lt;/Author&gt; &lt;/Book&gt; &lt;Book&gt; &lt;Title&gt;Book 4&lt;/Title&gt; &lt;Author&gt;Mister 4&lt;/Author&gt; &lt;/Book&gt; &lt;Book&gt; &lt;Title&gt;Book 5&lt;/Title&gt; &lt;Author&gt;Mister 5&lt;/Author&gt; &lt;/Book&gt; &lt;Book&gt; &lt;Title&gt;Book 6&lt;/Title&gt; &lt;Author&gt;Mister 6&lt;/Author&gt; &lt;/Book&gt; &lt;/Books&gt; &lt;/x:XData&gt; &lt;/XmlDataProvider&gt; &lt;CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource TestData}, XPath=Book}"/&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;StackPanel&gt; &lt;Label HorizontalContentAlignment="Center"&gt;Listbox 1&lt;/Label&gt; &lt;ListBox x:Name="box1" ItemsSource="Source={StaticResource cvs}}" IsSynchronizedWithCurrentItem="True"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Label Content="{Binding XPath=Title}"/&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/StackPanel&gt; &lt;StackPanel&gt; &lt;Label HorizontalContentAlignment="Center"&gt;Listbox 2&lt;/Label&gt; &lt;ListBox x:Name="box2" ItemsSource="{Binding Source={StaticResource cvs}}" IsSynchronizedWithCurrentItem="True"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Label Content="{Binding XPath=Title}"/&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload