C
C#2y ago
Elio

❔ ✅ DataGrid RowStyle depends of the datatype WPF

Hello, how can i change the row style of my datagrid depends of the datatype in it to represent this kind of table.
14 Replies
HimmDawg
HimmDawg2y ago
Are you grouping the data here or can those separators be set anywhere?
Elio
ElioOP2y ago
theses datas are in a list of step which containts 2 datatype herited from Step Step => StepNormal and StepSpecial so the so called separators in blue and yellow if i've understand can be set anywhere depends of his index called Pas in our case
HimmDawg
HimmDawg2y ago
Not really sure if there is an elegant solution for this, but have the following in mind:
Elio
ElioOP2y ago
maybe i can use listview or combobox ? idk i'm not really familiar with wpf
HimmDawg
HimmDawg2y ago
- bind the list of steps and the indices to your datagrid view as a multibinding - create a class that contains only the data and a bool IsSeparator - create a converter that converts the list of items and the indices to a new list of that new class - create a data template for ??? that can handle IsSeparator and shows what you need not really sure which template you have to modify, hence the ??? fluffyFoxThink just be DataGrid.ItemTemplate
Elio
ElioOP2y ago
I'm not sure to fully understand your explanation. i've not mention but the separator is link to the datatype, you put the separator if the step is StepSpecial. I had something in mind like when the datatype is Stepnormal i set the rowstyle to get the number and if the datatype is StepSpecial, i just set the row style to have a yellow of blue style background with the image
public class StepSpecial : Step
{
public string Message { get; set; } = null!;

public EnumStepType EnumStepType { get; set; } = EnumStepType.Pause;

public StepSpecial()
{
}
}
public class StepSpecial : Step
{
public string Message { get; set; } = null!;

public EnumStepType EnumStepType { get; set; } = EnumStepType.Pause;

public StepSpecial()
{
}
}
public class StepNormal : Step
{
public double AV { get; set; } = 0;

public double AR { get; set; } = 0;

public double PR { get; set; } = 0;

public double ROT { get; set; } = 0;

public double Speed { get; set; } = 0;

public StepNormal()
{
}
}
public class StepNormal : Step
{
public double AV { get; set; } = 0;

public double AR { get; set; } = 0;

public double PR { get; set; } = 0;

public double ROT { get; set; } = 0;

public double Speed { get; set; } = 0;

public StepNormal()
{
}
}
Elio
ElioOP2y ago
currently i've something like this
HimmDawg
HimmDawg2y ago
Ah, so then we can skip the converter part But you'll need different data templates then
Elio
ElioOP2y ago
i create a template to each step and i select the good one with a converter ?
HimmDawg
HimmDawg2y ago
Hold on, I'm gonna check something really quick So I'm quite sure you need to use DataTemplateSelector here. But i'm not sure how it is exactly assigned to a gridview I made a simple test case, however the selector never tries to choose a template fluffyFoxThink seems to work for a ListBox though
Elio
ElioOP2y ago
i should probably use itemTemplateselector for the datagrid i think can you enlighten me please what make me choose between listbox or datagrid in that case ?
HimmDawg
HimmDawg2y ago
DataGrid is for editing ListBox is not
It seems to work for a ListBox though
Seems like you'd need to assign a CellTemplateSelector directly on a DataGridTemplateColumn itself, instead of the ItemTemplate on the DataGrid
public class SeparatorRowTemplateSelector : DataTemplateSelector
{
public DataTemplate DataRowTemplate { get; set; }
public DataTemplate SeparatorTemplate { get; set; }

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
IUser? data = item as IUser;
if (data is User2)
{
return SeparatorTemplate;
}
return DataRowTemplate;
}

}
public class SeparatorRowTemplateSelector : DataTemplateSelector
{
public DataTemplate DataRowTemplate { get; set; }
public DataTemplate SeparatorTemplate { get; set; }

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
IUser? data = item as IUser;
if (data is User2)
{
return SeparatorTemplate;
}
return DataRowTemplate;
}

}
So here's a selector class I made for the test case. Note that I used IUser as the base and User, User2 as the deriving types
<Window.Resources>
<local:SeparatorRowTemplateSelector
x:Key="TemplateSelector"
SeparatorTemplate="{StaticResource UserTemplate}"
DataRowTemplate="{StaticResource User2Template}">
</local:SeparatorRowTemplateSelector>
</Window.Resources>
<Window.Resources>
<local:SeparatorRowTemplateSelector
x:Key="TemplateSelector"
SeparatorTemplate="{StaticResource UserTemplate}"
DataRowTemplate="{StaticResource User2Template}">
</local:SeparatorRowTemplateSelector>
</Window.Resources>
And this is how you add it as a resource
Elio
ElioOP2y ago
ok we are close to the solution, Selector is definitly the right way to resolve my problem from there i think i can continu you gave me all the key to do it Tanks a lot ! 🙂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server