WPF SelectTemplate called multiple times











up vote
0
down vote

favorite












I have a treeview and each time I switch among the nodes, I load a DataTemplate in order to show properly data.
This is an example of by DataTemplate:



<DataTemplate x:Key="dataFlexBCR" DataType="{x:Type cls:cFlexBcr}">
<Grid HorizontalAlignment="Stretch" x:Name="grdDataFlexBcr" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.IsVirtualizing="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Desc" />
<ColumnDefinition Width="Auto" SharedSizeGroup="vActive" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Modalità indentificazione: " Style="{StaticResource labelStyle2}" />
<ComboBox Grid.Column="1" Width="Auto" ItemsSource="{Binding Path=ConfigObject.Bcrs}" Style="{StaticResource popModeStyle}" SelectionChanged="Combo_SelectionChanged" Typography.Capitals="AllPetiteCaps" SelectedValue="{Binding ConfigObject.ActiveId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Key" DisplayMemberPath="Value" />
</Grid>
</DataTemplate>
<local:FlexconfigTemplateSelector dataFlexBCR="{StaticResource dataFlexBCR}" dataFlexBCRs="{StaticResource dataFlexBCRs}" x:Key="flexconfigTemplateSelector" />


The problem is that my SelectTemplate method (below) is called for each rows present in the datagrid defined in the template. So if I have 20 rows, the template is called 20 times.



public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
LeafInfo li = item as LeafInfo;

if (li == null) return null;

if (li.ConfigObject is cFlexBcrSingle)
return dataFlexBCR;

if (li.ConfigObject is cFlexBcr)
return dataFlexBCRs;

return null;
}


Code behind of my FleconfigTemplateSelector:



public class FlexconfigTemplateSelector : DataTemplateSelector
{
public DataTemplate dataFlexBCR { get; set; }
public DataTemplate dataFlexBCRs { get; set; }


/// <summary>
/// template
/// </summary>
/// <param name="item"></param>
/// <param name="container"></param>
/// <returns></returns>
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
LeafInfo li = item as LeafInfo;

if (li == null) return null;

if (li.ConfigObject is cFlexBcr)
return dataFlexBCRs;

if (li.ConfigObject is cFlexBcrSingle)
return dataFlexBCR;

return null;
}
}


