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?
wpf templates
add a comment |
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?
wpf templates
add a comment |
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?
wpf templates
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
wpf templates
edited Nov 8 at 11:23
asked Nov 8 at 11:18
John Goodman
246
246
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered Nov 8 at 12:58
mm8
79.1k81731
79.1k81731
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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