WPF reuse of identical nodes

2 posts, 0 answers
  1. Itai
    Itai avatar
    6 posts
    Member since:
    Mar 2014

    Posted 18 Sep 2014 Link to this post

    Hello.

    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.)
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 22 Sep 2014 Link to this post

    Hi,

    I am afraid it is not possible to reuse instances of such identical nodes for different instances of RadGridView. You will have to define the EventBindings every time you need.

    Regards,
    Dimitrina
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top