我在做项目时需要动态添加一些列来显示数据,在网上查找了一些资料,总结了一下,希望对大家有帮助
1.不显示第1列即列指示器(Row Indicators)
在 telerik:RadGridView中设置属性 RowIndicatorVisibility="Collapsed"
2.不显示最上面的分组面板(Group Panel)
在 telerik:RadGridView中 设置属性 ShowGroupPanel="False"
3.不显示最左侧的那条线(Frozen Columns Splitter)
在 telerik:RadGridView中 设置属性 CanUserFreezeColumns="False"
4.不显示各表头右侧的过滤按钮(Filter Icon)
在 telerik:RadGridView中设置属性 IsFilteringAllowed="False"
5.整体设置GridView表头居中
[html] view plaincopyprint?<Style TargetType="telerik:GridViewHeaderCell">
<Setter Property="HorizontalContentAlignment"Value="Center"></Setter>
</Style>
关键代码
.cs
private void Page_Loaded(object sender, RoutedEventArgs e)
{
Telerik.Windows.Controls.GridViewColumn oColumn=new Telerik.Windows.Controls.GridViewColumn();
oColumn.Header="标题";
oColumn.HeaderTextAlignment=TextAlignment.Center;
//string strDataTemplate= "<StackPanel Background='{Binding FCaptionColor}'>\r\n"+
// "<TextBlock Text='{Binding FCaption}'/>\r\n"+
// "</StackPanel>";
//========================动态添加第一列的数据模板========================
StringBuilder sbTemp = new StringBuilder();
sbTemp.Append("<DataTemplate ");
sbTemp.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
sbTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
sbTemp.Append("xmlns:d='http://schemas.microsoft.com/expression/blend/2008' ");
sbTemp.Append("xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' ");
//sbTemp.Append("xmlns:navigation='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation'");
sbTemp.Append("xmlns:telerik='http://schemas.telerik.com/2008/xaml/presentation' >");
sbTemp.Append("<StackPanel Background=\"{Binding FCaptionColor}\">");
sbTemp.Append("<TextBlock Text=\"{Binding FCaption}\"/>");
sbTemp.Append("</StackPanel>");
sbTemp.Append("</DataTemplate>");
DataTemplate dtTemplate=(DataTemplate)XamlReader.Load(sbTemp.ToString());
oColumn.CellTemplate=dtTemplate;
lstData.Columns.Add(oColumn);
//========================动态添加第二列的数据模板========================
sbTemp.Remove(0, sbTemp.Length);
oColumn = new Telerik.Windows.Controls.GridViewColumn();
oColumn.Header = "数据值1";
oColumn.HeaderTextAlignment = TextAlignment.Center;
sbTemp.Append("<DataTemplate ");
sbTemp.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
sbTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
sbTemp.Append("xmlns:d='http://schemas.microsoft.com/expression/blend/2008' ");
sbTemp.Append("xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' ");
//sbTemp.Append("xmlns:navigation='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation'");
sbTemp.Append("xmlns:telerik='http://schemas.telerik.com/2008/xaml/presentation' >");
sbTemp.Append("<StackPanel Background=\"{Binding FValue1Color}\">");
sbTemp.Append("<TextBlock Text=\"{Binding FValue1}\"/>");
sbTemp.Append("</StackPanel>");
sbTemp.Append("</DataTemplate>");
DataTemplate dtTemplate2 = (DataTemplate)XamlReader.Load(sbTemp.ToString());
oColumn.CellTemplate = dtTemplate2;
lstData.Columns.Add(oColumn);
List<FData2> lst = new List<FData2>()
{
new FData2{FCaption="标题一",FCaptionColor="White",FValue1="我的数据1-1",FValue1Color="Red"},
new FData2{FCaption="标题二",FCaptionColor="Red",FValue1="我的数据2-1",FValue1Color="White"},
};
lstData.ItemsSource = lst;
}
}
public class FData2
{
public string FCaption { get; set; }
public string FCaptionColor { get; set; }
public string FValue1 { get; set; }
public string FValue1Color { get; set; }
//public string FValue2 { get; set; }
//public string FValue2Color { get; set; }
}
Xaml代码
<navigation:Page x:Class="SilverlightListView单元格绑定颜色.RadGridView动态添加列"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="RadGridView动态添加列 Page" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" Loaded="Page_Loaded">
<Grid x:Name="LayoutRoot" Height="400" Width="400" VerticalAlignment="Center" HorizontalAlignment="Center">
<telerik:RadGridView x:Name="lstData" Height="211" HorizontalAlignment="Left" Margin="30,46,0,0" VerticalAlignment="Top" Width="337" RowIndicatorVisibility="Collapsed" ShowGroupPanel="False" AutoGenerateColumns="False" >
</telerik:RadGridView>
<TextBlock Height="21" HorizontalAlignment="Left" Margin="63,14,0,0" Name="textBlock1" Text="我是在后台代码中动态绑定的" VerticalAlignment="Top" Width="182" />
</Grid>
</navigation:Page>