Accessing C# things from XAML
I made a simple Window object that declared a simple list in C# code:
Code:
List<int> list = new List<int>();
public Window1()
{
InitializeComponent();
list.Add(2);
list.Add(3);
}
How to bind to 'list' object in XAML? I tried something like this:
Code:
<ListBox Name="lb" ItemsSource="{Binding Source={StaticResource list}}">
</ListBox>
But obviously I does not work. How to solve that?
Thank you for help in advance
Re: Accessing C# things from XAML
First, list in your example is a private member of your class Window1. XAML can only access public members.
Second, XAML can only create instances, it can not access already existing instances of a type that I'm aware of. You could check out the Window.Resources collection and see about adding the resource 'list' to it. If it exists there, you can then access it from XAML using the StaticResource binding.
Re: Accessing C# things from XAML
So declaring 'list' in XAMl in Resources is the only solution to my problem?
Supposing that I would use such solution - when such 'list' is being created? In other words, when at soonest could I use it in C# code?
Re: Accessing C# things from XAML
Search google for "wpf collection binding"
Then search google for "wpf observablecollection binding"