Adding to C# API Demo -- DataTemplateSelector not working
Posted: 23 Apr 2015, 17:02
Knowing now that the BuildTool does not yet allow use of C# classes in the xaml, I have switched to setting the ItemControl's ItemSource and ItemTemplate in my C# code and that is working.
Unfortunately, when I set ItemTemplateSelector instead, it is NOT working.
In my xaml, I'm defining a DataTemplate resource and an ItemsControl that will make use of it.
In my C# code, I'm successfully setting the ItemSource and the ItemTemplate.
This works fine.
But if I define a DataTemplateSelector class
and assign this template selector in my C# code
This does NOT work.
What am I doing wrong?
Unfortunately, when I set ItemTemplateSelector instead, it is NOT working.
In my xaml, I'm defining a DataTemplate resource and an ItemsControl that will make use of it.
Code: Select all
<UserControl.Resources>
<DataTemplate x:Key="TextDataTemplate_key">
<TextBlock Text="{Binding StatusItemText}" />
</DataTemplate>
</UserControl.Resources>
<Grid>
<ItemsControl x:Name="State_StatusBarArea" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
Code: Select all
//Load xaml and find the ItemsControl
UserControl userControl = Noesis.GUI.Load("StatusBarAndNotificationsControl.xaml") as UserControl;
object itemsObject = userControl.FindName("State_StatusBarArea");
ItemsControl control = itemsObject as ItemsControl;
//set ItemSource to data model's ObservableCollection property
StateStatusModel = new StatusBarAreaModel();
StateStatusModel.SetItem(0, "First", "Fred", null);
control.ItemsSource = StateStatusModel.StatusBarItemList;
//Find the DataTemplate resource and use it to set the ItemsControl ItemTemplate
DataTemplate textDataTemplate = userControl.FindResource("TextDataTemplate_key") as DataTemplate;
control.ItemTemplate = textDataTemplate;
But if I define a DataTemplateSelector class
Code: Select all
public class StatusItemTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
return TextDataTemplate;
}
public DataTemplate TextDataTemplate { get; set; }
}
Code: Select all
StatusItemTemplateSelector selector = new StatusItemTemplateSelector();
selector.TextDataTemplate = textDataTemplate;
control.ItemTemplateSelector = selector;
What am I doing wrong?