Change the row color in a Silverlight Datagrid
I want to change the color on a row. So basically. I get a from my Web Service with the names of all my admins. And I want to compare that list to the information in my datagridsource. And if they are admins I want their rows to turn red.
void proxy_GetListOfAdm开发者_如何学运维insInGroupCompleted(object sender, gkws.GetListOfAdminsInGroupCompletedEventArgs e)
{
TestlistBox1.ItemsSource = e.Result;
int i = 0;
foreach (var item in UsersInSelectedGroup[0])
{
foreach (var admin in e.Result)
{
if (admin.ToString().Equals(item.ToString()))
{
//Code to change the color of the row where "admin.ToString().Equals(item.ToString())"
}
}
i++;
}
}
Thanks!
I created a sample CustomClass:
public class CustomClass
{
public string PropertyToBeWatched { get; set; }
}
Created a list:
MyList = new ObservableCollection<CustomClass>();
MyList.Add(new CustomClass() { PropertyToBeWatched = "1"});
MyList.Add(new CustomClass() { PropertyToBeWatched = "2" });
MyList.Add(new CustomClass() { PropertyToBeWatched = "2" });
MyList.Add(new CustomClass() { PropertyToBeWatched = "2" });
Then created the Datagrid:
<sdk:DataGrid ItemsSource="{Binding MyList}" RowStyle="{StaticResource Style1}">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding PropertyToBeWatched}" Header="Property1"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
Then the resources:
xmlns:sdk1="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
xmlns:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
<SolidColorBrush x:Key="Red" Color="#FFFF0000" />
<SolidColorBrush x:Key="Green" Color="#FF00FF00" />
<Style x:Key="Style1" TargetType="sdk:DataGridRow">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:DataGridRow">
<Grid Background="{Binding Converter={StaticResource Test}}">
<sdk1:DataGridFrozenGrid x:Name="Root">
<sdk1:DataGridFrozenGrid.Resources>
<Storyboard x:Key="DetailsVisibleTransition">
<DoubleAnimation Duration="00:00:0.1" Storyboard.TargetProperty="ContentHeight" Storyboard.TargetName="DetailsPresenter"/>
</Storyboard>
</sdk1:DataGridFrozenGrid.Resources>
<sdk1:DataGridFrozenGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</sdk1:DataGridFrozenGrid.ColumnDefinitions>
<sdk1:DataGridFrozenGrid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</sdk1:DataGridFrozenGrid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="NormalAlternatingRow">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="NormalSelected">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOverSelected">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="UnfocusedSelected">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundRectangle"/>
</Storyboard>
</VisualState>
<VisualState x:Name="UnfocusedEditing"/>
<VisualState x:Name="NormalEditing"/>
<VisualState x:Name="MouseOverUnfocusedEditing"/>
<VisualState x:Name="MouseOverEditing"/>
<VisualState x:Name="MouseOverUnfocusedSelected"/>
</VisualStateGroup>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid"/>
<VisualState x:Name="Invalid">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="BackgroundRectangle">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="InvalidVisualElement"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="BackgroundRectangle" Grid.ColumnSpan="2" Fill="Red" Opacity="0" Grid.RowSpan="2"/>
<Rectangle x:Name="InvalidVisualElement" Grid.ColumnSpan="2" Fill="#FFF7D8DB" Opacity="0" Grid.RowSpan="2"/>
<sdk1:DataGridRowHeader x:Name="RowHeader" sdk1:DataGridFrozenGrid.IsFrozen="True" Grid.RowSpan="3"/>
<sdk1:DataGridCellsPresenter x:Name="CellsPresenter" Grid.Column="1" sdk1:DataGridFrozenGrid.IsFrozen="True"/>
<sdk1:DataGridDetailsPresenter x:Name="DetailsPresenter" Grid.Column="1" Grid.Row="1"/>
<Rectangle x:Name="BottomGridLine" Grid.Column="1" HorizontalAlignment="Stretch" Height="1" Grid.Row="2"/>
</sdk1:DataGridFrozenGrid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and this is for the converter:
public class RowStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (((CustomClass)value).PropertyToBeWatched == "1")
return App.Current.Resources["Red"] as SolidColorBrush;
else
return App.Current.Resources["Green"] as SolidColorBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
It basically gets and sets the solidcolorbrush based on the property "PropertyToBeWatched".
Hope this helps
精彩评论