WPF 用户控件绑定问题?

发布于 2024-09-30 19:09:18 字数 507 浏览 1 评论 0原文

我有这个:

public MyView: UserControl
{
  public IList<Person> PersonList { get; set; } 

  public MyView()
  {
   //code
  }

  public void Display(MyData myData)
  {
    DataContext=myData;
  }
  //code
}

用于此的 XAML 包括一个 ComboBox :

ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=PersonList}"

出于某种原因,这不起作用,并且组合框不会被填充(但是,如果我使用隐藏代码并且我说comboBox.ItemsSource = PersonList,那么组合框就会已被填充)。

有什么想法吗?

问候, 麦德塞布

I have this:

public MyView: UserControl
{
  public IList<Person> PersonList { get; set; } 

  public MyView()
  {
   //code
  }

  public void Display(MyData myData)
  {
    DataContext=myData;
  }
  //code
}

The XAML for this includes a ComboBox :

ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=PersonList}"

For some reason this does not work and the combo box does not get populated ( however, If I use the code-behind and I say comboBox.ItemsSource = PersonList then the combo box does got populated ).

Any ideas ?

Regards,
MadSeb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小情绪 2024-10-07 19:09:18

您的属性已设置为私有,并且您确定要设置 DataContext。

* 编辑*

根据您上面所做的更改,您设置的数据上下文不正确。您的“PersonList”是 MyView 类上的IList<>,但您将数据上下文设置为其他内容。

尝试将项目添加到 MyView 中的 PersonList 并设置 this.DataContext = this; 另外,按照建议,将您的 IList 切换到 ObservableCollection<>< /代码>。

我还强烈建议阅读模型视图视图模型 (MVVM) 方法。这会有很大帮助。 Josh Smith 有很多关于 MVVM 方法的好文章(并且还写了一本关于它的好书)。

这是他的博客的链接。他的书也有链接。

Your property is set to private, and are you sure that you are setting the DataContext.

* EDIT *

Based on the change you made above, you're setting your datacontext incorrectly. Your "PersonList" is anIList<> on your MyView class, but you're setting your data context to something else.

Try adding items to PersonList within MyView and setting this.DataContext = this; Also, as suggested, switch your IList<> to an ObservableCollection<>.

I would also highly suggest reading up on the Model View ViewModel (MVVM) approach. It will help out a lot. Josh Smith has a lot of good articles about the MVVM approach (and has written a good book about it too).

Here's a link to his blog. His book is linked there, as well.

寄居人 2024-10-07 19:09:18

我怀疑这是因为你没有触发任何属性更改事件。如果首次设置属性值时未通知 UI,则绑定不会更新。查看 INotifyPropertyChanged 接口并在您的类中实现它。

同样,如果您的 IList 属性不是 ObservableCollection 或未实现 INotifyCollectionChanged,那么当您将项目添加到列表时,数据绑定 UI不会反映这一点。

I suspect it's because you're not firing any property-changed events. If you don't notify your UI when the property's value is first set, the binding won't update. Look into the INotifyPropertyChanged interface and implement it in your class.

Similarly, if your IList property isn't an ObservableCollection or doesn't implement INotifyCollectionChanged, then when you add items to the list the databound UI won't reflect this.

×纯※雪 2024-10-07 19:09:18

我相信你的绑定声明是问题所在。
“{BindingrelativeSource={RelativeSource Self},Path=PersonList}”正在组合框本身上查找“PersonList”。

您在输出窗口中看到任何绑定错误吗?

理想情况下,您希望绑定到 DataContext (MyData) 中的属性

I believe your binding statement is the problem.
"{Binding RelativeSource={RelativeSource Self}, Path=PersonList}" is looking for a "PersonList" on the combobox itself.

Are you seeing any binding errors in the output window?

Ideally you'd want to bind to a property in your DataContext (MyData)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文