下面的xaml將不起作用,因爲對於ListBoxItem,BacckGround表示TextBlock下的顏色。如何在鼠標懸停時更改WPF TextBlock ListBoxItem的背景顏色?
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource Theme.Button.Background.Hover}" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"
Background="{DynamicResource Theme.Button.Background}"
Foreground="{DynamicResource Theme.Button.Foreground}"
Padding="{DynamicResource Theme.Button.Padding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
對於請求我在這裏添加我的實際代碼(SelectButton可以在這裏找到https://gist.github.com/loraderon/580405):
<cntrls:SelectButton x:Name="Insert_BtnStartPolyline" Grid.Row="3" ItemsSource="{Binding Path=InsertLineItemsSource}" Command="{ui:CommandHandler OpenVersion}" HorizontalAlignment="Left" MinWidth="100">
<cntrls:SelectButton.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</cntrls:SelectButton.ItemContainerStyle>
<cntrls:SelectButton.Resources>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Chartreuse" />
</Trigger>
</Style.Triggers>
</Style>
</cntrls:SelectButton.Resources>
<cntrls:SelectButton.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"
Background="{DynamicResource Theme.Button.Background}"
Foreground="{DynamicResource Theme.Button.Foreground}"
Padding="{DynamicResource Theme.Button.Padding}"
HorizontalAlignment="Stretch"/>
</DataTemplate>
</cntrls:SelectButton.ItemTemplate>
</cntrls:SelectButton>
什麼TextBlock的... –
爲什麼不給TextBlock的透明背景,並設置在ListBoxItem的樣式默認的背景是什麼?或者將鼠標懸停觸發器置於TextBlock中的樣式上? –
@ H.B。 :我更新了問題 –