简单的silverlight数据绑定列表框列表<>

时间:2010-07-16 13:26:08

标签: silverlight data-binding silverlight-4.0

看起来这应该很简单......
我有一个usercontrol,我将在选项卡控件的几个选项卡上使用。我希望同步usercontrol的所有实例。

在我的usercontrol中,我有一个字符串列表:

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

还有一个ListBox:

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

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

使用AWJ的建议更新了,但仍然无效:
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" />

1 个答案:

答案 0 :(得分:4)

  • 首先,您只能绑定到属性而不是字段。
  • 其次,Property需要是支持绑定的实例属性
  • 除非您将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}" />
相关问题