silverlight - 简单的 silverlight 数据绑定(bind)列表框到 List<>

标签 silverlight data-binding silverlight-4.0

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

在我的用户控件中,我有一个字符串列表:

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/

相关文章:

c# - Silverlight 中的自动 UI 测试

model-view-controller - Backbone.js:避免 View →模型→ View 双重转换

c# - HtmlPage.Document.Cookies 为空

c# - 更改控件内容时显示交叉淡入淡出(或其他)过渡

silverlight-4.0 - 无法分配给 Silverlight 4 中的属性 System.Windows.EventTrigger.RoutedEvent

silverlight - 将样式应用于 Silverlight 4 中的子元素

c# - 动态更新 WMAppManifest 文件

c# - 在 WP7 的 silverlight 中删除图像源

wpf - 在 WPF 中重新计算可绑定(bind)对象的聚合?

android - 使用 Android 数据绑定(bind)在 EditText text.length() > 0 时启用按钮