通过代码在 WPF 中设置 GridView 的颜色

发布于 2024-10-28 04:54:24 字数 924 浏览 1 评论 0原文

我已经搜索了很长时间如何做到这一点。我不知道如何使用代码设置 GridView (WPF) 中行项目的颜色 我不需要 XAML 中的示例

我的数据来自我加载&分解 XML 文件。然后,我将其放入一个具有属性的小类中,并将这些属性绑定到列以填充数据。稍后,它可以对表的数据启动一些功能。因为这有时需要超过 10 分钟,所以它是在一个线程上进行的。我想根据结果对已完成解析的行进行着色。 (IE:红色表示 HTTP 错误,橙色表示 XML 解析错误等...)

这是 GridView 的 XAML:

<ListView Name="RepoListView" Margin="0,22,0,0">
  <ListView.View>
    <GridView AllowsColumnReorder="True">
      <GridViewColumn Header="Title" DisplayMemberBinding="{Binding title}" />
      <GridViewColumn Header="URL" DisplayMemberBinding="{Binding url}" />
      <GridViewColumn Header="Subscriptions" DisplayMemberBinding="{Binding dlc}" />
    </GridView>
  </ListView.View>
</ListView>

然后我使用 url 列解析数据。我目前正在更改所选项目。我想知道我是否可以做些什么来更改特定项目。假设我在第 500 行:

我使用:setSelected(i),它使用 Dispatcher 来安全地更改所选行。我还能做些什么来改变颜色吗?我已经尝试了几个小时来寻找解决方案。

谢谢

I've been searching for quite a long time how to do this. I don't know how to set the colour of a row item in the GridView (WPF) using code I don't want an example in XAML

My data comes from me loading & pulling apart an XML file. I then put it into a small class with properties and those are bound to the column to populate the data. Later, it can begin some functions on the table's data. Because this takes at some times over 10 minutes it's on a thread & I would like to colour the rows I've completed parsing depending on the result. (IE: Red for HTTP error, Orange for XML parsing error ect...)

This is the GridView's XAML:

<ListView Name="RepoListView" Margin="0,22,0,0">
  <ListView.View>
    <GridView AllowsColumnReorder="True">
      <GridViewColumn Header="Title" DisplayMemberBinding="{Binding title}" />
      <GridViewColumn Header="URL" DisplayMemberBinding="{Binding url}" />
      <GridViewColumn Header="Subscriptions" DisplayMemberBinding="{Binding dlc}" />
    </GridView>
  </ListView.View>
</ListView>

I am then parsing the data using the url column. I am currently changing the selected item as I go through. I want to know if theres something I can do to change the specific item. Let's assume I'm on row 500:

I use: setSelected(i) which uses Dispatcher to safely change the selected row. Is there anything I can do to change the colour as well? I've been trying for hours to find a solution.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2024-11-04 04:54:24

我可能会采用绑定方法,后面的代码很丑陋。创建一个带有错误枚举的属性,并创建一个 DataTemplate 或 Style,在其中将背景绑定到所述属性。

编辑:示例...

在您的类中:

public enum ParsingStatus { None, Completed, XmlError, HtmlError }

private ParsingStatus _status = ParsingStatus.None;
public ParsingStatus Status
{
    get { return _status; }
    set
    {
        if (_status != value)
        {
            _status = value;
            NotifyPropertyChanged("Status");
        }
    }
}

然后您可以使用转换器绑定到背景属性,如下所示:

public class ErrorToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Employee.ParsingStatus input = (Employee.ParsingStatus)value;
        switch (input)
        {
            case Employee.ParsingStatus.None:
                return null;
            case Employee.ParsingStatus.Completed:
                return Brushes.LightGreen;
            case Employee.ParsingStatus.XmlError:
                return Brushes.Pink;
            case Employee.ParsingStatus.HtmlError:
                return Brushes.Yellow;
            default:
                return null;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

以及相应的 XAML:

<Window.Resources>
    <local:ErrorToBrushConverter x:Key="ErrorToBrushConv"/>
    ...
</Window.Resources>
    ...
        <ListView ItemsSource="{Binding Data}">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Background"
                            Value="{Binding Status, Converter={StaticResource ErrorToBrushConv}}"/>
                </Style>
            </ListView.ItemContainerStyle>
            ...
        </ListView>

或者您仅指定样式并在状态上进行切换使用触发器:(

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Status}" Value="XmlError">
                <Setter Property="Background" Value="Pink"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Status}" Value="HtmlError">
                <Setter Property="Background" Value="#FFFFFF66"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Status}" Value="Completed">
                <Setter Property="Background" Value="LightGreen"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>

罗伯特·罗斯尼(Robert Rossney)建议使用触发器,正如你所看到的,它很短并且根本不需要任何代码,我真的想不出这有什么缺点,我不知道为什么我不这样做它首先是这样的...)

转换器看起来像这样(触发方法的黄色更合适):<​​br>
多彩 ListView

I would probably go for a binding approach, code behind is ugly. Create a property with an error enum and create a DataTemplate or Style in which you bind the background to said property.

Edit: Example...

In your class:

public enum ParsingStatus { None, Completed, XmlError, HtmlError }

private ParsingStatus _status = ParsingStatus.None;
public ParsingStatus Status
{
    get { return _status; }
    set
    {
        if (_status != value)
        {
            _status = value;
            NotifyPropertyChanged("Status");
        }
    }
}

Then you can either bind to the background property using a converter like this:

public class ErrorToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Employee.ParsingStatus input = (Employee.ParsingStatus)value;
        switch (input)
        {
            case Employee.ParsingStatus.None:
                return null;
            case Employee.ParsingStatus.Completed:
                return Brushes.LightGreen;
            case Employee.ParsingStatus.XmlError:
                return Brushes.Pink;
            case Employee.ParsingStatus.HtmlError:
                return Brushes.Yellow;
            default:
                return null;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

And the respective XAML:

<Window.Resources>
    <local:ErrorToBrushConverter x:Key="ErrorToBrushConv"/>
    ...
</Window.Resources>
    ...
        <ListView ItemsSource="{Binding Data}">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Background"
                            Value="{Binding Status, Converter={StaticResource ErrorToBrushConv}}"/>
                </Style>
            </ListView.ItemContainerStyle>
            ...
        </ListView>

Or you only specify a Style and do the switch on the status using triggers:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Status}" Value="XmlError">
                <Setter Property="Background" Value="Pink"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Status}" Value="HtmlError">
                <Setter Property="Background" Value="#FFFFFF66"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Status}" Value="Completed">
                <Setter Property="Background" Value="LightGreen"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>

(Using triggers was suggested by Robert Rossney, as you can see it is quite short and requires no code behind at all, i cannot really think of any downsides to this and i have no idea why i did not do it like this in the first place...)

Which looks like this with the converter (the yellow of the trigger-method is more suitable):
A colourful ListView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文