WPF : Binding Order

标签 wpf xaml user-controls mvvm binding

我有一个在我的其他 View 之一中使用的用户控件(ItemsView)。因为我需要访问它的 ViewModel,所以 ItemsViewViewModel 包含在包含控件的 View 的 ViewModel 中。我多次使用这个控件,我发现在我的 ItemsView 控件的 ItemsSource 上绑定(bind)一个集合很有用(不同的,取决于使用它的 View )。它有效,但并非一直有效。
这里有一些代码:

<local:ItemsView DataContext="{Binding Path=ItemsViewModel}" ItemsSource="{Binding Path=DataContext.CurrentItem.Children, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />

问题是 ItemsView 的 DataContext 有时设置在 ItemsSource 之前,有时设置在之后。这是有问题的,因为 ItemsSource 是链接到 ItemsViewViewModel 的依赖项属性。
有没有办法每次都在 ItemsSource 之前设置 DataContext ?

最佳答案

之后 InitializeComponent , 设置 SelectedIndex到 -1 - 这对我有用。

关于WPF : Binding Order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1143941/

相关文章:

c# - 寻找在 WPF 中显示数据行的最简单方法

.net - 如何将应用程序停靠在 Windows 桌面上?

wpf - 如何使用双向绑定(bind)回滚 Silverlight 3 中对对象所做的更改

c# - 在 Usercontrol 中填充 Stackpanel

c# - 从用户控件代码隐藏,在设计模式下,如何获取父控件中包含的所有控件名称?

c# - 如何设置值如果组合框不更改 WPF MVVM 模式中的值

c# - 如何阻止用户在不禁用列表框中选择项目

xaml - 在 UWP 中保持 ListView.HeaderTemplate 可见/静态/粘性

c# - 绑定(bind)字体系列 ViewModel 属性

c# - 如何从 ASP.NET 网站(而非 Web 应用程序)中的用户控件获取父页面