我想改变一行的颜色。所以基本上。我从我的Web服务获得了一个名为我所有管理员的名字。我想将该列表与我的datagridsource中的信息进行比较。如果他们是管理员,我希望他们的行变成红色。
void proxy_GetListOfAdminsInGroupCompleted(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++;
}
}
谢谢!
答案 0 :(得分:2)
我创建了一个示例CustomClass:
public class CustomClass
{
public string PropertyToBeWatched { get; set; }
}
创建了一个列表:
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" });
然后创建了Datagrid:
<sdk:DataGrid ItemsSource="{Binding MyList}" RowStyle="{StaticResource Style1}">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding PropertyToBeWatched}" Header="Property1"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
然后是资源:
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>
这是转换器:
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();
}
}
它基本上根据属性“PropertyToBeWatched”获取并设置solidcolorbrush。
希望这有帮助