我有一個窗口,其中兩個ListBox
都綁定到XMLDataProvider
。 Listbox1
的SelectedItem
屬性與ListBox2
的SelectedItem
屬性雙向綁定。到現在爲止還挺好。WPF中列表框的奇怪行爲
ListBox2
包含Style
Trigger
當鼠標懸停在項目上時,將IsSelected
設置爲true。由於雙向綁定,所以也選擇了ListBox1
中的相應項目。當我通過點擊它
例如選擇Listbox1
一個項目出現問題時,我選擇「圖書1」 ListBox1
然後將鼠標移動到所有項目中ListBox2
項目「第1冊」將不再被選中時風格觸發器觸發。只要在Listbox1
中選擇一個項目,我就不能再通過將鼠標移動到Listbox2
上來選擇相應的項目。但是,通過鼠標點擊選擇仍然有效。
有人可以解釋行爲和/或提供解決方案嗎?
<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">
<Window.Resources>
<Style TargetType="ListBox">
<Style.Setters>
<Setter Property="Width" Value="150"/>
<Setter Property="Margin" Value="4"/>
</Style.Setters>
</Style>
<!-- define the XML data source as a resource -->
<XmlDataProvider x:Key="TestData" XPath="/Books">
<x:XData>
<Books xmlns="">
<Book>
<Title>Book 1</Title>
<Author>Mister 1</Author>
</Book>
<Book>
<Title>Book 2</Title>
<Author>Mister 2</Author>
</Book>
<Book>
<Title>Book 3</Title>
<Author>Mister 3</Author>
</Book>
<Book>
<Title>Book 4</Title>
<Author>Mister 4</Author>
</Book>
<Book>
<Title>Book 5</Title>
<Author>Mister 5</Author>
</Book>
<Book>
<Title>Book 6</Title>
<Author>Mister 6</Author>
</Book>
</Books>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<StackPanel>
<Label HorizontalContentAlignment="Center">Listbox 1</Label>
<ListBox x:Name="box1" ItemsSource="{Binding Source={StaticResource TestData}, XPath=Book}"
SelectedItem="{Binding ElementName=box2, Path=SelectedItem, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding XPath=Title}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
<StackPanel>
<Label HorizontalContentAlignment="Center">Listbox 2</Label>
<ListBox x:Name="box2" ItemsSource="{Binding Source={StaticResource TestData}, XPath=Book}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding XPath=Title}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Padding" Value="12"/>
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</StackPanel>
</StackPanel>
</Grid>
</Window>
+1提供一個完整的可運行的例子和清晰的解釋,很抱歉不能幫你,但.. :) – Oskar 2009-11-06 07:44:30