2

我似乎无法把头绕在什么东西上。假设我的代码中有以下逻辑:

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 的 Items 列表上执行 foreach 循环并设置每个 ListViewItem 的属性,因为该列表可能有很多项目,而且这可能很昂贵。相反,是否没有某种方法可以使用 Style 类或一些运行时逻辑以编程方式执行此操作?

4

2 回答 2

10

我想到了。作为示例,您可以这样做:

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

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

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;

上面的代码只会在Text字段设置为“This is a test”的情况下设置行的背景。

于 2013-07-11T14:28:45.217 回答
2

GridView 列不支持样式,仅支持标题样式。而是使用单元格模板来设置列单元格的样式。这使您可以为不同的列使用不同的样式。例如,您希望货币列右对齐,但文本列左对齐。

这是将 XAML 用于具有红色背景样式的时间列的外观。

<GridViewColumn Header="Time">                            
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Date}">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="Red" />
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

这可以在代码中完成。FrameworkElementFactory类是一个已弃用的类,它支持创建模板。以编程方式创建模板的推荐方法是使用XamlReader类的Load方法从字符串或内存流加载 XAML。应修改下面的代码以允许根据列的类型使用不同的样式。

public MainWindow()
{
    InitializeComponent();

    messages.Add(new Message(DateTime.Now, "This is a test."));

    ListView listView = new ListView();
    GridView gridView = new GridView();
    listView.View = gridView;

    gridView.Columns.Add(CreateGridViewColumn("Time", "Date"));
    gridView.Columns.Add(CreateGridViewColumn("Message","Text"));

    listView.SetBinding(ItemsControl.ItemsSourceProperty, new Binding() { Source = messages });

    MainGrid.Children.Add(listView);
}

private static GridViewColumn CreateGridViewColumn(string header, string bindingPath)
{
    GridViewColumn gridViewColumn = new GridViewColumn();
    gridViewColumn.Header = new GridViewColumnHeader() { Content = header };

    string xaml = @"
        <DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> 
            <TextBlock Text=""{Binding " + bindingPath + @"}""> 
                <TextBlock.Style>
                    <Style TargetType=""{x:Type TextBlock}"">
                        <Setter Property=""Background"" Value=""Red"" />
                    </Style>
                </TextBlock.Style>
            </TextBlock>
            </DataTemplate>";
    StringReader stringReader = new StringReader(xaml);
    XmlReader xmlReader = XmlReader.Create(stringReader);
    gridViewColumn.CellTemplate = XamlReader.Load(xmlReader) as DataTemplate;

    return gridViewColumn;
}
于 2013-07-11T15:14:10.593 回答