-
December 15th, 2009, 10:43 AM
#1
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
-
December 15th, 2009, 01:19 PM
#2
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.
-
December 16th, 2009, 05:54 AM
#3
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?
-
December 16th, 2009, 10:45 AM
#4
Re: Accessing C# things from XAML
Search google for "wpf collection binding"
Then search google for "wpf observablecollection binding"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|