【发布时间】:2022-01-04 20:43:39
【问题描述】:
我有一个可以正常显示文本的 ListBox,但是当出现错误时,只有第一行是反向视频,其余的都是普通文本。我可以正常显示行,但不能从程序中反转视频行。我是使用 Windows 窗体完成的,但我正在升级到 WPF。有人可以帮忙吗?
【问题讨论】:
我有一个可以正常显示文本的 ListBox,但是当出现错误时,只有第一行是反向视频,其余的都是普通文本。我可以正常显示行,但不能从程序中反转视频行。我是使用 Windows 窗体完成的,但我正在升级到 WPF。有人可以帮忙吗?
【问题讨论】:
我发现了问题所在。在 .xaml 文件中:
<Window.Resources>
<Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="_Border"
Padding="2"
SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="_Border" Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
在列表框上:
<ListBox x:Name="TestList" HorizontalAlignment="Left" Height="528" Margin="25,24,0,0" VerticalAlignment="Top" Width="1351" Background="#FFDDDDDD" ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"/>
在.cs代码中:
Application.Current.Dispatcher.Invoke(new Action(() =>
{
MainWindow mv = (MainWindow)Application.Current.MainWindow;
_ = mv.TestList.Items.Add(str);
mv.TestList.SelectedIndex = 0;
}));
【讨论】: