Cast exception with ItemsControl
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:
And I got this when I launch it on Unity:
Thank you in advance!
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:
Code: Select all
<ItemsControl x:Name="SessionsList" Style="{StaticResource SessionPanelStyle}">
<!--GENERATED SESSION TILES HERE-->
</ItemsControl>
Code: Select all
<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>
Code: Select all
private ItemsControl SessionsList;
private void InitializeComponent()
{
Noesis.GUI.LoadComponent(this, "Assets/WPF/File.xaml");
SessionsList = (ItemsControl)FindName("SessionsList");;
}
Code: Select all
public void FillSessionsList()
{
SessionsList.Items.Clear();
var tile = new SessionTile()
{
...
};
SessionsList.Items.Add(tile);
}
Do you have an idea? Do you need more informations?InvalidCastException: Specified cast is not valid.
Thank you in advance!
Re: Cast exception with ItemsControl
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!
-
sfernandez
Site Admin
- Posts: 2997
- Joined:
Re: Cast exception with ItemsControl
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:
Set that ViewModel as DataContext of your user control:
And in xaml:
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:
Code: Select all
public class ViewModel
{
public ObservableCollection<SessionTile> SessionList { get; private set; }
public ViewModel() { SessionList = new ObservableCollection<SessionTile>(); }
}
Code: Select all
public YourUserControl: UserControl
{
public YourUserControl()
{
InitializeComponent();
DataContext = new ViewModel();
}
...
}
Code: Select all
<ItemsControl ItemsSource="{Binding SessionList}" .../>
Re: Cast exception with ItemsControl
Sorry I didn't see you answered my topic!
Yes I'm using NoesisGUI 3.0. I will try your suggestion, thank you!
Yes I'm using NoesisGUI 3.0. I will try your suggestion, thank you!