2009-11-06 80 views
1

我有一個窗口,其中兩個ListBox都綁定到XMLDataProviderListbox1SelectedItem屬性與ListBox2SelectedItem屬性雙向綁定。到現在爲止還挺好。WPF中列表框的奇怪行爲

ListBox2包含StyleTrigger當鼠標懸停在項目上時,將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> 
+0

+1提供一個完整的可運行的例子和清晰的解釋,很抱歉不能幫你,但.. :) – Oskar 2009-11-06 07:44:30

回答

3

該問題是由雙向綁定引起的。當您選擇ListBox 1中的項目時,它將SelectedItem屬性設置爲ListBox 2.這將「覆蓋」設置爲ListBox2.SelectedItemBinding。如果你願意,你可以在Snoop進行驗證。

至於如何實現您的目標,您應該使用集合視圖和IsSynchronizedWithCurrentItem屬性。兩個ListBox es應綁定到相同的集合視圖並與當前項目同步。因此,選擇一個ListBox中的項目將強制其他ListBox與所選項目同步。

下面是你需要做到這一點的最低XAML:

<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"> 
    <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> 

     <CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource TestData}, XPath=Book}"/> 
    </Window.Resources> 
    <Grid> 
     <StackPanel Orientation="Horizontal"> 
      <StackPanel> 
       <Label HorizontalContentAlignment="Center">Listbox 1</Label> 
       <ListBox x:Name="box1" ItemsSource="Source={StaticResource cvs}}" IsSynchronizedWithCurrentItem="True"> 
        <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 cvs}}" IsSynchronizedWithCurrentItem="True"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <Label Content="{Binding XPath=Title}"/> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </StackPanel> 
     </StackPanel> 
    </Grid> 
</Window> 
+0

感謝您的建議。不幸的是,問題依然存在。只要我在Listbox1中選擇一個項目,當IsMouseOver觸發器觸發時,它將不再在ListBox2中選擇。 – 2009-11-06 13:09:39

+0

你複製了上面的代碼,它不起作用?適合我的作品。 – 2009-11-06 14:01:45

+0

您的代碼可以正常工作,但導致問題的Listbox2的樣式觸發器在示例中缺失。 – 2009-11-06 15:04:35