绑定属性不会在自定义控件中更新

本文关键字:自定义控件 更新 属性 绑定 | 更新日期: 2023-09-27 18:33:31

我创建了一个自定义控件,其中包含由自定义类型列表(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 个项目(,但当我单击按钮时它不会更新(仍然是包含 100 个项目的相同列表,而按钮单击创建包含 1.000 个项目的列表(。

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

绑定属性不会在自定义控件中更新

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