通过代码在 WPF 中设置 GridView 的颜色
我已经搜索了很长时间如何做到这一点。我不知道如何使用代码设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能会采用绑定方法,后面的代码很丑陋。创建一个带有错误枚举的属性,并创建一个 DataTemplate 或 Style,在其中将背景绑定到所述属性。
编辑:示例...
在您的类中:
然后您可以使用转换器绑定到背景属性,如下所示:
以及相应的 XAML:
或者您仅指定样式并在状态上进行切换使用触发器:(
罗伯特·罗斯尼(Robert Rossney)建议使用触发器,正如你所看到的,它很短并且根本不需要任何代码,我真的想不出这有什么缺点,我不知道为什么我不这样做它首先是这样的...)
转换器看起来像这样(触发方法的黄色更合适):<br>
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:
Then you can either bind to the background property using a converter like this:
And the respective XAML:
Or you only specify a Style and do the switch on the status using triggers:
(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):