Reputation: 31
I am trying to color the row based on a column value and may be other calculations in the value converter, but as long as I set the background value to any binding or anything but the hardcoded color Value="Red" in the setter, it throws a XAML parsing error. I am implementing like this:
<sdk:DataGrid ItemsSource="{Binding
EmailJobs}"
AutoGenerateColumns="False"
Height="Auto"
HorizontalAlignment="Center"
Name="dgEmailJObs"
VerticalAlignment="Top"
Width="Auto" Grid.Row="2">
<sdk:DataGrid.RowStyle>
<Style TargetType="sdk:DataGridRow">
<Style.Setters>
<Setter Property="Background"
Value="{Binding Path=Status,
Converter={StaticResource
valueConverter}}"/>
</Style.Setters>
</Style>
</sdk:DataGrid.RowStyle>
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn
CanUserReorder="False"
CanUserResize="False"
CanUserSort="True" Header="Customer"
Width="Auto" Binding="{Binding
Customer}" />
<sdk:DataGridTextColumn
CanUserReorder="False"
CanUserResize="False"
CanUserSort="True" Header="Program"
Width="Auto" Binding="{Binding
Program}" />
<sdk:DataGridTextColumn
CanUserReorder="False"
CanUserResize="False"
CanUserSort="True"
Header="Application" Width="Auto"
Binding="{Binding Application}"/>
<sdk:DataGridTextColumn
CanUserReorder="True"
CanUserResize="False"
CanUserSort="True" Header="Status"
Width="Auto" Binding="{Binding
Status}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn
CanUserReorder="False"
CanUserResize="False"
CanUserSort="True" Header="Last
Created By" Width="Auto"
Binding="{Binding LastChangedBy}"/>
<sdk:DataGridTextColumn
CanUserReorder="False"
CanUserResize="False"
CanUserSort="True" Header="Last
Created On" Width="Auto"
Binding="{Binding
LastChangedOn,StringFormat='MM/DD/YYYY
hh:mm tt'}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
I am using MVVM and dont wanna use the triggering event. All I want is to simply supply the fourth column value to the converter, but it blows up, should be pretty simple jsut lack of XAML binding knowledge :( any help
Upvotes: 1
Views: 11690
Reputation: 1732
I think I see... Silverlight 4 doesn't allow (anymore) bindings into Setters.
However there are 2 simple solutions to solve your issue :
You can remove the <sdk:DataGrid.RowStyle>
and its content and replace it by RowStyle="{Binding Path=Status, Converter={StaticResource valueConverter}}"
as a property of your datagrid (example below)
<sdk:DataGrid Grid.Column="1" ItemsSource="{Binding EmailJobs}" RowStyle="{Binding Path=Status, Converter={StaticResource valueConverter}}" AutoGenerateColumns="False" HorizontalAlignment="Center" Name="dgEmailJObs" Width="Auto" Margin="0,0,0,87" LoadingRow="dgEmailJObs_LoadingRow">
Or, you can manage it in the code behind after triggering the event LoadingRow:
void dgEmailJObs_LoadingRow(object sender, DataGridRowEventArgs e)
{
myClass c = e.Row.DataContext as myClass;
if (c != null)
{
if (c.Status == "Stopped")
{
e.Row.Background = new SolidColorBrush(Colors.Red);
}
else
{
e.Row.Background = new SolidColorBrush(Colors.Green);
}
}
}
Hope that helps ;)
Upvotes: 2
Reputation: 31
my value converter:
public class EmailJobStatusConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (value.ToString()=="Stopped") ?
new SolidColorBrush(Colors.Red) :
new SolidColorBrush(Colors.Green);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush resultColor = value as SolidColorBrush;
if (resultColor.Color == Colors.Green)
{
return "Green";
}
else
return "Red";
}
}
Upvotes: 0
Reputation: 11
private void grdPendingRequest_LoadingRow(object sender, DataGridRowEventArgs e)
{
string changedColPath = string.Empty;
dynamic itemData = (dynamic)e.Row.DataContext;
if (itemData != null)
changedColPath = itemData.Changed_Field;
if (changedColPath != string.Empty)
{
string colPath;
System.Windows.Data.Binding binding;
string[] ChangedColNameList = null;
foreach (DataGridColumn col in grdPendingRequest.Columns)
{
if (col is DataGridBoundColumn)
{
binding = (col as DataGridBoundColumn).Binding;
colPath = binding.Path.Path;
ChangedColNameList = changedColPath.Split(';');
if (ChangedColNameList.Contains(colPath))
{
FrameworkElement felt = col.GetCellContent(e.Row);
FrameworkElement felt_result = GetParent(felt, typeof(DataGridCell));
if (felt_result != null)
{
DataGridCell cell = (DataGridCell)felt_result;
//cell.Background = new SolidColorBrush(Color.FromArgb(255, 196, 219, 249));
cell.Background = new SolidColorBrush(Colors.Red);
}
}
}
}
}
}
Upvotes: 1