Silverlight模拟WPF的TemplateSelector,以实现动态显示不同column
代码下载:
Service:WcfService7.zip
Silverlight:DynamicGridViewDemo.zip
思路简单介绍如下:
1.AttachedBehavior结合Resource来为Column中的列添加事件
2.使用DP,来封装多个GridView:
public class FormFieldTemplateSelector : UserControl { public DataTemplate FirstTemplate { get; set; } public DataTemplate SecondTemplate { get; set; } public static readonly DependencyProperty FieldTypeProperty = DependencyProperty.Register("FieldType", typeof(string), typeof(FormFieldTemplateSelector), new PropertyMetadata(string.Empty)); public string FieldType { get { return (string)GetValue(FieldTypeProperty); } set { SetValue(FieldTypeProperty, value); } } public FormFieldTemplateSelector() { Loaded += new RoutedEventHandler(OnLoaded); } private void OnLoaded(object sender, RoutedEventArgs e) { string fieldType = FieldType; if (fieldType == "1") { Content = FirstTemplate.LoadContent() as UIElement; } else if (fieldType == "2") { Content = SecondTemplate.LoadContent() as UIElement; } else { Content = null; } } }
相应的XAML:
<local:FormFieldTemplateSelector Grid.Column="1" FieldType="{Binding CarriedNO}" Margin="0,2,0,2"> <local:FormFieldTemplateSelector.FirstTemplate> <DataTemplate> <data:DataGrid ItemsSource="{Binding FlightList, Mode=OneWay}" SelectedItem="{Binding SelectedFlightInfo, Mode=TwoWay}" Margin="24,29,54,33" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" IsReadOnly="True" > <data:DataGrid.Columns> <data:DataGridTemplateColumn Header="FlightNO" Width="60" SortMemberPath="FlightNO" > <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Tag="FlightNO" Text="{Binding FlightNO}" /> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTextColumn Header="Date" Width="58" Binding="{Binding Date}" /> <data:DataGridTemplateColumn Header="ORIG" Width="58" SortMemberPath="ORIG"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Tag="ORIG" Text="{Binding ORIG}" local:MouseLeftButtonUp.Command="{Binding Path=Value, Source={StaticResource MouseLeftButtonUpCommand}}" /> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns> </data:DataGrid> </DataTemplate> </local:FormFieldTemplateSelector.FirstTemplate> <local:FormFieldTemplateSelector.SecondTemplate> <DataTemplate> <data:DataGrid ItemsSource="{Binding FlightList, Mode=OneWay}" SelectedItem="{Binding SelectedFlightInfo, Mode=TwoWay}" Margin="24,29,54,33" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" IsReadOnly="True" > <data:DataGrid.Columns> <data:DataGridTemplateColumn Header="FlightNO" Width="60" SortMemberPath="FlightNO" > <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Tag="FlightNO" Text="{Binding FlightNO}" /> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> <data:DataGridTextColumn Header="Date" Width="58" Binding="{Binding Date}" /> <data:DataGridTemplateColumn Header="DEST" Width="58" SortMemberPath="DEST"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Tag="DEST" Text="{Binding DEST}" local:MouseLeftButtonUp.Command="{Binding Path=Value, Source={StaticResource MouseLeftButtonUpCommand}}" /> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns> </data:DataGrid> </DataTemplate> </local:FormFieldTemplateSelector.SecondTemplate> </local:FormFieldTemplateSelector>