【发布时间】:2015-02-05 13:32:13
【问题描述】:
我绑定如下:
views:SciChartUserControl Name="SciChartUserControl" Quotes="{Binding QuoteCollection}"></views:SciChartUserControl>
我确定 QuoteCollection 会更新,因为网格也绑定到它并且我看到它已更新。我希望在我的 SciChartUserControl 视图的代码隐藏中得到通知,但永远不会调用 QuotesPropertyChanged。这让我发疯了,我已经尝试了几个小时的不同方式......我忽略了什么明显的东西?
public partial class SciChartUserControl : UserControl
{
private SciChartControlViewModel _viewModel;
public SciChartUserControl()
{
//Set ViewModel Datacontext
_viewModel = new SciChartControlViewModel();
DataContext = _viewModel;
InitializeComponent();
}
public static DependencyProperty QuotesProperty = DependencyProperty.Register("Quotes", typeof(List<Quote>), typeof(SciChartUserControl), new PropertyMetadata(QuotesPropertyChanged));
public List<Quote> Quotes
{
get
{
return (List<Quote>)GetValue(QuotesProperty);
}
set
{
SetValue(QuotesProperty, value);
}
}
private static void QuotesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
throw new NotImplementedException();
var quotes = (List<Quote>) e.NewValue;
}
}
编辑:我添加了托管SciChartUserControl 的部分视图。
<dxdo:LayoutPanel Caption="Time Series Visualization">
<views:SciChartUserControl Name="SciChartUserControl" Quotes="{Binding QuoteCollection}"></views:SciChartUserControl>
</dxdo:LayoutPanel>
<dxdo:LayoutPanel Caption="Time Series Data">
<dxg:GridControl Name="SampleDataGridControl" ItemsSource="{Binding QuoteCollection}" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" AutoGeneratedColumns="SampleDataGridControl_OnAutoGeneratedColumns">
<dxg:GridControl.View>
<dxg:TableView AllowEditing="False" AutoWidth="True" BestFitArea="All" AllowBestFit="True" ShowGroupPanel="True" ShowSearchPanelMode="Always"/>
</dxg:GridControl.View>
</dxg:GridControl>
</dxdo:LayoutPanel>
【问题讨论】:
-
那么
QuoteCollection是SciChartControlViewModel类的属性,是在SciChartControlViewModel构造函数中初始化的?您也可以向我们展示 SciChartControlViewModel 代码。 -
不,我将 SciChartControl 托管在不同的 WPF UserControl 中。 viewmodel 实际上包含一个 QuoteCollection。我编辑了我的问题并添加了部分视图以显示另一个控件也绑定到 QuoteCollection 并且更新得很好。
-
但是不要在 SciChartUserControl 的构造函数中设置 DataContext。这会覆盖从父控件继承的 DataContext。
-
我需要访问视图模型,因为它应该作用于更新的
Quotes。我还能如何访问视图模型? -
当您说您将 SciChartControl 托管在不同的 WPF UserControl 中时,我的假设是 DataContext(包含视图模型对象)由该外部控件继承。不然你为什么要告诉我们?无论如何,这里似乎不是这样。
标签: c# wpf binding dependency-properties