看起来这应该很简单......
我有一个用户控件,我将在选项卡控件的多个选项卡上使用它。我希望同步用户控件的所有实例。
在我的用户控件中,我有一个字符串列表:
public static List<string> fonts = new List<string>() { "Arial", "Courier" };
还有一个列表框:
<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" />
最佳答案
- 首先,您只能绑定(bind)到属性,而不能绑定(bind)到字段。
- 其次,属性必须是实例属性才能支持绑定(bind)
- 第三,除非您将 UserControl 设为自己的 DataContext,否则您需要更复杂的绑定(bind)。
在代码中您将需要:-
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}" />
关于silverlight - 简单的 silverlight 数据绑定(bind)列表框到 List<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3265270/