【问题标题】:Highlighting Lines in ListBox with WPF使用 WPF 突出显示 ListBox 中的行
【发布时间】:2022-01-04 20:43:39
【问题描述】:

我有一个可以正常显示文本的 ListBox,但是当出现错误时,只有第一行是反向视频,其余的都是普通文本。我可以正常显示行,但不能从程序中反转视频行。我是使用 Windows 窗体完成的,但我正在升级到 WPF。有人可以帮忙吗?

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:

    我发现了问题所在。在 .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;
            }));
    

    【讨论】:

      猜你喜欢
      • 2014-05-02
      • 2021-07-16
      • 2010-10-22
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 2011-10-01
      • 2023-03-29
      • 2020-08-19
      相关资源
      最近更新 更多