Hello.
My WPF code has several nodes that are textually identical (including their contents) but refer to different data-contexts:
As you can see, the EventBindings of all RadGridView instances are textually identical, only under different data-contexts of each RadTabItem. This is code duplication, and I need to have code-reuse in order to solve it.
Is there a way to put all of those identical nodes in a single place and just make a reference to that place in each RadTabItem?
(If you also manage to do that for the identical repetitive appearances of ButtonPanel and Mandatory in each RadTabItem that would be great.)
My WPF code has several nodes that are textually identical (including their contents) but refer to different data-contexts:
<telerik:RadTabControl IsSynchronizedWithCurrentItem="True"> <telerik:RadTabControl.Resources> <system:String x:Key="Mandatory">* Mandatory field</system:String> </telerik:RadTabControl.Resources> <telerik:EventToCommandBehavior.EventBindings> <telerik:EventBinding Command="{Binding PreviewSelectionChanged}" EventName="PreviewSelectionChanged" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> </telerik:EventToCommandBehavior.EventBindings> <telerik:RadTabItem Visibility="{Binding ProjectVisibility}"> <telerik:RadTabItem.Header> <Label Content="_Projects" /> </telerik:RadTabItem.Header> <DockPanel> <DockPanel.DataContext> <local:ProjectGridViewModel /> </DockPanel.DataContext> <local:ButtonPanel DockPanel.Dock="Top"/> <TextBlock Text="{StaticResource Mandatory}" DockPanel.Dock="Bottom"/> <telerik:RadGridView ItemsSource="{Binding Path=ItemCollectionProperty, Mode=TwoWay}" SelectedItem="{Binding CurrentlySelectedItem, Mode=TwoWay, Converter={StaticResource ignoreNewItemPlaceHolderConverter}}" AutoGenerateColumns="False" CanUserDeleteRows="True" IsSynchronizedWithCurrentItem="True" ShowGroupPanel="False" DockPanel.Dock="Top" AlternateRowBackground="Beige" AlternationCount="2" SelectionMode="Extended"> <telerik:EventToCommandBehavior.EventBindings> <telerik:EventBinding Command="{Binding SelectionChangingCommand}" EventName="SelectionChanging" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> <telerik:EventBinding Command="{Binding NewEventCommand}" EventName="AddingNewDataItem" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> <telerik:EventBinding Command="{Binding DeleteEventCommand}" EventName="Deleting" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> </telerik:EventToCommandBehavior.EventBindings> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn Header="Name*" IsReadOnly="False" DataMemberBinding="{Binding ItemProperty.Name, Mode=TwoWay}"> <telerik:GridViewDataColumn.CellEditTemplate> <DataTemplate> <TextBox Text="{Binding ItemProperty.Name, Mode=TwoWay}" telerik:TextBoxBehavior.UpdateTextOnTextChanged="True" /> </DataTemplate> </telerik:GridViewDataColumn.CellEditTemplate> </telerik:GridViewDataColumn> <telerik:GridViewDataColumn Header="Description" IsReadOnly="False" DataMemberBinding="{Binding ItemProperty.Description, Mode=TwoWay}"> <telerik:GridViewDataColumn.CellEditTemplate> <DataTemplate> <TextBox Text="{Binding ItemProperty.Description, Mode=TwoWay}" telerik:TextBoxBehavior.UpdateTextOnTextChanged="True" /> </DataTemplate> </telerik:GridViewDataColumn.CellEditTemplate> </telerik:GridViewDataColumn> <telerik:GridViewComboBoxColumn Header="Coordinator*" ItemsSource="{Binding RelativeSource={RelativeSource Findancestor, AncestorType={x:Type Window}}, Path=DataContext.EmployeeCollection}" DataMemberBinding="{Binding Path=ItemProperty.coordinator}" DisplayMemberPath="ItemProperty.Name" SelectedValueMemberPath="ItemProperty.Name" IsVisible="{Binding CoordinatorColumnIsVisible}" /> </telerik:RadGridView.Columns> </telerik:RadGridView> </DockPanel> </telerik:RadTabItem> <telerik:RadTabItem Visibility="{Binding EmployeeVisibility}"> <telerik:RadTabItem.Header> <Label Content="_Employees" /> </telerik:RadTabItem.Header> <DockPanel> <DockPanel.DataContext> <local:EmployeeGridViewModel /> </DockPanel.DataContext> <local:ButtonPanel DockPanel.Dock="Top"/> <TextBlock Text="{StaticResource Mandatory}" DockPanel.Dock="Bottom"/> <telerik:RadGridView ItemsSource="{Binding Path=ItemCollectionProperty, Mode=TwoWay}" SelectedItem="{Binding CurrentlySelectedItem, Mode=TwoWay, Converter={StaticResource ignoreNewItemPlaceHolderConverter}}" CurrentItem="{Binding CurrentlySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ignoreNewItemPlaceHolderConverter}}" AutoGenerateColumns="False" CanUserDeleteRows="True" IsSynchronizedWithCurrentItem="True" ShowGroupPanel="False" DockPanel.Dock="Top" AlternateRowBackground="Beige" AlternationCount="2" SelectionMode="Extended" VerticalContentAlignment="Stretch"> <telerik:EventToCommandBehavior.EventBindings> <telerik:EventBinding Command="{Binding SelectionChangingCommand}" EventName="SelectionChanging" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> <telerik:EventBinding Command="{Binding NewEventCommand}" EventName="AddingNewDataItem" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> <telerik:EventBinding Command="{Binding DeleteEventCommand}" EventName="Deleting" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> </telerik:EventToCommandBehavior.EventBindings> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn Header="Name*" IsReadOnly="False" DataMemberBinding="{Binding ItemProperty.Name, Mode=TwoWay}"> <telerik:GridViewDataColumn.CellEditTemplate> <DataTemplate> <TextBox Text="{Binding ItemProperty.Name, Mode=TwoWay}" telerik:TextBoxBehavior.UpdateTextOnTextChanged="True" /> </DataTemplate> </telerik:GridViewDataColumn.CellEditTemplate> </telerik:GridViewDataColumn> <telerik:GridViewComboBoxColumn Header="Location*" x:Name="Location" ItemsSource="{Binding RelativeSource={RelativeSource Findancestor, AncestorType={x:Type Window}}, Path=DataContext.ROLocationDbmCollectionProperty}" DataMemberBinding="{Binding Path=ItemProperty.Location}" DisplayMemberPath="ItemProperty.Name" SelectedValueMemberPath="ItemProperty.Name" /> <telerik:GridViewDataColumn Header="Recruitment Date*" DataMemberBinding="{Binding ItemProperty.RecruitmentDate}" DataFormatString="dd/MM/yyyy" Width="86" /> </telerik:RadGridView.Columns> </telerik:RadGridView> </DockPanel> </telerik:RadTabItem> <telerik:RadTabItem Visibility="{Binding LocationVisibility}"> <telerik:RadTabItem.Header> <Label Content="_Locations" /> </telerik:RadTabItem.Header> <DockPanel> <DockPanel.DataContext> <local:LocationGridViewModel /> </DockPanel.DataContext> <local:ButtonPanel DockPanel.Dock="Top"/> <TextBlock Text="{StaticResource Mandatory}" DockPanel.Dock="Bottom"/> <telerik:RadGridView ItemsSource="{Binding Path=ItemCollectionProperty, Mode=TwoWay}" SelectedItem="{Binding CurrentlySelectedItem, Mode=TwoWay, Converter={StaticResource ignoreNewItemPlaceHolderConverter}}" CurrentItem="{Binding CurrentlySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ignoreNewItemPlaceHolderConverter}}" AutoGenerateColumns="False" CanUserDeleteRows="True" IsSynchronizedWithCurrentItem="True" ShowGroupPanel="False" DockPanel.Dock="Top" AlternateRowBackground="Beige" AlternationCount="2" SelectionMode="Extended" VerticalContentAlignment="Stretch"> <telerik:EventToCommandBehavior.EventBindings> <telerik:EventBinding Command="{Binding SelectionChangingCommand}" EventName="SelectionChanging" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> <telerik:EventBinding Command="{Binding NewEventCommand}" EventName="AddingNewDataItem" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> <telerik:EventBinding Command="{Binding DeleteEventCommand}" EventName="Deleting" RaiseOnHandledEvents="True" PassEventArgsToCommand="True" /> </telerik:EventToCommandBehavior.EventBindings> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn Header="Name*" IsReadOnly="False" DataMemberBinding="{Binding ItemProperty.Name, Mode=TwoWay}"> <telerik:GridViewDataColumn.CellEditTemplate> <DataTemplate> <TextBox Text="{Binding ItemProperty.Name, Mode=TwoWay}" telerik:TextBoxBehavior.UpdateTextOnTextChanged="True" /> </DataTemplate> </telerik:GridViewDataColumn.CellEditTemplate> </telerik:GridViewDataColumn> </telerik:RadGridView.Columns> </telerik:RadGridView> </DockPanel> </telerik:RadTabItem>... several more similar-looking tab-items</telerik:RadTabControl>As you can see, the EventBindings of all RadGridView instances are textually identical, only under different data-contexts of each RadTabItem. This is code duplication, and I need to have code-reuse in order to solve it.
Is there a way to put all of those identical nodes in a single place and just make a reference to that place in each RadTabItem?
(If you also manage to do that for the identical repetitive appearances of ButtonPanel and Mandatory in each RadTabItem that would be great.)