【发布时间】:2010-11-27 09:25:27
【问题描述】:
快速总结
- 在我的应用程序 GUI 的网格中没有显示任何项目
- 启动时网格中会列出正确的列(拾取集合类型)
- CollectionChanged 事件在网格绑定到的 ObservableCollection 上触发
- 输出窗口无绑定错误(绑定成功但未注册委托)
MyView.xaml
.
.
.
<UserControl.Resources>
<xcdg:DataGridCollectionViewSource
x:Key="items"
Source="{Binding Path=Items}"
AutoFilterMode="And" >
</xcdg:DataGridCollectionViewSource>
</UserControl.Resources>
<xcdg:DataGridControl
Name="dataGrid"
ReadOnly="True"
ItemsSource="{Binding Source={StaticResource items}}"
.
.
.
MyView.xaml.cs 构造函数
public MyView()
{
if (!DesignerProperties.GetIsInDesignMode(this))
DataContext = new MyViewModel().Tree;
initializeComponent();
}
MyViewModel.cs 关键部分
public MyTree Tree { get; set; }
public MyViewModel()
{
Tree = new MyTree();
}
MyTree.cs 关键部分
public ObservableCollection<Item> Items{ get; private set; }
public MyTree()
{
Items= new ObservableCollection<Item>();
Items.CollectionChanged += delegate { Console.WriteLine("Trigger"); };
}
每次添加和删除时都会打印触发器,但 UI 仍然认为集合是空的并且不知道更新。没有我的额外委托 Items.CollectionChanged 是 null(即解析的 Xaml 不会导致将侦听器添加到集合中)
- 我做错了什么尝试
绑定我的
DataGridCollectionViewSource给一个 ObservableCollection?
很高兴提供更多细节,我已尝试从我的用例抽象到问题的核心。集合的嵌套可能看起来很荒谬,但它基本上是树中节点的集合,以便更快地访问。
提前致谢!
编辑
通过将PresentationTraceSources.TraceLevel=High 添加到我的 Xaml 中,我得到了一些更详细的输出。
ItemsSource="{Binding Source={StaticResource orders}, PresentationTraceSources.TraceLevel=High}"
我认为问题在于Default update trigger resolved to PropertyChanged 我希望默认为CollectionChanged。不过,我仍然不确定该怎么做。
BindingExpression (hash=43320496): Default mode resolved to OneWay
BindingExpression (hash=43320496): Default update trigger resolved to PropertyChanged
BindingExpression (hash=43320496): Attach to Xceed.Wpf.DataGrid.DataGridControl.ItemsSource (hash=9150720)
BindingExpression (hash=43320496): Resolving source
BindingExpression (hash=43320496): Found data context element: <null> (OK)
BindingExpression (hash=43320496): Use View from DataGridCollectionViewSource (hash=9026257)
BindingExpression (hash=43320496): Activate with root item <null>
BindingExpression (hash=43320496): Replace item at level 0 with <null>, using accessor {DependencyProperty.UnsetValue}
BindingExpression (hash=43320496): GetValue at level 0 from <null> using <null>: <null>
BindingExpression (hash=43320496): TransferValue - got raw value <null>
BindingExpression (hash=43320496): TransferValue - using final value <null>
BindingExpression (hash=43320496): Got PropertyChanged event from DataGridCollectionViewSource (hash=9026257) for View
BindingExpression (hash=43320496): Deactivate
BindingExpression (hash=43320496): Replace item at level 0 with {NullDataItem}
BindingExpression (hash=43320496): Use View from DataGridCollectionViewSource (hash=9026257)
BindingExpression (hash=43320496): Activate with root item DataGridCollectionView (hash=54545236 Count=0)
BindingExpression (hash=43320496): Replace item at level 0 with DataGridCollectionView (hash=54545236 Count=0), using accessor {DependencyProperty.UnsetValue}
BindingExpression (hash=43320496): GetValue at level 0 from DataGridCollectionView (hash=54545236 Count=0) using <null>: DataGridCollectionView (hash=54545236 Count=0)
BindingExpression (hash=43320496): TransferValue - got raw value DataGridCollectionView (hash=54545236 Count=0)
BindingExpression (hash=43320496): TransferValue - using final value DataGridCollectionView (hash=54545236 Count=0)
【问题讨论】:
-
想评论一些答案吗?
-
当然,我陷入了另一个问题,我尝试了一半没有运气,明天早上再尝试其余的。我非常感谢您的所有帮助,我很惊讶能得到这么多好的建议。
标签: c# .net wpf observablecollection