Accessing C# things from XAML
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Accessing C# things from XAML

  1. #1
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Question 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

  2. #2
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    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.

  3. #3
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    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?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,370

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center