I have a collection of objects that have a collection of other objects in the following way:
public class Elemento
{
public string Nombre {get; set;}
public ObservableCollection<OtroObjeto> Lista {get; set;}
}
In WPF, I link a collection of "Element" in a DataGrid in this way:
<DataGrid ItemsSource={Binding ColeccionElementos}>
<DataGridTextColumn Header="Nombre" Binding="{Binding Nombre}"/>
<DataGridTextColumn Header="Dia 1" Binding="{Binding Lista[0].Dia}"/>
<DataGridTextColumn Header="Dia 2" Binding="{Binding Lista[1].Dia}"/>
<DataGridTextColumn Header="Dia 3" Binding="{Binding Lista[2].Dia}"/>
</DataGrid>
So far, everything works fine.
If I want to change the background of a cell based on the day value of the element corresponding to the column, I can do it that way, and it works well (I use a converter that transforms the data into a color).
<DataGrid ItemsSource={Binding ColeccionElementos}>
<DataGridTextColumn Header="Nombre" Binding="{Binding Nombre}">
<DataGridTextColumn Header="Dia 1" Binding="{Binding Lista[0].Dia}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Lista[0].Dato, Converter={StaticResource MiConvertidor}}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
...
</DataGrid>
The problem arises when I want to translate the style of the cell to a ResourceDictionary. I put it like that, but I color the entire row based on the data of the first day.
<Style x:Key="EstiloCelda" TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Lista/Dato, Converter={StaticResource MiConvertidor}}"/>
</Style>
I have also tried Lista[/].Dato
and many other combinations and the result is, or the same, or does not recognize the Binding.
How can I refer to the current item in the List from the ResourceDictionary?
Thanks and best regards.
[EDITED]
Here is a valid example
public class Trabajador
{
public string Nombre { get; set; }
public ObservableCollection<Labor> ListaLabores { get; set; }
}
public class Labor
{
public int Dia { get; set; }
public int Dato { get; set; }
}
public class MiConvertidor :IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
int dato = (dato)value;
if (Dato == 1) return new SolidColorBrush(Colors.Red);
if (Dato == 2) return new SolidColorBrush(Colors.Blue);
return Colors.Transparent;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
And here is the DataGrid that uses the data.
<DataGrid ItemsSource={Binding Trabajadores}>
<DataGridTextColumn Header="Nombre" Binding="{Binding Nombre}"/>
<DataGridTextColumn Header="Dia 1" Binding="{Binding Lista[0].Dia}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Lista[0].Dato, Converter={StaticResource MiConvertidor}}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Dia 2" Binding="{Binding Lista[1].Dia}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Lista[1].Dato, Converter={StaticResource MiConvertidor}}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Dia 3" Binding="{Binding Lista[2].Dia}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Lista[2].Dato, Converter={StaticResource MiConvertidor}}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid>
Now I put part of the ViewModel that contains the property linked to the Datagrid:
public class TrabajadoresViewModel
{
public ObservableCollection<Trabajador> Trabajadores{
get{
return new ObservableCollection<Trabajador>(){
new Trabajador() {Nombre = "Nombre1", ListaLabores = new ObservableCollection<Labor>() {Dia = 1, Dato = 2} }
new Trabajador() {Nombre = "Nombre2", ListaLabores = new ObservableCollection<Labor>() {Dia = 2, Dato = 1} }
new Trabajador() {Nombre = "Nombre3", ListaLabores = new ObservableCollection<Labor>() {Dia = 3, Dato = 5} }
};
}
}
}
What I want is not to have to use the cell style in each column of the DataGrid, putting it in a ResourceDictionary.
If I do so, the DataGrid would look like this:
<DataGrid ItemsSource={Binding Trabajadores}>
<DataGridTextColumn Header="Nombre" Binding="{Binding Nombre}"/>
<DataGridTextColumn Header="Dia 1" Binding="{Binding Lista[0].Dia}" CellStyle={StaticResource EstiloCelda}/>
<DataGridTextColumn Header="Dia 2" Binding="{Binding Lista[1].Dia}" CellStyle={StaticResource EstiloCelda}/>
<DataGridTextColumn Header="Dia 3" Binding="{Binding Lista[2].Dia}" CellStyle={StaticResource EstiloCelda}/>
</DataGrid>
And the style in the ResourceDictionary:
<Style x:Key="EstiloCelda" TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Lista/Dato, Converter={StaticResource MiConvertidor}}"/>
</Style>
[EDITION 2]
I add a picture of what I want to achieve:
As you can see, if the data of the days is 1, the background is red. If the data is 2, the background is blue.
This works fine if I put the cell style in each column. If I put it in a ResourceDictionary using the '/' bar to indicate the current element of the collection, it looks like this:
As you can see, take the data from the first column and move it to the whole row, even if the day data of the other columns is different.