我从telerik:RadGridView创建了一个派生类。我需要根据数据设置单元格样式。因此,我创建了一个样式类并将其用作列的单元格样式选择器。我需要为每列设置列单元格样式,我使用了下面的代码,但它不起作用。你知道为什么它不起作用吗?
public WPFGridConv()
{
this.ShowGroupPanel = false;
this.Loaded += WPFGridConv_Loaded;
this.AutoGeneratingColumn += WPFGridConv_AutoGeneratingColumn;
this.ValidationType = GridViewValidationType.None;
this.ValidatesOnDataErrors= GridViewValidationMode.InViewMode;
}
private void WPFGridConv_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
CreateHeader();
}
public void WPFGridConv_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
CreateHeader();
}
private void CreateHeader()
{
foreach (Telerik.Windows.Controls.GridViewColumn column in this.Columns)
{
column.CellStyleSelector= new GridStyleSelector();
}
}
public class GridStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
if (container is GridViewCell)
{
GridViewCell cell = container as GridViewCell;
string columnId = cell.Column.Tag.ToString();
WPFGridConv grid = cell.ParentOfType<WPFGridConv>();
List<InputClass.GridColumnInfoClass> columnsInfoList = grid.workingGridClass.ListOfColumnsInfo;
InputClass.GridColumnInfoClass columnInfo = columnsInfoList.Find(x => x.id == columnId);
Type columnType = columnInfo.typeOfColumn;
if ((cell.Value==null||string.IsNullOrEmpty(cell.Value.ToString())) && columnInfo.IsValueNecessary)
{
return NeedeValueStyle;
}
else
{
if (columnType == null || columnType == typeof(double))
{
double dummyDbl;
if (!double.TryParse(cell.Value.ToString(), out dummyDbl))
return BadInputStyle;
else
return CorrectValueStyle;
}
else if (columnType == typeof(int))
{
int dummyInt;
if (!int.TryParse(cell.Value.ToString(), out dummyInt))
return BadInputStyle;
else
return CorrectValueStyle;
}
return CorrectValueStyle;
}
}
return null;
}
public Style OutOfRangeStyle
{
get
{
Style returnStyle=new Style(typeof(GridViewCell));
returnStyle.Setters.Add(new Setter(GridViewCell.BorderBrushProperty, new SolidColorBrush(Colors.Purple)));
return returnStyle;
}
set
{
}
}
public Style BadInputStyle {
get
{
Style returnStyle = new Style(typeof(GridViewCell));
returnStyle.Setters.Add(new Setter(GridViewCell.BorderBrushProperty, new SolidColorBrush(Colors.Red)));
return returnStyle;
}
set
{
}
}
public Style NeedeValueStyle
{
get
{
Style returnStyle = new Style(typeof(GridViewCell));
returnStyle.Setters.Add(new Setter(GridViewCell.BorderBrushProperty, new SolidColorBrush(Colors.IndianRed)));
return returnStyle;
}
set
{
}
}
public Style CorrectValueStyle
{
get
{
Style returnStyle = new Style(typeof(GridViewCell));
returnStyle.Setters.Add(new Setter(GridViewCell.BorderBrushProperty, new SolidColorBrush(Colors.Black)));
return returnStyle;
}
set
{
}
}
}
如果您使用的是隐式主题,那么您的样式将不起作用,因为它们不基于现有样式。
应删除在 getter 中创建的样式,而是将它们作为 xaml 资源添加到应用/页面/控件中。
例如,更改
public Style CorrectValueStyle
{
get
{
Style returnStyle = new Style(typeof(GridViewCell));
returnStyle.Setters.Add(new Setter(GridViewCell.BorderBrushProperty, new SolidColorBrush(Colors.Black)));
return returnStyle;
}
set
{
}
}
自
public Style CorrectValueStyle { get; set; }
然后在 XAML 中定义样式选择器,如下所示:
<Grid.Resources>
<my:GridStyleSelector x:Key="styleSelector">
<my:GridStyleSelector.CorrectValueStyle>
<Style TargetType="telerik:GridViewCell">
<Setter Property="BorderBrush" Value="Black"/>
</Style>
</my:GridStyleSelector.CorrectValueStyle>
...etc...
</my:GridStyleSelector>
</Grid.Resources>
如果使用隐式主题,则需要为每个样式设置 BasedOn 属性:
<Style TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle">
然后,您可以在如下所示的列上使用样式选择器:
<telerik:RadGridView>
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding SomeProperty}"
CellStyleSelector="{StaticResource styleSelector}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
好,感谢您的回应。 Telerik 支持人员按如下方式回答我,正如我所检查的那样,这是真的。
你好 您定义的样式已正确定义并已应用,但是,由于 RadGridView 的网格线,单元格的边框厚度和边框画笔属性不受尊重。可以通过设置"背景"或"前景"属性来测试这一点,以查看是否应用了样式。 因此,您需要编辑元素的控件模板才能获得所需的结果。这是一个讨论类似场景的论坛帖子。 我希望对您有所帮助。 问候 迪利安·特雷科夫 Telerik by Progress