【问题标题】:How set color for cell (DataGrid WPFtoolKIt)?如何为单元格设置颜色(DataGrid WPFtoolKIt)?
【发布时间】:2010-10-08 11:36:06
【问题描述】:

我需要这套红色。怎么做? 数据表 dtSet = new DataTable();

    string sql = @"requevst";
    using (MySqlConnection connection = ConnectToDataBase.GetConnection())
    {
        ...
        int count = adapter.Fill(dtSet);
    }

    double totalPrice = 0;
    foreach (DataRow row in dtSet.Rows)
    {
        totalPrice += Double.Parse(row["price"].ToString());
    }
    DataRow lastRow = dtSet.NewRow();
    lastRow["price"] = totalPrice;
    dtSet.Rows.Add(lastRow);

    datagrid.DataContext = dtSet;

【问题讨论】:

    标签: wpf xaml datagrid wpftoolkit


    【解决方案1】:

    我从未使用过数据集,但使用 clr-objects 是一项简单的任务:

    public enum RowType { Ordinary, Total };
    public class MyRowItem
    {
        public string Name { get; set; }
        public int Price { get; set; }
        public RowType RowType { get; set; }
    }
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var items = new List<MyRowItem>
            {
                new MyRowItem{Name="Jack White", Price = 2500, RowType=RowType.Ordinary},
                new MyRowItem{Name="John Black", Price = 3000, RowType=RowType.Ordinary}
            };
            items.Add(new MyRowItem { Name = null, Price = items.Sum(i => i.Price), RowType = RowType.Total });
            this.DataContext = items;
        }
    }
    

    Xaml:

    <Window.Resources>
        <DataTemplate x:Key="priceTemplate">
            <TextBlock x:Name="priceText" Text="{Binding Price}" Margin="-1"/>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RowType}" Value="Total">
                    <Setter Property="Background" TargetName="priceText" Value="Red"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>
    
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ФИО" Binding="{Binding Name}"/>
            <DataGridTemplateColumn Header="Цена" CellTemplate="{StaticResource priceTemplate}"/>
        </DataGrid.Columns>
    </DataGrid>
    

    但是,我建议您在单独的 TextBlock 中显示总和。

    【讨论】:

    • Theanks... "但是,我建议您在单独的 TextBlock 中显示总和。"我不知道该怎么做
    猜你喜欢
    • 1970-01-01
    • 2023-01-13
    • 2013-04-07
    • 1970-01-01
    • 2019-09-06
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    相关资源
    最近更新 更多