Why this behaviour? Shouldn't the template called once?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a treeview and each time I switch among the nodes, I load a DataTemplate in order to show properly data.
    This is an example of by DataTemplate:



    <DataTemplate x:Key="dataFlexBCR" DataType="{x:Type cls:cFlexBcr}">
    <Grid HorizontalAlignment="Stretch" x:Name="grdDataFlexBcr" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.IsVirtualizing="True">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" SharedSizeGroup="Desc" />
    <ColumnDefinition Width="Auto" SharedSizeGroup="vActive" />
    </Grid.ColumnDefinitions>
    <Label Grid.Column="0" Content="Modalità indentificazione: " Style="{StaticResource labelStyle2}" />
    <ComboBox Grid.Column="1" Width="Auto" ItemsSource="{Binding Path=ConfigObject.Bcrs}" Style="{StaticResource popModeStyle}" SelectionChanged="Combo_SelectionChanged" Typography.Capitals="AllPetiteCaps" SelectedValue="{Binding ConfigObject.ActiveId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Key" DisplayMemberPath="Value" />
    </Grid>
    </DataTemplate>
    <local:FlexconfigTemplateSelector dataFlexBCR="{StaticResource dataFlexBCR}" dataFlexBCRs="{StaticResource dataFlexBCRs}" x:Key="flexconfigTemplateSelector" />


    The problem is that my SelectTemplate method (below) is called for each rows present in the datagrid defined in the template. So if I have 20 rows, the template is called 20 times.



    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
    LeafInfo li = item as LeafInfo;

    if (li == null) return null;

    if (li.ConfigObject is cFlexBcrSingle)
    return dataFlexBCR;

    if (li.ConfigObject is cFlexBcr)
    return dataFlexBCRs;

    return null;
    }


    Code behind of my FleconfigTemplateSelector:



    public class FlexconfigTemplateSelector : DataTemplateSelector
    {
    public DataTemplate dataFlexBCR { get; set; }
    public DataTemplate dataFlexBCRs { get; set; }


    /// <summary>
    /// template
    /// </summary>
    /// <param name="item"></param>
    /// <param name="container"></param>
    /// <returns></returns>
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
    LeafInfo li = item as LeafInfo;

    if (li == null) return null;

    if (li.ConfigObject is cFlexBcr)
    return dataFlexBCRs;

    if (li.ConfigObject is cFlexBcrSingle)
    return dataFlexBCR;

    return null;
    }
    }


    Why this behaviour? Shouldn't the template called once?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a treeview and each time I switch among the nodes, I load a DataTemplate in order to show properly data.
      This is an example of by DataTemplate:



      <DataTemplate x:Key="dataFlexBCR" DataType="{x:Type cls:cFlexBcr}">
      <Grid HorizontalAlignment="Stretch" x:Name="grdDataFlexBcr" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.IsVirtualizing="True">
      <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" SharedSizeGroup="Desc" />
      <ColumnDefinition Width="Auto" SharedSizeGroup="vActive" />
      </Grid.ColumnDefinitions>
      <Label Grid.Column="0" Content="Modalità indentificazione: " Style="{StaticResource labelStyle2}" />
      <ComboBox Grid.Column="1" Width="Auto" ItemsSource="{Binding Path=ConfigObject.Bcrs}" Style="{StaticResource popModeStyle}" SelectionChanged="Combo_SelectionChanged" Typography.Capitals="AllPetiteCaps" SelectedValue="{Binding ConfigObject.ActiveId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Key" DisplayMemberPath="Value" />
      </Grid>
      </DataTemplate>
      <local:FlexconfigTemplateSelector dataFlexBCR="{StaticResource dataFlexBCR}" dataFlexBCRs="{StaticResource dataFlexBCRs}" x:Key="flexconfigTemplateSelector" />


      The problem is that my SelectTemplate method (below) is called for each rows present in the datagrid defined in the template. So if I have 20 rows, the template is called 20 times.



      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
      LeafInfo li = item as LeafInfo;

      if (li == null) return null;

      if (li.ConfigObject is cFlexBcrSingle)
      return dataFlexBCR;

      if (li.ConfigObject is cFlexBcr)
      return dataFlexBCRs;

      return null;
      }


      Code behind of my FleconfigTemplateSelector:



      public class FlexconfigTemplateSelector : DataTemplateSelector
      {
      public DataTemplate dataFlexBCR { get; set; }
      public DataTemplate dataFlexBCRs { get; set; }


      /// <summary>
      /// template
      /// </summary>
      /// <param name="item"></param>
      /// <param name="container"></param>
      /// <returns></returns>
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
      LeafInfo li = item as LeafInfo;

      if (li == null) return null;

      if (li.ConfigObject is cFlexBcr)
      return dataFlexBCRs;

      if (li.ConfigObject is cFlexBcrSingle)
      return dataFlexBCR;

      return null;
      }
      }


      Why this behaviour? Shouldn't the template called once?










      share|improve this question















      I have a treeview and each time I switch among the nodes, I load a DataTemplate in order to show properly data.
      This is an example of by DataTemplate:



      <DataTemplate x:Key="dataFlexBCR" DataType="{x:Type cls:cFlexBcr}">
      <Grid HorizontalAlignment="Stretch" x:Name="grdDataFlexBcr" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.IsVirtualizing="True">
      <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" SharedSizeGroup="Desc" />
      <ColumnDefinition Width="Auto" SharedSizeGroup="vActive" />
      </Grid.ColumnDefinitions>
      <Label Grid.Column="0" Content="Modalità indentificazione: " Style="{StaticResource labelStyle2}" />
      <ComboBox Grid.Column="1" Width="Auto" ItemsSource="{Binding Path=ConfigObject.Bcrs}" Style="{StaticResource popModeStyle}" SelectionChanged="Combo_SelectionChanged" Typography.Capitals="AllPetiteCaps" SelectedValue="{Binding ConfigObject.ActiveId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Key" DisplayMemberPath="Value" />
      </Grid>
      </DataTemplate>
      <local:FlexconfigTemplateSelector dataFlexBCR="{StaticResource dataFlexBCR}" dataFlexBCRs="{StaticResource dataFlexBCRs}" x:Key="flexconfigTemplateSelector" />


      The problem is that my SelectTemplate method (below) is called for each rows present in the datagrid defined in the template. So if I have 20 rows, the template is called 20 times.



      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
      LeafInfo li = item as LeafInfo;

      if (li == null) return null;

      if (li.ConfigObject is cFlexBcrSingle)
      return dataFlexBCR;

      if (li.ConfigObject is cFlexBcr)
      return dataFlexBCRs;

      return null;
      }


      Code behind of my FleconfigTemplateSelector:



      public class FlexconfigTemplateSelector : DataTemplateSelector
      {
      public DataTemplate dataFlexBCR { get; set; }
      public DataTemplate dataFlexBCRs { get; set; }


      /// <summary>
      /// template
      /// </summary>
      /// <param name="item"></param>
      /// <param name="container"></param>
      /// <returns></returns>
      public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
      LeafInfo li = item as LeafInfo;

      if (li == null) return null;

      if (li.ConfigObject is cFlexBcr)
      return dataFlexBCRs;

      if (li.ConfigObject is cFlexBcrSingle)
      return dataFlexBCR;

      return null;
      }
      }


      Why this behaviour? Shouldn't the template called once?







      wpf templates






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 11:23

























      asked Nov 8 at 11:18









      John Goodman

      246




      246
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          This is the expected behaviour. The SelectTemplate method is called for each item in an ItemsControl and the returned DataTemplate is then applied to the corresponding object in its ItemsSource collection.



          It you only want to call it once for all objects, you might as well use an ItemTemplate.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206690%2fwpf-selecttemplate-called-multiple-times%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            This is the expected behaviour. The SelectTemplate method is called for each item in an ItemsControl and the returned DataTemplate is then applied to the corresponding object in its ItemsSource collection.



            It you only want to call it once for all objects, you might as well use an ItemTemplate.






            share|improve this answer

























              up vote
              1
              down vote



              accepted










              This is the expected behaviour. The SelectTemplate method is called for each item in an ItemsControl and the returned DataTemplate is then applied to the corresponding object in its ItemsSource collection.



              It you only want to call it once for all objects, you might as well use an ItemTemplate.






              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                This is the expected behaviour. The SelectTemplate method is called for each item in an ItemsControl and the returned DataTemplate is then applied to the corresponding object in its ItemsSource collection.



                It you only want to call it once for all objects, you might as well use an ItemTemplate.






                share|improve this answer












                This is the expected behaviour. The SelectTemplate method is called for each item in an ItemsControl and the returned DataTemplate is then applied to the corresponding object in its ItemsSource collection.



                It you only want to call it once for all objects, you might as well use an ItemTemplate.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 12:58









                mm8

                79.1k81731




                79.1k81731






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206690%2fwpf-selecttemplate-called-multiple-times%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Run scheduled task as local user group (not BUILTIN)

                    Port of Spain