darthmaule2
Topic Author
Posts: 98
Joined: 23 Oct 2014, 19:54

Adding to C# API Demo -- DataTemplateSelector not working

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.
<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>
In my C# code, I'm successfully setting the ItemSource and the ItemTemplate.
//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;
This works fine.

But if I define a DataTemplateSelector class
    public class StatusItemTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            return TextDataTemplate;
        }

        public DataTemplate TextDataTemplate { get; set; }
    }
and assign this template selector in my C# code
StatusItemTemplateSelector selector = new StatusItemTemplateSelector();
selector.TextDataTemplate = textDataTemplate;

control.ItemTemplateSelector = selector;
This does NOT work.

What am I doing wrong?
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Adding to C# API Demo -- DataTemplateSelector not workin

23 Apr 2015, 20:42

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 :oops:

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
Topic Author
Posts: 98
Joined: 23 Oct 2014, 19:54

Re: Adding to C# API Demo -- DataTemplateSelector not working

31 Jul 2019, 22:02

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...
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Adding to C# API Demo -- DataTemplateSelector not working

01 Aug 2019, 17:41

Hello,

It is already implemented in Noesis C# API. I just tried the following scenario that works fine:
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;
        }
    }
}
<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>
What is not working in your case?
 
darthmaule2
Topic Author
Posts: 98
Joined: 23 Oct 2014, 19:54

Re: Adding to C# API Demo -- DataTemplateSelector not working

01 Aug 2019, 18:40

*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.
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Adding to C# API Demo -- DataTemplateSelector not working

02 Aug 2019, 12:39

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