WPF:我如何编程风格我的ListView,使所有行具有某种背景颜色和高度? [英] WPF: How do I programmatically style my ListView so that all rows have a certain background color and height?

查看:2226
本文介绍了WPF:我如何编程风格我的ListView,使所有行具有某种背景颜色和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法环绕的东西我的头。说我有以下逻辑在我的code:

I can't seem to wrap my head around something. Say I have the following logic in my code:

namespace WPFTesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        ObservableCollection<Message> messages = new ObservableCollection<Message>();

        public MainWindow()
        {
            InitializeComponent();
            messages.Add(new Message(DateTime.Now, "This is a test."));
            ListView listView = new ListView();
            GridView gridView = new GridView();
            listView.View = gridView;
            GridViewColumn timeStampColumn = new GridViewColumn();
            timeStampColumn.DisplayMemberBinding = new Binding("Date");
            GridViewColumnHeader timeStampHeader = new GridViewColumnHeader();
            timeStampHeader.Content = "Time";
            timeStampColumn.Header = timeStampHeader;
            gridView.Columns.Add(timeStampColumn);
            GridViewColumn messageColumn = new GridViewColumn();
            messageColumn.DisplayMemberBinding = new Binding("Text");
            GridViewColumnHeader messageHeader = new GridViewColumnHeader();
            messageHeader.Content = "Message";
            messageColumn.Header = messageHeader;
            gridView.Columns.Add(messageColumn);
            Binding binding = new Binding();
            binding.Source = messages;
            listView.SetBinding(ItemsControl.ItemsSourceProperty, binding);
            MainGrid.Children.Add(listView);
        }

        public class Message
        {

            public Message(DateTime aDate, String aText)
            {
                Date = aDate;
                Text = aText;
            }

            public DateTime Date { get; set; }
            public String Text { get; set; }
        }
    }
}

如何编程风格我的ListView,使所有行具有某种背景颜色和高度?请注意,我要避免做ListView的项目列表中的foreach循环,并设置每个ListViewItem的属性,因为这个名单可能有很多项目,这可能是昂贵的。取而代之的,是不是有一些方法来做到这一点编程方式使用Style类,或者一些运行时的逻辑是什么?

How do I programmatically style my ListView so that all rows have a certain background color and height? Note, I want to avoid doing a foreach loop on the ListView's Items list and setting each ListViewItem's properties, because this list may have very many items and this could be expensive. Instead, is there not some way to do this programmatically using the Style class, or perhaps some run-time logic?

推荐答案

我想通了。你可以这样做它作为一个例子:

I figured it out. You can do it this way as an example:

Style style = new Style();
style.TargetType = typeof(ListViewItem);
style.Setters.Add(new Setter(ListViewItem.BackgroundProperty, Brushes.Pink));
listView.ItemContainerStyle = style;

编辑:您也可以有条件地使用触发器从数据集的某些值设置样式的ListView的项目。我发现这非常有用,所以这可能帮助他人,以及:

You can also conditionally set a style on the ListView's Items using a trigger for certain values from your data set. I found this very useful, so this may help others as well:

DataTrigger trigger = new DataTrigger();
trigger.Binding = new Binding("Text");
trigger.Value = "This is a test.";
trigger.Setters.Add(new Setter(ListViewItem.BackgroundProperty, Brushes.Pink));
style.Triggers.Add(trigger);
listView.ItemContainerStyle = style;

以上code将只设置行的背景下,文本字段设置为的条件下,这是一个考验。

The above code will only set the background of the row under the condition that the Text field is set to "This is a test."

这篇关于WPF:我如何编程风格我的ListView,使所有行具有某种背景颜色和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