Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange behaviour of Listboxes in WPF
    primarykey
    data
    text
    <p>I have a window with two <code>ListBox</code>es both bound to an <code>XMLDataProvider</code>. The <code>SelectedItem</code> property of <code>Listbox1</code> is two-way-bound to the <code>SelectedItem</code> property of <code>ListBox2</code>. So far so good.</p> <p><code>ListBox2</code> contains a <code>Style</code> <code>Trigger</code> which sets <code>IsSelected</code> to true when the mouse hovers over an item. The corresponding item in <code>ListBox1</code> is selected too because of the two-way binding. The problem arises when I select an item in <code>Listbox1</code> by clicking on it</p> <p>For example when I select "Book 1" in <code>ListBox1</code> then move the mouse over all items in <code>ListBox2</code> the item "Book 1" will no longer be selected when the style trigger fires. As soon as I select an item in <code>Listbox1</code> I can no longer select the corresponding item in <code>Listbox2</code> by moving the mouse over it. However, selecting by mouseclick still works.</p> <p>Can someone explain the behaviour and/or provide a solution?</p> <pre><code>&lt;Window x:Class="Test.sample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight"&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;/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="{Binding Source={StaticResource TestData}, XPath=Book}" SelectedItem="{Binding ElementName=box2, Path=SelectedItem, Mode=TwoWay}"&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 TestData}, XPath=Book}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Label Content="{Binding XPath=Title}"/&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;ListBox.ItemContainerStyle&gt; &lt;Style TargetType="ListBoxItem"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsMouseOver" Value="True"&gt; &lt;Setter Property="Padding" Value="12"/&gt; &lt;Setter Property="IsSelected" Value="True"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/ListBox.ItemContainerStyle&gt; &lt;/ListBox&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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