2010-07-16 37 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}" />