【问题标题】:simple silverlight databinding listbox to List<>简单的 silverlight 数据绑定列表框到 List<>
【发布时间】:2010-07-16 13:26:07
【问题描述】:

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

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

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" />

【问题讨论】:

    标签: silverlight data-binding silverlight-4.0


    【解决方案1】:
    • 首先,您只能绑定到属性而不是字段。
    • 第二个属性需要是实例属性才能支持绑定
    • 第三,除非您将 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}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 2018-08-01
      • 2011-05-10
      • 2014-01-31
      • 1970-01-01
      相关资源
      最近更新 更多