It seems like this should be simple...
I have a usercontrol that I am going to use on several tabs of a tab control. I want all instances of the usercontrol to be synchronized.In my usercontrol I have a list of strings:
public static List<string> fonts = new List<string>() { "Arial", "Courier" };
And a ListBox:
<ListBox x:Name="fontList" ItemsSource="{Binding Path=fonts}" />
开发者_JAVA技巧However, the listbox is never populated.
In searching for example code, it seems like I have seen this implementation in several samples, but I can't get it to work. Thanks for any hints...Updated with AWJ's suggestions, but still not working:
MainPage.xaml.cs:public partial class MainPage : UserControl
{
public static List<string> _fonts
= new List<string>() { "Arial", "Courier" };
public List<string> Fonts { get { return _fonts; } }
}
In TestGroup.xaml:
<ListBox x:Name="fontList1" ItemsSource="{Binding Parent.Fonts, ElementName=LayoutRoot}" Margin="3" />
- First of all you can only bind to Properties not Fields.
- Second the Property needs to be an instance property to support binding
- Third unless you make the UserControl its own DataContext you need a more sophisticated binding.
In code you will need:-
public static List<string> fonts = new List<string>() { "Arial", "Courier" };
public List<string> Fonts {get { return fonts; } }
and in xaml:-
<ListBox x:Name="fontlist" ItemsSource="{Binding Parent.Fonts, ElementName=LayoutRoot}" />
精彩评论