我创建了一个自定义控件,其属性包含在自定义类型列表(list<OHLCV>
)中。我正在使用依赖属性来允许它可绑定。
这是我背后的代码
public partial class GraphControl : UserControl
{
//OHLCVSerie Property
public List<OHLCV> OHLCVSerie
{
get { return (List<OHLCV>)GetValue(OHLCVSerieProperty); }
set { SetValueDP(OHLCVSerieProperty, value); }
}
public static readonly DependencyProperty OHLCVSerieProperty =
DependencyProperty.Register("OHLCVSerie", typeof(List<OHLCV>), typeof(GraphControl), null);
//reuse
public event PropertyChangedEventHandler PropertyChanged;
void SetValueDP(DependencyProperty property, object value,
[System.Runtime.CompilerServices.CallerMemberName] String p = null)
{
SetValue(property, value);
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
public GraphControl()
{
InitializeComponent();
(this.Content as FrameworkElement).DataContext = this;
}
}
尚未修改XAML(=用户控件为空,除了后面的代码)
在我的主窗口中,我创建了一个自定义控件的实例,并将其绑定到list<OHLCV>
<Window x:Class="MarketAnalyzer.Tester.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:grph="clr-namespace:MarketAnalyzer.DataVisualization;assembly=MarketAnalyzer.DataVisualization"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<grph:GraphControl OHLCVSerie="{Binding OHLCVSerie}" Margin="0,41,0,0"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
后面的代码在MainWindow的初始化时创建list<OHLCV>
,而按钮在单击时修改列表。
public partial class MainWindow : Window, INotifyPropertyChanged
{
protected virtual void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// The OHLCV Serie
/// </summary>
private List<OHLCV> _ohlcvserie;
public List<OHLCV> OHLCVSerie
{
get { return _ohlcvserie; }
set
{
if (_ohlcvserie != value)
{
_ohlcvserie = value;
RaisePropertyChanged("OHLCVSerie");
}
}
}
public MainWindow()
{
InitializeComponent();
OHLCVSerie = new List<OHLCV>();
OHLCVSerie = CreateRandomOHLCV(1, new DateTime(2016, 1, 1, 9, 0, 0), 4000, 100);
this.DataContext = new
{
OHLCVSerie,
};
}
/// <summary>
/// Generate a random serie following usual index distribution parameters
/// </summary>
/// <param name="MinuteStep">number of minutes between each tick</param>
/// <param name="StartDate">starting date</param>
/// <param name="StartValue">starting value at tick 0</param>
/// <param name="N">Number of ticks</param>
/// <returns></returns>
public List<OHLCV> CreateRandomOHLCV(int MinuteStep, DateTime StartDate, double StartValue, int N)
{
List<OHLCV> RandomOHLCV = new List<OHLCV>();
//whatever code that create my random list
return RandomOHLCV;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OHLCVSerie = CreateRandomOHLCV(1, new DateTime(2016, 1, 1, 9, 0, 0), 4000, 1000);
}
如果我在自定义控件中检查列表的值,我会看到它在MainWindow的初始化时正确实现(一个值被传递,列表中有100个项目),但是当它没有更新时我点击按钮(当按钮单击创建一个包含1.000项的列表时,仍然是包含100个项目的相同列表)。
在MainWindow中更改相应列表后,如何在自定义控件中更新列表?
答案 0 :(得分:1)
您正在将DataContext设置为OHLCVSerie的当前实例,尝试将DataContext设置为此(您的MainWindow)。