Как изменить цвет listview wpf

от admin

Программное изменение цвета фона строки ListView (wpf)

У меня есть класс, который заполняет ListView, передавая список объектов. Класс использует отражение, чтобы увидеть свойства каждого объекта, чтобы сгенерировать ListView. Как я мог изменить цвет фона строки в ListView.

Эта страница выполняет именно то, что я ищу. Единственная проблема в том, что мой ListView привязан к списку объектов. Другими словами, каждый элемент ListView — это привязанный объект, а не ListViewItem. Я предполагаю, что это причина, по которой я не могу преобразовать какой-либо элемент из ListView в ListViewItem. Например, когда я делаю это:

Я получаю InvalidcastException, потому что если я физически добавляю объекты в ListView, например:

Listview.items.add (someObject), тогда это будет работать, но поскольку я привязываю список к ListView, эта строка не работает. Думаю, именно поэтому я не умею кастовать. Причина, по которой я хочу использовать это, заключается в том, что ListViewItem имеет свойство Background.

Я могу сделать это с первыми 12 объектами, которые я пробовал следующим образом:

Change background color of ListView row programmatically (wpf)

I have a class that populates a ListView by passing a list of objects. The class uses reflection to see the properties of each object in order to generate the ListView. How could I change the background color of a row in the ListView.

This page does exactly what I am looking for. The only problem is that my ListView is bound to the list of objects. In other words each item of the ListView is an object that is bound instead of a ListViewItem. I am assuming that is the reason why I cannot cast some item in the ListView to a ListViewItem. For example when I do this:

I get an InvalidcastException because if I where to physically add the objects to the ListView like:

listview.items.add(someObject) then this will work, but because I am binding the list to the ListView that line does not work. I think that is the reason why I am not able to cast. The reason why I want to cast it is becasue a ListViewItem has a Background property.

I am able to do that with the first 12 objects I have tried the folowing:

Изменить цвет фона строки ListView программно (wpf)

У меня есть класс, который заполняет ListView, передавая список объектов. Класс использует отражение, чтобы увидеть свойства каждого объекта, чтобы сгенерировать ListView. Как изменить цвет фона строки в ListView.

Читать:
Какой программой записать музыку на cd диск

Эта страница делает именно то, что я ищу. Единственная проблема заключается в том, что мой ListView привязан к списку объектов. Другими словами, каждый элемент ListView является объектом, который привязан вместо ListViewItem. Я предполагаю, что именно поэтому я не могу использовать какой-либо элемент в ListView для ListViewItem. Например, когда я это делаю:

Я получаю InvalidcastException, потому что если я физически добавляю объекты в ListView, например:

How to set a specific row color of a WPF ListView/GridView control?

Continuing the series of blog posts on WPF ListView/GridView control, today we will learn how to set the background color (more precisely, ‘the Style’) of a WPF ListView/GridView control’s row based on some specific parameter passed as the data.

You may want to give your user a proper visual by providing a different style to that specific row. Let’s discuss with a simple example.

There could be many instances when we need to color a particular row of a ListView/GridView control in our WPF application. For example, if a data model for a particular row has errors, we may need to highlight the entire row with ‘Red’ as the background color. For the other rows, we may want to color it to ‘Green’ or keep it as-is.

The below code demonstrates how to use a DataTrigger to set the background color of a WPF ListView/GridView control based on the value of the associated data model. Here you can see how we are setting the background color to red and green based on the property value of ‘HasErrors’ part of the associated data model:

You can create as many DataTrigger‘s as you want but make sure that, you have unique property value with which you are comparing the value to trigger the UI change. Was the post helpful? Do let me know your feedback. Also, don’t forget to read my entire series on WPF ListView/GridView control and other posts. Cheers! Have a great day ahead.

Похожие статьи