Thanatos
Topic Author
Posts: 3
Joined: 16 Jun 2020, 18:48

Cast exception with ItemsControl

16 Jun 2020, 19:02

Hello,

I am stuck with a cast problem on two ItemsControl which have for ItemPanel a WrapPanel and a StackPanel.

Here is the code for the WrapPanel:
<ItemsControl x:Name="SessionsList" Style="{StaticResource SessionPanelStyle}">
	<!--GENERATED SESSION TILES HERE-->
</ItemsControl>
    <Style x:Key="SessionPanelStyle" TargetType="ItemsControl">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel Style="{DynamicResource TilesPanel}"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border>
	                    ...
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
private ItemsControl SessionsList;

private void InitializeComponent()
{
	Noesis.GUI.LoadComponent(this, "Assets/WPF/File.xaml");
	
	SessionsList = (ItemsControl)FindName("SessionsList");;
}
public void FillSessionsList()
{
	SessionsList.Items.Clear();
	
	var tile = new SessionTile()
    	{
    		...
    	};
	
	SessionsList.Items.Add(tile);

}
And I got this when I launch it on Unity:
InvalidCastException: Specified cast is not valid.
Do you have an idea? Do you need more informations?

Thank you in advance!
 
Thanatos
Topic Author
Posts: 3
Joined: 16 Jun 2020, 18:48

Re: Cast exception with ItemsControl

17 Jun 2020, 10:14

Ok I found a solution: I took the ItemsControl parent and then removed the SessionList in the InitializeComponent to create the ItemControl directly into FillSessionsList and set the parent content with it, it worked!
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Cast exception with ItemsControl

18 Jun 2020, 11:25

Are you using NoesisGUI 3.0?
I'm not able to reproduce the cast exception if the named element type specified in the xaml corresponds to the one used in code. It shouldn't be necessary to create the ItemsControl in code.

Anyway, have you considered using bindings to populate the ItemsControl instead of directly accessing it from code?
You can define a ViewModel that exposes a SessionList observable collection property:
public class ViewModel
{
  public ObservableCollection<SessionTile> SessionList { get; private set; }
  public ViewModel() { SessionList = new ObservableCollection<SessionTile>(); }
}
Set that ViewModel as DataContext of your user control:
public YourUserControl: UserControl
{
  public YourUserControl()
  {
    InitializeComponent();
    
    DataContext = new ViewModel();
  }
  ...
}
And in xaml:
<ItemsControl ItemsSource="{Binding SessionList}" .../>
 
Thanatos
Topic Author
Posts: 3
Joined: 16 Jun 2020, 18:48

Re: Cast exception with ItemsControl

06 Jul 2020, 10:49

Sorry I didn't see you answered my topic!

Yes I'm using NoesisGUI 3.0. I will try your suggestion, thank you!

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Polymorph, Semrush [Bot] and 10 guests