【问题标题】:WPF Live-Charts -- Binding Separator in Code Not WorkingWPF Live-Charts - 代码中的绑定分隔符不起作用
【发布时间】:2023-03-11 20:16:02
【问题描述】:

使用实时图表 0.9.7 - 和 .NET 4.5

我正在尝试通过代码将分隔符添加到轴并在代码中绑定分隔符步长值,因为我在运行时动态地将新系列添加到笛卡尔图。分隔符会根据数据集的大小而变化。

这是我的代码:

public partial class PlottingTool : UserControl, INotifyPropertyChanged
{
    public static SeriesCollection SeriesCollection { get; set; }

    #region LineSeries1Specifics

    private double _lineSeries1XAxisSeparatorStep;

    // Separator for x-axis
    public double LineSeries1XAxisSeparatorStep     // Bind the separator for the x-axis to this.
    {
        get
        {
            return _lineSeries1XAxisSeparatorStep;
        }
        set
        {
            _lineSeries1XAxisSeparatorStep = value;
            OnPropertyChanged("LineSeries1AxisSeparatorStep");
        }
    }

    #endregion





    public PlottingTool()
    {
        InitializeComponent();

        // Setup Chart
        SetupChart();

        // DataContext for the liveChart
        DataContext = this;
    }

    private void SetupChart()
    {
        // Create an empty series collection.
        SeriesCollection = new SeriesCollection();

        // Setup the axis for the first chart
        ChartFile.AxisX.Add(new Axis
        {
            Title = "Time",
            Unit = TimeSpan.FromSeconds(1).Seconds,
            Separator = new LiveCharts.Wpf.Separator
            {
                IsEnabled = true
            },
            DisableAnimations = true

        });

        ChartFile.AxisY.Add(new Axis
        {
            Unit = 1,
            DisableAnimations = true
        });

        // Bind the series1 separator to the x-axis for the first chart
        Binding xAxisSeparatorBinding = new Binding();
        xAxisSeparatorBinding.Source = this;
        xAxisSeparatorBinding.Path = new PropertyPath("LineSeries1XAxisSeparatorStep");
        xAxisSeparatorBinding.Mode = BindingMode.OneWay;
        xAxisSeparatorBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        BindingOperations.SetBinding(ChartFile.AxisX[0].Separator, LiveCharts.Wpf.Separator.StepProperty, xAxisSeparatorBinding);

    }



    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises this object's PropertyChanged event.
    /// </summary>
    /// <param name="propertyName">The property that has a new value.</param>
    protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;

        if (handler != null)
        {

            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }


    #endregion

    }

XAML 代码:

<UserControl x:Class="DataAnalyzer.Controls.PlottingTool"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:DataAnalyzer.Controls"
         xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <lvc:CartesianChart Name="ChartFile" Series="{Binding SeriesCollection}" Grid.Row="0" LegendLocation="Top" DisableAnimations="true" Hoverable="false" DataTooltip="{x:Null}" Margin="10">
    </lvc:CartesianChart>
</Grid>

为分隔符“step”添加绑定会导致绘图不显示并且 UI 锁定,但没有任何崩溃,并且 Visual Studio 不会提供任何导致错误的反馈。我想知道为什么这不起作用——因为它似乎应该这样做。我已经使用类似的方法为其他项目(如标题)设置了绑定,并且效果很好。

绑定正在更新值,因为我可以通过方法跟踪它的进度。这是绑定的实际分配不起作用。

谢谢...

【问题讨论】:

    标签: c# wpf livecharts


    【解决方案1】:

    如果您至少使用 C#6(.NET Framework 4.6 或更高版本),则可以简单地使用 nameof 运算符来防止拼写错误。看起来像这样:

    public double LineSeries1XAxisSeparatorStep
    {
        get
        {
            return _lineSeries1XAxisSeparatorStep;
        }
        set
        {
            _lineSeries1XAxisSeparatorStep = value;
            OnPropertyChanged(nameof(LineSeries1XAxisSeparatorStep));
        }
    }
    

    或者,如果您修改 OnPropertyChanged 方法,您可以变得更简单:

    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    

    您可以在 System.Runtime.CompilerServices 命名空间中找到 CallerMemberName(从 .NET Framework 4.5 开始)

    通过此修改,您可以将属性简化为:

    public double LineSeries1XAxisSeparatorStep
    {
        get { return _lineSeries1XAxisSeparatorStep; }
        set { _lineSeries1XAxisSeparatorStep = value; OnPropertyChanged(); }
    }
    

    【讨论】:

      【解决方案2】:

      所以我发现了问题 - 以下代码中的简单错字(传递的属性名称不正确 - 缺少 LineSeries1XAxisSeparatorStep 中的 X)用于 OnPropertyChanged(在下面修复):

      public double LineSeries1XAxisSeparatorStep     // Bind the separator for the x-axis to this.
      {
          get
          {
              return _lineSeries1XAxisSeparatorStep;
          }
          set
          {
              _lineSeries1XAxisSeparatorStep = value;
              OnPropertyChanged("LineSeries1XAxisSeparatorStep");
          }
      }
      

      程序挂起是因为我没有初始化 LineSeries1XAxisSeparatorStep 的值,所以它必须使用的值是 0,因为它不为空。如果绑定工作正常,则不需要初始化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 1970-01-01
        相关资源
        最近更新 更多