2010-07-16 41 views
2

看起来这应该是简单的...
我有一个用户控件,我将在选项卡控件的多个选项卡上使用。我想要同步usercontrol的所有实例。简单的silverlight数据绑定列表框<>

在我的用户我有一个字符串列表:

​​

按钮和列表框:

<ListBox x:Name="fontList" ItemsSource="{Binding Path=fonts}" /> 

但是,列表框是永远不会填充。
在搜索示例代码中,似乎我已经在几个示例中看到了这个实现,但是我无法使其工作。
感谢您的任何提示...

更新与磨料水射流的建议,但仍然没有工作:
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; } } 
} 

在TestGroup.xaml:

<ListBox x:Name="fontList1" ItemsSource="{Binding Parent.Fonts, ElementName=LayoutRoot}" Margin="3" /> 

回答

4
  • 首先你只能绑定到属性而不是字段。
  • 第二个属性需要是一个实例属性来支持绑定
  • 第三,除非你使UserControl自己的DataContext,你需要一个更复杂的绑定。

在代码中,你将需要: -

public static List<string> fonts = new List<string>() { "Arial", "Courier" }; 
public List<string> Fonts {get { return fonts; } } 

,并在XAML: -

<ListBox x:Name="fontlist" ItemsSource="{Binding Parent.Fonts, ElementName=LayoutRoot}" />