2016-02-07 132 views
0

我创建了一个自定义控件,其中包含一个自定义类型列表(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); 
    } 

如果我去检查我的名单在我的自定义控件的价值我看到它在主窗口(一个值传递,与列表中的100个项目)的初始化正确实施,但它点击按钮时不更新(仍然是100个项目的同一列表,而点击按钮则创建一个包含1.000项目的列表)。

当我的主窗口中相应的列表发生更改时,如何在自定义控件中更新列表?

回答

1

您正在将您的DataContext设置为OHLCVSerie的当前实例,尝试将您的DataContext设置为此(您的MainWindow)。

+0

现在的问题是:为什么我没有发现它...... :)谢谢。 –

+0

不客气,好工作! – Giangregorio

相关问题