silverlight后台动态创建模板列问题
<Grid>
<Grid.Resources>
<my:Day30Style1Converter x:Key="Day30Style1Converter"></my:Day30Style1Converter>
<my:Day30Style2Converter x:Key="Day30Style2Converter"></my:Day30Style2Converter>
<Style x:Key="todaystyle" TargetType="Rectangle" >
<Setter Property="Fill" Value="{Binding Converter={StaticResource Day30Style1Converter}}"></Setter>
<Setter Property="Grid.Column" Value="{Binding Converter={StaticResource Day30Style2Converter}}"></Setter>
</Style>
<Style x:Key="daystyle" TargetType="Rectangle" >
<Setter Property="Fill" Value="#FFC9CACA"></Setter>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
.......
</grid>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="0" Margin="21,12,0,0" Name="cboColumns" VerticalAlignment="Top" Width="243">
<ComboBoxItem Content="1" IsSelected="True" />
<ComboBoxItem Content="2" />
<ComboBoxItem Content="3" />
<ComboBoxItem Content="4" />
</ComboBox>
<Button Content="Show" Height="23" HorizontalAlignment="Left" Grid.Row="0" Name="btnshow" VerticalAlignment="Top" Width="191" Margin="293,12,0,0" Click="btnshow_Click" />
<sdk:DataGrid AutoGenerateColumns="False" Grid.Row="1" Height="299" HorizontalAlignment="Left"
Margin="21,23,0,0" Name="dgrdResult" VerticalAlignment="Top" Width="565"
ItemsSource="{Binding}">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="ID" Width="50" IsReadOnly="True" CanUserResize="True" Binding="{Binding Id}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="Name" Width="100" IsReadOnly="True" CanUserResize="True" Binding="{Binding Name}" ></sdk:DataGridTextColumn>
<sdk:DataGridTemplateColumn Header="Test" CanUserResize="False" Width="Auto">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
silverlight datatemplate 动态创建
private void btnshow_Click(object sender, RoutedEventArgs e)
{
dgrdResult.Columns.RemoveAt(2);
dgrdResult.Columns.Add(GetGridViewColumn("Test",GetElementStr()));
}
private string GetElementStr()
{
Dictionary<int, int> dic = new Dictionary<int, int>();
int intColumnsCount = 3 * (cboColumns.SelectedIndex + 1);
StringBuilder strElementStr = new StringBuilder();
strElementStr.AppendFormat("<Grid>");
//strElementStr.Append(" <Grid.Resources>");
//strElementStr.Append("<Style x:Key='todaystyle' TargetType='Rectangle' >");
//strElementStr.Append("<Setter Property='Fill'></Setter>");
//strElementStr.Append(" <Setter Property='Grid.Column'></Setter>");
//strElementStr.Append("</Style>");
//strElementStr.Append(" <Style x:Key='daystyle' TargetType='Rectangle' >");
//strElementStr.Append("<Setter Property='Fill' Value='#FFC9CACA'></Setter>");
//strElementStr.Append("</Style>");
//strElementStr.Append(" </Grid.Resources>");
strElementStr.AppendFormat("<Grid.ColumnDefinitions>");
for (int i = 0; i < intColumnsCount; i++)
{
strElementStr.Append("<ColumnDefinition Width='40' ></ColumnDefinition>");
}
strElementStr.AppendFormat("</Grid.ColumnDefinitions>");
for (int i = 0; i < intColumnsCount; i++)
{
strElementStr.AppendFormat("<TextBlock Grid.Column='{0}' Text='{0}' />", i);
}
strElementStr.AppendFormat("</Grid>");
return strElementStr.ToString();
}
private DataGridTemplateColumn GetGridViewColumn(string header, string element)
{
DataGridTemplateColumn tempCol = new DataGridTemplateColumn();
tempCol.Header = header;
StringBuilder cellTemp = new StringBuilder();
cellTemp.Append("<DataTemplate ");
cellTemp.Append(" xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'");
cellTemp.Append(" xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'");
cellTemp.Append(" xmlns:grid='clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView'>");
cellTemp.Append(element);
cellTemp.Append("</DataTemplate>");
tempCol.CellTemplate = (DataTemplate)XamlReader.Load(cellTemp.ToString());
return tempCol;
}
/// <summary>
/// 准备数据
/// </summary>
/// <returns></returns>
private List<Person> PreData()
{
List<Person> lstPerson = new List<Person>();
lstPerson.Add(new Person
{
Id = 1,
Name = "whi1",
Sex = "男",
PInfo = new PersonInfo
{
Info1 = "info1_1",
Info2 = "info2_1",
Info3 = "info3_1",
Info4 = "info4_1",
Info5 = "info5_1",
Info6 = "info6_1",
Info7 = "info7_1",
Info8 = "info8_1",
Info9 = "info9_1"
}
});
lstPerson.Add(new Person
{
Id = 2,
Name = "whi2",
Sex = "男",
PInfo = new PersonInfo
{
Info1 = "info1_2",
Info2 = "info2_2",
Info3 = "info3_2",
Info4 = "info4_2",
Info5 = "info5_2",
Info6 = "info6_2",
Info7 = "info7_2",
Info8 = "info8_2",
Info9 = "info9_2"
}
});
lstPerson.Add(new Person
{
Id = 3,
Name = "whi3",
Sex = "男",
PInfo = new PersonInfo
{
Info1 = "info1_3",
Info2 = "info2_3",
Info3 = "info3_3",
Info4 = "info4_3",
Info5 = "info5_3",
Info6 = "info6_3",
Info7 = "info7_3",
Info8 = "info8_3",
Info9 = "info9_3"
}
});
lstPerson.Add(new Person
{
Id = 4,
Name = "whi4",
Sex = "男",
PInfo = new PersonInfo
{
Info1 = "info1_4",
Info2 = "info2_4",
Info3 = "info3_4",
Info4 = "info4_4",
Info5 = "info5_4",
Info6 = "info6_4",
Info7 = "info7_4",
Info8 = "info8_4",
Info9 = "info9_4"
}
});
return lstPerson;
}
[解决办法]
推荐楼主参考以下教程:
http://silverlightchina.net/html/tips/2010/1023/2878.html
http://silverlightchina.net/html/tips/2011/0314/6057.html
http://silverlightchina.net/html/tips/2011/1217/12625.html
http://silverlightchina.net/html/tips/2012/0725/17646.html
[解决办法]