Home > database >  C# WPF DataGrid AutoGeneratingColumn PackIcon Content
C# WPF DataGrid AutoGeneratingColumn PackIcon Content

Time:11-01

I use MVVM Toolkit and Material Design XAML Toolkit

My View:

I have a Datagrid:

<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Data}" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" />

My ViewModel:

    public partial class ViewModel: ObservableObject
    {
        [ObservableProperty]
        DataTable _data;


        public ViewModel()
        {
            var currentData = new DataTable();
            currentData.Columns.Add("Name", typeof(string));
            currentData.Columns.Add("BooleanValue", typeof(bool));
            currentData.Columns.Add("Icon", typeof(PackIcon));


            //Two rows
            DataRow firstRow = currentData.NewRow();
            DataRow secondRow = currentData.NewRow();

            firstRow[0] = "Text1";
            firstRow[1] = true;
            firstRow[2] = new PackIcon { Kind = PackIconKind.SmileyHappy };
            secondRow[0] = "Text2"; 
            secondRow[1] = false;
            secondRow[2] = new PackIcon { Kind = PackIconKind.SmileyAngry };

            currentData.Rows.Add(firstRow);
            currentData.Rows.Add(secondRow);

            Data = currentData;
        }
    }

So far, so good.

The DataGrid shows MaterialDesignThemes.Wpf.PackIcon instead of the actual Icon. I found the solution for non-dynamic DataGrid but this will not work for dynamic DataGrid.

The goal is that a specific cell will display the actual icon.

Because of the dynamic DataGrid I need AutoGenerateColumns="True". The idea is that I intercept the AutoGenerateColumns, and everytime the type is PackIcon, I change the content.

For that reason, I intercept it:


 private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.PropertyType == typeof(PackIcon))
            {
                //todo 
            }
        }

I tried several things, but non seems to work. How is it possible to set the "content" to the actual PackIcon content. I thought, I could do something with e.Column.CellStyle but I found no way to convert a IconPack to a Style.

Thanks in advance!

CodePudding user response:

How is it possible to set the "content" to the actual PackIcon content.

You could create a DataGridTemplateColumn with a ContentControl that displays the icon:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(PackIcon))
    {
        const string Xaml = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"  
            "<ContentControl Content=\"{{Binding {0}}}\"  />"  
            "</DataTemplate>";
        e.Column = new DataGridTemplateColumn()
        {
            Header = e.PropertyName,
            CellTemplate = (DataTemplate)XamlReader.Parse(string.Format(Xaml, e.PropertyName))
        };
    }
}
  • Related