简体   繁体   English

设置单元格背景颜色在 TreeView Gtk C# 上无法正常工作

[英]Set Cell Background Color Not Working Properly On TreeView Gtk C#

Using SetCellDataFunc for modifying cell property at runtime.使用SetCellDataFunc在运行时修改单元格属性。

Requirement: Modify cell background colour for a particular column要求:修改特定列的单元格背景颜色

Want to modify cell colour of " Item Quantit y" Column想要修改“ Item Quantit y”列的单元格颜色

It modifies the colour of the cell of all columns.它修改所有列的单元格的颜色。

this unexpected behaviour because enabled the only for one column.这种意外行为是因为只为一列启用。

Can you please help me with this你能帮我解决这个问题吗

应用程序输出

Please find snippet code请找到代码片段

using System;
using Gtk;

public partial class MainWindow : Gtk.Window
{
private Gtk.TreeView treeView;
private Gtk.ListStore listStore;

private Gtk.VBox ItemViewPad;
private Gtk.ScrolledWindow scrolledWindow;

public MainWindow() : base(Gtk.WindowType.Toplevel)
{
    Build();

    ItemViewPad = new VBox();

    scrolledWindow = new ScrolledWindow();

    treeView = new TreeView();

    listStore = new ListStore(typeof(string), typeof(string), typeof(string));

    CellRendererText cellRendererText = new CellRendererText();
    TreeViewColumn treeViewColumnName = new TreeViewColumn();
    treeViewColumnName.Expand = true;
    treeViewColumnName.Title = "Item Name";
    treeViewColumnName.PackStart(cellRendererText, false);
    treeViewColumnName.AddAttribute(cellRendererText, "text", 0);

    TreeViewColumn treeViewColumnType = new TreeViewColumn();
    treeViewColumnType.Expand = true;
    treeViewColumnType.Title = "Item Type";
    treeViewColumnType.PackStart(cellRendererText, false);
    treeViewColumnType.AddAttribute(cellRendererText, "text", 1);

    TreeViewColumn treeViewColumnQuantity = new TreeViewColumn();
    treeViewColumnQuantity.Expand = true;
    treeViewColumnQuantity.Title = "Item Quantity";
    treeViewColumnQuantity.PackStart(cellRendererText, false);
    treeViewColumnQuantity.AddAttribute(cellRendererText, "text", 2);
    treeViewColumnQuantity.SetCellDataFunc(cellRendererText, new TreeCellDataFunc(OnChangedQuantity));

    treeView.AppendColumn(treeViewColumnName);
    treeView.AppendColumn(treeViewColumnType);
    treeView.AppendColumn(treeViewColumnQuantity);

    treeView.Model = listStore;

    scrolledWindow.Add(treeView);

    ItemViewPad.PackStart(scrolledWindow, true, true, 6);

    Add(ItemViewPad);
    if ((this.Child != null))
        this.Child.ShowAll();
    ShowAll();

    listStore.AppendValues(new object[] { "Pen", "10", "10" });
    listStore.AppendValues(new object[] { "Pencil", "20", "20" });
    listStore.AppendValues(new object[] { "ColorPen", "30", "30" });
}

private void OnChangedQuantity(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
    string quantity = (string)tree_model.GetValue(iter, 2);
    if(string.IsNullOrEmpty(quantity))
    {
        return;
    }

    Console.WriteLine(tree_column.Title);

    Gdk.Color color = cell.CellBackgroundGdk;

    if(string.Compare(quantity, "30") == 0)
    {
        color = new Gdk.Color(255, 0, 0);
    }
    else if (string.Compare(quantity, "20") == 0)
    {
        color = new Gdk.Color(0, 255, 0);
    }
    else if (string.Compare(quantity, "10") == 0)
    {
        color = new Gdk.Color(0, 0, 255);
    }

    cell.CellBackgroundGdk = color;
}

private void OnChangedType(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
    string type = (string)tree_model.GetValue(iter, 1);
    if (string.IsNullOrEmpty(type))
    {
        return;
    }

    Gdk.Color color = cell.CellBackgroundGdk;

    if (string.Compare(type, "30") == 0)
    {
        color = new Gdk.Color(0, 0, 255);
    }
    else if (string.Compare(type, "20") == 0)
    {
        color = new Gdk.Color(0, 255, 0);
    }
    else if (string.Compare(type, "10") == 0)
    {
        color = new Gdk.Color(255, 0, 0);
    }

    cell.CellBackgroundGdk = color;
}

private void OnChangedName(TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
    string name = (string)tree_model.GetValue(iter, 0);
    if (string.IsNullOrEmpty(name))
    {
        return;
    }
}

protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
    Application.Quit();
    a.RetVal = true;
}
}

    public static void Main(string[] args)
    {
        Application.Init();
        MainWindow win = new MainWindow();
        win.Show();
        Application.Run();
    }

Need to use different CellRendererText binding for each Column.需要为每个 Column 使用不同的 CellRendererText 绑定。

    CellRendererText cellRendererTextName = new CellRendererText();
    TreeViewColumn treeViewColumnName = new TreeViewColumn();
    treeViewColumnName.Expand = true;
    treeViewColumnName.Title = "Item Name";
    treeViewColumnName.PackStart(cellRendererTextName, false);
    treeViewColumnName.AddAttribute(cellRendererTextName, "text", 0);

    CellRendererText cellRendererTextType = new CellRendererText();
    TreeViewColumn treeViewColumnType = new TreeViewColumn();
    treeViewColumnType.Expand = true;
    treeViewColumnType.Title = "Item Type";
    treeViewColumnType.PackStart(cellRendererTextType, false);
    treeViewColumnType.AddAttribute(cellRendererTextType, "text", 1);
    treeViewColumnType.SetCellDataFunc(cellRendererTextType, new TreeCellDataFunc(OnChangedType));

    CellRendererText cellRendererTextQuantity = new CellRendererText();
    TreeViewColumn treeViewColumnQuantity = new TreeViewColumn();
    treeViewColumnQuantity.Expand = true;
    treeViewColumnQuantity.Title = "Item Quantity";
    treeViewColumnQuantity.PackStart(cellRendererTextQuantity, false);
    treeViewColumnQuantity.AddAttribute(cellRendererTextQuantity, "text", 2);
    treeViewColumnQuantity.SetCellDataFunc(cellRendererTextQuantity, new TreeCellDataFunc(OnChangedQuantity));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM