亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更改所選ListBox項的背景顏色

更改所選ListBox項的背景顏色

一只名叫tom的貓 2019-11-29 11:05:22
到目前為止,這是我的XAML。    <ScrollViewer Grid.Column="1" Grid.RowSpan="2">        <ListBox   Background="Black" ItemsSource="{Binding Path=ActiveLog}" >            <ListBox.ItemTemplate>                <DataTemplate>                    <Grid Background="Black">                        <Grid.ColumnDefinitions>                            <ColumnDefinition Width="200"></ColumnDefinition>                            <ColumnDefinition Width="*"></ColumnDefinition>                        </Grid.ColumnDefinitions>                        <Grid.RowDefinitions>                            <RowDefinition></RowDefinition>                            <RowDefinition></RowDefinition>                        </Grid.RowDefinitions>                        <TextBlock Grid.Column="0" Grid.Row="0" Foreground="White">                            <TextBlock >Date:</TextBlock>                            <TextBlock  Text="{Binding Path=LogDate}"/>                        </TextBlock>                        <TextBlock Grid.Column="1" Grid.Row="0" Foreground="White">                            <TextBlock >Severity:</TextBlock>                            <TextBlock  Text="{Binding Path=Severity}"/>                        </TextBlock>                        <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Foreground="LightGray" Text="{Binding Path=Message}"></TextBlock>                    </Grid>                </DataTemplate>            </ListBox.ItemTemplate>            <ListBox.Template>                <ControlTemplate>                    <StackPanel Background="Black" IsItemsHost="True" >                    </StackPanel>                </ControlTemplate>            </ListBox.Template>        </ListBox>    </ScrollViewer>唯一的問題是所選項目的右側有一個藍色框。我認為可以更改選擇的顏色,但是找不到。
查看完整描述

3 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

您需要使用ListBox.ItemContainerStyle。


ListBox.ItemTemplate指定應如何顯示項目的內容。但是WPF仍將每個項目包裝在ListBoxItem控件中,默認情況下,如果選中它,則將其Background設置為系統突出顯示顏色。您不能停止WPF創建ListBoxItem控件,但是可以為它們設置樣式(在您的情況下,將Background設置為始終為Transparent或Black或其他顏色),并使用ItemContainerStyle。


juFo的答案顯示了一種可能的實現方式,即在項目樣式的上下文內“劫持”系統背景畫筆資源;另一種可能更慣用的技術是對SetterBackground屬性使用。


查看完整回答
反對 回復 2019-11-29
?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

<UserControl.Resources>

    <Style x:Key="myLBStyle" TargetType="{x:Type ListBoxItem}">

        <Style.Resources>

            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"

                             Color="Transparent"/>

        </Style.Resources>

    </Style>

</UserControl.Resources> 


<ListBox ItemsSource="{Binding Path=FirstNames}"

         ItemContainerStyle="{StaticResource myLBStyle}">  

您只需覆蓋listboxitem的樣式(請參閱:TargetType是ListBoxItem)


查看完整回答
反對 回復 2019-11-29
  • 3 回答
  • 0 關注
  • 1204 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號