【问题标题】:Silverlight user control databindingSilverlight 用户控件数据绑定
【发布时间】:2013-03-30 22:15:33
【问题描述】:

我正在尝试创建一个包含列表框的用户控件,但我不知道如何正确设置数据绑定。

在 MainForm.xaml 中(MyItems 是 ViewModel 中定义的 ObservableCollection):

<my:ItemsList Items="{Binding MyItems}"/>

用户控制:

public partial class ItemsList : UserControl
{
    public ItemsList()
    {
        InitializeComponent();
    }

    public IEnumerable Items
    {
        get { return (IEnumerable)GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }

    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.Register("Items", typeof(IEnumerable), typeof(ItemsList), null);
}

还有 xaml(省略命名空间声明):

<UserControl x:Class="MyApp.Controls.ItemsList">
    <phone:LongListSelector ItemsSource="{Binding Items}">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                 <TextBlock Text="{Binding ItemName}" />
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>
</UserControl>

我收到的错误:BindingExpression 路径错误:在“MyApp.ViewModels.MainViewModel”上找不到“Items”属性?!?

【问题讨论】:

    标签: c# wpf silverlight windows-phone-7 data-binding


    【解决方案1】:

    在您的 xaml 中,您需要添加对视图模型的引用并使其成为控件的数据上下文。

        <UserControl xmlns:local="clr-namespace:"myproject.mynamespace;assembly=myproject">
            <UserControl.Resources>
               <local:myviewmodel x:key="viewModel"/>
            </UserControl.Resources>
            <UserControl.DataContext>
               <Binding Source="{StaticResource viewModel}"/>
            </UserControl.DataContext>
            <phone:LongListSelector ItemsSource="{Binding Items}">
               <phone:LongListSelector.ItemTemplate>
               <DataTemplate>
                 <TextBlock Text="{Binding ItemName}" />
              </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
           </phone:LongListSelector>
        </UserControl>
    

    请注意:您可以使用 DisplayMemberPath="ItemName" 属性而不是数据模板,除非您需要以某种方式与文本块进行交互。 希望这会有所帮助。

    【讨论】:

    • 感觉不对,控件不知道视图模型(在设计时)。我打算在几个不同的地方使用不同的视图模型。
    • 我第一次看时可能会错过它。您可能希望在绑定到 c# 文件中的属性或依赖项属性的控件标记上使用模板绑定
    【解决方案2】:

    我缺少的是在用户控件的构造函数中为列表框设置数据上下文...

    LayoutRoot.DataContext = this;
    

    【讨论】:

      【解决方案3】:

      检查:您是否使用了正确的页面数据上下文(必须是您的 ViewModel)?

      用户控件必须是:

      public partial class ItemsList : UserControl
      {
          public ItemsList()
          {
              InitializeComponent();
          }
      
          public IEnumerable Items
          {
          get { return (IEnumerable)GetValue(ItemsProperty); }
          set { SetValue(ItemsProperty, value); }
          }
      
          public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(IEnumerable), typeof(ItemsList), new PropertyMetadata(ItemsChanged));
      
          private static void ItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              var controll = (ItemsList)d;
              var val = (IEnumerable)e.NewValue;
              controll.lls.ItemSource = val;
          }
      

      Xaml

      <UserControl x:Class="MyApp.Controls.ItemsList">
          <phone:LongListSelector x:name="lls">
              <phone:LongListSelector.ItemTemplate>
                  <DataTemplate>
                       <TextBlock Text="{Binding ItemName}" />
                  </DataTemplate>
              </phone:LongListSelector.ItemTemplate>
          </phone:LongListSelector>
      </UserControl>
      

      希望有帮助

      【讨论】:

        猜你喜欢
        • 2010-12-20
        • 2023-03-24
        • 2011-02-03
        • 2010-11-18
        • 2011-11-16
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        • 2011-09-03
        相关资源
        最近更新 更多