2013-01-20 29 views
1

提供反饋我試圖創建一個列表框,顯示的頁面內容的縮略圖視圖,用於應用程序與一個畫布,但多個「頁」。是的,這可能不是最好的開始,但由於歷史原因,這是我的。 我已經實現了ListBox與數據綁定到一個具有PageData的ObservableCollection(顯示在頁面上的所有內容,包括它的背景)的單例WorkBook。 我真的很想要的是能夠更改ListBoxItem的邊框顏色,當它被選中時,保持邊框顏色,而內容是承載集合的類中當前選定的項目。WPF列表框的選擇問題,用戶

我的問題是: - 1 /我不能讓列表框,選擇在程序啓動的第一個項目。

2當列表框失去焦點的SelectedIndex /始終是-1(所以沒有選擇)

3 /添加到列表框的結果中沒有選擇(的SelectedIndex == -1)

4 /使用觸發器,我可以設置selectedItem的邊框,但是當ListBox失去焦點時會丟失。由於ListBoxItem中顯示的圖像是不透明使得「標準」的方式選擇顏色留在失焦不工作 - 即

<Style x:Key="PsThumb"> 
       <Style.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="White"></SolidColorBrush> 
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="White"></SolidColorBrush> 
       </Style.Resources> 
</Style> 

我的列表框代碼如下: -

<ListBox x:Name="PageSorter" Style="{StaticResource PsThumb}" Width="148" BorderThickness="4" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
     VerticalAlignment="Stretch" ItemsSource="{Binding Pages,Source={StaticResource WorkBook}}" SelectedItem="{Binding Path=CurrentPageData, Mode=TwoWay}" 
     AllowDrop="True" ScrollViewer.VerticalScrollBarVisibility="Auto"> 
       <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Border x:Name="border" BorderBrush="DarkGray" BorderThickness="4" Margin="2,4,2,4" CornerRadius="5"> 
          <Border.Effect> 
           <DropShadowEffect ShadowDepth="6"/> 
          </Border.Effect> 
          <Image Source="{Binding Thumbnail}" Width="130" Height="95" Stretch="Fill"/> 
         </Border> 
        </Grid> 
         <DataTemplate.Triggers> 
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True"> 
           <Setter TargetName="border" Property="BorderBrush" Value="Red"></Setter> 
          </DataTrigger> 
         </DataTemplate.Triggers> 
        </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
+0

我無法獲得代碼顯示,不允許將它添加到註釋中:( – Hoverfrog

回答

0

達到你的要求,最簡單的方法是將DataTrigger綁定到ListBox的裏面的物品的屬性。在下面的例子中,我加入我的ContactSelected屬性:

public class Contact : INPCBase 
{ 
    public string Name { get; set; } 

    private bool _Selected; 
    public bool Selected 
    { 
     get { return _Selected; } 
     set 
     { 
      _Selected = value; 
      NotifyPropertyChanged("Selected"); 
     } 
    } 
} 

我然後綁定我ListBoxList<Contact>。當用戶檢查CheckBox,我們改變了Selected屬性,進而引發Style

<Window x:Class="WpfApp.ListboxKeepSelectionWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="ListboxKeepSelectionWindow" Height="277" Width="343" 
    xmlns:me="clr-namespace:WpfApp"> 

<Window.Resources> 
    <me:ContactList x:Key="sample"/> 

    <Style TargetType="ListBoxItem" x:Key="SelectedListBoxItemStyle"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Path=Selected}" Value="True"> 
       <Setter Property="BorderBrush" Value="Orange"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 

<Grid> 
    <ListBox Name="lbxContacts" 
      ItemsSource="{StaticResource ResourceKey=sample}" 
      SelectionMode="Extended" 
      ItemContainerStyle="{StaticResource ResourceKey=SelectedListBoxItemStyle}" 
      SelectionChanged="lbxContacts_SelectionChanged"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <CheckBox IsChecked="{Binding Path=Selected}"> 
         <TextBlock Text="{Binding Path=Name}"/> 
        </CheckBox> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

爲了讓用戶來選擇項目,而無需使用複選框,我加了這個小事件上ListBox 。由於Contact類實現了INotifyPropertyChanged接口,該CheckBox的值更新,使用戶知道工作的選擇:

private void lbxContacts_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     foreach (Contact c in e.AddedItems) 
     { 
      c.Selected = !c.Selected; 
     } 
    } 

當你想選擇的項目列表,你只需要使用的LINQ查詢ItemsSource獲得項目Selected == true

+0

設置ItemContainerStyle無效 - 但更改了DataTemplate觸發器!我使用了Binding Path = Selected而不是Binding = 「{Binding RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type ListBoxItem}},Path = IsSelected}」非常感謝! – Hoverfrog