提供反饋我試圖創建一個列表框,顯示的頁面內容的縮略圖視圖,用於應用程序與一個畫布,但多個「頁」。是的,這可能不是最好的開始,但由於歷史原因,這是我的。 我已經實現了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>
我無法獲得代碼顯示,不允許將它添加到註釋中:( – Hoverfrog