WPF 可绑定依赖项 属性 不工作
WPF bindable dependency property is not working
我绑定如下:
views:SciChartUserControl Name="SciChartUserControl" Quotes="{Binding QuoteCollection}"></views:SciChartUserControl>
我确定 QuoteCollection
会更新,因为网格也绑定到它并且我看到它 updated.I 希望在我的 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>
试试这个...
在依赖项 属性 声明中将 PropertyMetadata 更改为以下内容..
new PropertyMetadata(null, new PropertyChangedCallback(QuotesPropertyChanged))
尝试为 PropertyMetadata
class 使用另一个构造函数:
public static DependencyProperty QuotesProperty = DependencyProperty.Register("Quotes",
typeof(List<Quote>), typeof(SciChartUserControl),
new PropertyMetadata(someDefaultvalue, QuotesPropertyChanged));
可能是您正在使用的采用 PropertyChangedCallback
对象的单参数构造函数与采用单个 object
参数的构造函数混淆了。
我相信这是因为你在你的代码后面设置了你的 DataContext,我 运行 在 XAML 中设置它时遇到同样的问题?似乎 DependencyProperty 是相对于 UserControl 的 DataContext 绑定的。 UserControl's DependencyProperty is null when UserControl has a DataContext
<views:SciChartUserControl Name="SciChartUserControl"
Quotes="{Binding DataContext.QuoteCollection, RelativeSource={RelativeSource AncestorType={x:Type dxdo:LayoutPanel}}}" />
我绑定如下:
views:SciChartUserControl Name="SciChartUserControl" Quotes="{Binding QuoteCollection}"></views:SciChartUserControl>
我确定 QuoteCollection
会更新,因为网格也绑定到它并且我看到它 updated.I 希望在我的 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>
试试这个... 在依赖项 属性 声明中将 PropertyMetadata 更改为以下内容..
new PropertyMetadata(null, new PropertyChangedCallback(QuotesPropertyChanged))
尝试为 PropertyMetadata
class 使用另一个构造函数:
public static DependencyProperty QuotesProperty = DependencyProperty.Register("Quotes",
typeof(List<Quote>), typeof(SciChartUserControl),
new PropertyMetadata(someDefaultvalue, QuotesPropertyChanged));
可能是您正在使用的采用 PropertyChangedCallback
对象的单参数构造函数与采用单个 object
参数的构造函数混淆了。
我相信这是因为你在你的代码后面设置了你的 DataContext,我 运行 在 XAML 中设置它时遇到同样的问题?似乎 DependencyProperty 是相对于 UserControl 的 DataContext 绑定的。 UserControl's DependencyProperty is null when UserControl has a DataContext
<views:SciChartUserControl Name="SciChartUserControl"
Quotes="{Binding DataContext.QuoteCollection, RelativeSource={RelativeSource AncestorType={x:Type dxdo:LayoutPanel}}}" />