4

In my WPF application, I bind data to a DataGrid. Let's say I have a class

public MyRowData
{
    public int Id { get; set; }
    public int string Description { get; set; }
    public CustomClass1 SomeData1 { get; set; }
    public List<CustomClass2> SomeDataList2 { get; set; }
}

This class represents one row in the DataGrid. My DataGrid has a variable number of columns, depending on the length of the SomeDataList2 list.

In my ViewModel, I currently have the properties

public ObservableCollection<DataGridColumn> MyColumnCollection { get; set; }
public List<MyRowData> MyTable { get; set; }

In my XAML file, I have

<DataGrid IsReadOnly="True"
          ItemsSource="{Binding MyTable, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
          viewmodel:DataGridColumnsBehavior.BindableColumns="{Binding MyColumnCollection}"
          AutoGenerateColumns="False" />

where viewmodel:DataGridColumnsBehavior.BindableColumns is a class as suggested here.

MyColumnCollection contains some DataGridColumn objects like

new DataGridTextColumn()
{
    Header = "Id",
    Binding = new Binding("Id")
    {
        Converter = myConverter // some converter I defined earlier
    },
    CanUserSort = false,
    CanUserReorder = false,
    CellStyle = cellStyleDefault, // some style I defined earlier
    ElementStyle = elementStyleRight, // some style I defined earlier
    HeaderStyle = headerStyleRight // some style I defined earlier
};

Of course, I also add a DataGridColumn for each of the other columns I need, i.e. for Description, SomeData1 and for each element in SomeDataList2.

This works perfectly fine so far. But I find it a bit odd to set the styles (CellStyle, ElementStyle, HeaderStyle) in my ViewModel. I would prefer setting these styles in my view (in the XAML file).

I have found many examples of how to set styles in XAML files, however everything I found so far is made under the assumption that the same styles are applied to all columns. However, in my situation, string objects, CustomClass1 objects and CustomClass2 should use different styles.

Now my first question is: Is there a way in XAML to test an object for its type and apply different styles to it depending on the type?

The styles I use also make use of triggers that are bound to some properties. For example, I use

DataTrigger myTrigger = new DataTrigger()
{
    Binding = new Binding("SomeDataList2[" + index + "].SomePropteryOfCustomClass2"),
    Value = true
};

and apply this DataTrigger to a style that is used for displaying the object SomeDataList2[index]. Because I use an index in the name of the binding, I am not sure how to write such a binding in XAML.

Now my second question is: Is there a way to write bindings with indices in XAML, so that each object in SomeDataList2 (where index is the objects index in this list) can use a binding to SomeDataList2[" + index + "].SomePropteryOfCustomClass2 in a trigger?

Community
  • 1
  • 1
Xaver
  • 1,035
  • 9
  • 17

0 Answers0