【发布时间】:2015-01-14 17:26:12
【问题描述】:
我在这里查看了一些与我的问题相似的其他问题,但没有一个答案。所以,就这样吧……
我正在使用 WPF 和 MVVM 模式开发 C# 应用程序。我有一个包含依赖属性的 UserControl(称为 GroupTree):
public static DependencyProperty ChartGroupsProperty = DependencyProperty.Register("ChartGroups", typeof(ChartGroupCollection), typeof(GroupTree),
new FrameworkPropertyMetadata() { DefaultValue=new ChartGroupCollection(), PropertyChangedCallback = OnChartGroupsChanged, BindsTwoWayByDefault = true });
public ChartGroupCollection ChartGroups
{
get { return (ChartGroupCollection)GetValue(ChartGroupsProperty); }
set
{
SetValue(ChartGroupsProperty, value);
}
}
private static void OnChartGroupsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue)
{
// Nothing at the moment
}
}
它的 ChartGroupCollection 类型是这样定义的:
public class ChartGroupCollection : ObservableCollection<ChartGroup>
{
}
ChartGroup 包含使用 INotifyProperty 的属性,并且我已经独立验证了应该为主集合触发的所有更改事件都正确执行。
使用该控件的 MainWindow.Xaml:
<Controls:GroupTree x:Name="Groups" ChartGroups="{Binding MainGroups}" />
我有另一个控件绑定到 GroupTree 控件中的 ChartGroups 值:
<Controls:ChartsControl x:Name="Charts1_1" Tree="{Binding ElementName=Groups, Path=ChartGroups}" />
我想要发生的是当 GroupTree ChartGroups 值发生变化时,以便动态通知 ChartsControl 这些变化并相应地更新。但此刻,没有喜悦。数据在那里,因为如果手动强制刷新 ChartsControl,则会显示更改。我已经尝试绑定到 MainGroups 属性,希望这会起作用,但那也不起作用。
我可能错过了一些非常明显的东西,但我不确定是什么。有什么想法吗?
罗伯
【问题讨论】:
标签: c# wpf mvvm binding observablecollection