- darthmaule2
- Posts: 98
- Joined:
Adding to C# API Demo -- DataTemplateSelector not working
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?
-
sfernandez
Site Admin
- Posts: 2997
- Joined:
Re: Adding to C# API Demo -- DataTemplateSelector not workin
There is nothing wrong in your code, is just that DataTemplateSelector classes implemented in C# are not working yet. Native code is not calling the virtual function of the managed class
This feature is only working in the Native API right now. If you want, you can create a ticket in our bugtracker to follow the status of its development. We will fix it as soon as possible.
This feature is only working in the Native API right now. If you want, you can create a ticket in our bugtracker to follow the status of its development. We will fix it as soon as possible.
- darthmaule2
- Posts: 98
- Joined:
Re: Adding to C# API Demo -- DataTemplateSelector not working
Has this been implemented yet? I just tried it and it doesn't seem to be working for me. I'll add a feature request in the bugtracker if it's not...
-
sfernandez
Site Admin
- Posts: 2997
- Joined:
Re: Adding to C# API Demo -- DataTemplateSelector not working
Hello,
It is already implemented in Noesis C# API. I just tried the following scenario that works fine:
What is not working in your case?
It is already implemented in Noesis C# API. I just tried the following scenario that works fine:
Code: Select all
namespace MyNamespace
{
public class TestDataTemplateSelector : DataTemplateSelector
{
public DataTemplate Even { get; set; }
public DataTemplate Odd { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
int n = int.Parse(item.ToString());
return (n % 2) == 0 ? Even : Odd;
}
}
}
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace">
<Grid.Resources>
<DataTemplate x:Key="EvenTemplate">
<TextBlock Text="{Binding}" Foreground="Red"/>
</DataTemplate>
<DataTemplate x:Key="OddTemplate">
<TextBlock Text="{Binding}" Foreground="Blue"/>
</DataTemplate>
<local:TestDataTemplateSelector x:Key="TemplateSelector" Even="{StaticResource EvenTemplate}" Odd="{StaticResource OddTemplate}"/>
</Grid.Resources>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<ContentControl Content="1235" ContentTemplateSelector="{StaticResource TemplateSelector}"/>
<ContentControl Content="26442" ContentTemplateSelector="{StaticResource TemplateSelector}"/>
</StackPanel>
</Grid>
- darthmaule2
- Posts: 98
- Joined:
Re: Adding to C# API Demo -- DataTemplateSelector not working
*sigh*... I uncommented my DataTemplateSelector and now it's working. I'm not sure what I was doing wrong... checked it multiple times. Probably had the resource key misspelled or something.
-
sfernandez
Site Admin
- Posts: 2997
- Joined:
Re: Adding to C# API Demo -- DataTemplateSelector not working
Great to know it is working for you also. thanks for the update.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 34 guests