C# XAML停止列表框文本样式在离焦时更改
我对包含列表框的.NET 4 XAML程序有一个问题。 当列表框失去焦点时,文本将变为灰色,而不是设置的白色。背景确实做到了这一点,但我用C# XAML停止列表框文本样式在离焦时更改,c#,xaml,listbox,styles,C#,Xaml,Listbox,Styles,我对包含列表框的.NET 4 XAML程序有一个问题。 当列表框失去焦点时,文本将变为灰色,而不是设置的白色。背景确实做到了这一点,但我用 <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#376807" /> <SolidColorBrush x:Key="{x:Static
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#376807" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#487918" />
</Style.Resources>
<DataTemplate x:Key="SelectedTemplate">
<TextBlock Text="{Binding}" Background="Green" Foreground="White" />
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListBox x:Name="dummyList" ItemContainerStyle="{StaticResource ContainerStyle}" HorizontalContentAlignment="Stretch" >
<ListBoxItem Content="A" />
<ListBoxItem Content="B" />
<ListBoxItem Content="C" />
</ListBox>
<Button Height="31" Width="61" Content="Click"/>
<ListBox Name="listBox2" Height="100" Width="120" HorizontalContentAlignment="Stretch">
<ListBoxItem Content="XX" />
<ListBoxItem Content="YY" />
</ListBox>
</StackPanel>
我尝试了几种解决方法,包括
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="#FFFFFF" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Foreground" Value="#444444" />
</Trigger>
</Style.Triggers>
<DataTemplate x:Key="SelectedTemplate">
<TextBlock Text="{Binding}" Background="Green" Foreground="White" />
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListBox x:Name="dummyList" ItemContainerStyle="{StaticResource ContainerStyle}" HorizontalContentAlignment="Stretch" >
<ListBoxItem Content="A" />
<ListBoxItem Content="B" />
<ListBoxItem Content="C" />
</ListBox>
<Button Height="31" Width="61" Content="Click"/>
<ListBox Name="listBox2" Height="100" Width="120" HorizontalContentAlignment="Stretch">
<ListBoxItem Content="XX" />
<ListBoxItem Content="YY" />
</ListBox>
</StackPanel>
但是没有成功
<DataTemplate x:Key="SelectedTemplate">
<TextBlock Text="{Binding}" Background="Green" Foreground="White" />
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListBox x:Name="dummyList" ItemContainerStyle="{StaticResource ContainerStyle}" HorizontalContentAlignment="Stretch" >
<ListBoxItem Content="A" />
<ListBoxItem Content="B" />
<ListBoxItem Content="C" />
</ListBox>
<Button Height="31" Width="61" Content="Click"/>
<ListBox Name="listBox2" Height="100" Width="120" HorizontalContentAlignment="Stretch">
<ListBoxItem Content="XX" />
<ListBoxItem Content="YY" />
</ListBox>
</StackPanel>
好的,我已经编辑了我的代码。只需检查这是否满足您的需要,如果不满足,请回复。
<DataTemplate x:Key="SelectedTemplate">
<TextBlock Text="{Binding}" Background="Green" Foreground="White" />
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListBox x:Name="dummyList" ItemContainerStyle="{StaticResource ContainerStyle}" HorizontalContentAlignment="Stretch" >
<ListBoxItem Content="A" />
<ListBoxItem Content="B" />
<ListBoxItem Content="C" />
</ListBox>
<Button Height="31" Width="61" Content="Click"/>
<ListBox Name="listBox2" Height="100" Width="120" HorizontalContentAlignment="Stretch">
<ListBoxItem Content="XX" />
<ListBoxItem Content="YY" />
</ListBox>
</StackPanel>
以下内容已经在listbox中使用了一些虚拟数据进行了尝试,并且有效。我希望这就是你想要的,如果不是,那么请进一步澄清
<DataTemplate x:Key="SelectedTemplate">
<TextBlock Text="{Binding}" Background="Green" Foreground="White" />
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListBox x:Name="dummyList" ItemContainerStyle="{StaticResource ContainerStyle}" HorizontalContentAlignment="Stretch" >
<ListBoxItem Content="A" />
<ListBoxItem Content="B" />
<ListBoxItem Content="C" />
</ListBox>
<Button Height="31" Width="61" Content="Click"/>
<ListBox Name="listBox2" Height="100" Width="120" HorizontalContentAlignment="Stretch">
<ListBoxItem Content="XX" />
<ListBoxItem Content="YY" />
</ListBox>
</StackPanel>
<DataTemplate x:Key="SelectedTemplate">
<TextBlock Text="{Binding}" Background="Green" Foreground="White" />
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListBox x:Name="dummyList" ItemContainerStyle="{StaticResource ContainerStyle}" HorizontalContentAlignment="Stretch" >
<ListBoxItem Content="A" />
<ListBoxItem Content="B" />
<ListBoxItem Content="C" />
</ListBox>
<Button Height="31" Width="61" Content="Click"/>
<ListBox Name="listBox2" Height="100" Width="120" HorizontalContentAlignment="Stretch">
<ListBoxItem Content="XX" />
<ListBoxItem Content="YY" />
</ListBox>
</StackPanel>
您尝试过修改默认控件模板吗?@Gabe我没有,因为我的理解是这会更改所有列表框。我只想将此应用于列表框的此实例。您好,您是否尝试使用expression blend?如果你有,我可以帮助你快速解决这个问题:)@AllanChua说尝试使用expression blend来设计你的控件,它简单有效,你可以创建样式并更改其属性以满足你的需要。我很乐意使用任何方法,我已经安装了expression blend,但是,不要真的使用它。这是控件有焦点时的外观。。。这是它不在时的样子。点击窗外@Eddie,为什么不工作?让我知道是什么问题,我会很乐意帮忙的。