c# - 如何将图表的轴绑定(bind)到 List() 的特定索引?

标签 c# .net wpf xaml wpftoolkit

我正在尝试使用 WPF 工具包创建图表,其中 Y 轴由 List() 中的值更新。 .我正在尝试通过特定索引访问该值。当前,绑定(bind)到 List() 的索引而不是 int创建一个“没有合适的轴可用于绘制相关值。”异常。

这是我目前所拥有的,请注意我尝试获取 DependentValuePath访问索引:

<Charting:LineSeries VerticalAlignment="Stretch" 
                     HorizontalAlignment="Stretch" 
                     ItemsSource="{Binding Path=MemoryStats}" 
                     IndependentValuePath="Timestamp" 
                     DependentValuePath="ByteCount[0]"
                     Title="Data Points">

这就是MemoryStats值包含在后面的代码中:

public List<int> ByteCount { get; set; }
public DateTime Timestamp { get; set; }

当 XAML 中的 LineSeries 具有属性 DependentValuePath="ByteCount" 时,图表工作正常当代码隐藏使用一个简单的 int 时:

public int ByteCount { get; set; }
public DateTime Timestamp { get; set; }

如何让它绑定(bind)到 List() 的索引而不是 int

编辑

我已经能够通过命名其索引从后面的代码中获取列表中的特定值,但是会有多个 LineSeries创建图表时动态生成。我想将每个绑定(bind)到 List<int>() 的索引,我每隔一秒左右重新创建一次。

这是 MemoryStats 的完整方法用于更新 UI。它通过更新所有 LineSeries 来工作Y 值到单个 ByteCount int , 所以目前所有的行看起来都一样。

    public class MemorySample
    {
        public static MemorySample Generate(List<int> dataPoints)
        {

            return new MemorySample
            {
                ByteCount = dataPoints[0],
                Timestamp = DateTime.Now
            };
        }

        public int ByteCount { get; set; }
        public DateTime Timestamp { get; set; }
    }

当然,我想要所有 LineSeries与众不同。我想要每个 LineSeries 的 X 轴图表的 TimeStamp (所以他们都有相同的时间戳)和各种LineSeries通过 List() 更新它们的 Y 轴值整数,每个使用一个单独的索引 List()

我将尝试实现类型转换器,但我不完全确定何时何地执行此操作。

编辑 2

我让它按照我想要的方式工作!我找到了很多帮助from this S.O. question regarding using multiple series in a line chart.

似乎类型转换器也可以工作,所以 Shimrod 已经回答了这个问题。然而,我最终做的是设置 LineSeries 的绑定(bind) ItemSource到索引,然后检索该索引内容的数据。

所以,这就是 LineSeries 的内容看起来像:

        <Charting:LineSeries VerticalAlignment="Stretch" 
                            HorizontalAlignment="Stretch" 
                            ItemsSource="{Binding [0]}" 
                            IndependentValuePath="X" 
                            DependentValuePath="Y"
                            Title="Data Points">
        </Charting:LineSeries>

注意 ItemSource 中的索引捆绑。在后面的代码中,我设置了 DataContext控件的 ObservableCollection ,它包含一个继承自“IList”的对象(您可以使用任何这样做的对象),并且该对象包含包含属性 X 的对象和 Y特性。

   public ObservableCollection<InheritsFromIList<ObjectWithXandYProperties>> VariableDataContextIsSetTo { get; set; }

访问 ObservableCollection 的特定索引将返回列表。然后,该列表中的项目显示在图表上。

最佳答案

我认为最简单的方法是在您的类中添加一个属性,它是 Listint 值。

例如

int val { get { return ByteCount[0]; } }

或者您也可以创建一个转换器,它将列表作为绑定(bind)并将索引作为参数,并返回所需的值。

例如(我没试过这个):

public class ElementOfListConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is IList && parameter is int)
        {
            var lst = value as IList;
            int pos = (int)parameter;

            if (lst.Count >= pos)
            {
                return lst[pos];
            }
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

编辑

这些是使用转换器的步骤:

  1. 创建类 (ElementOfListConverter),如前所示。
  2. 添加一个指向转换器位置的新 xml 命名空间。例如。 xmlns:local="clr-namespace:WpfApplication2"
  3. 在您的窗口(或用户控件)的资源中,添加对转换器的引用,如下所示:

    <Window.Resources>
        <local:ElementOfListConverter x:Key="ElemOfList" />
    </Window.Resources>
    
  4. 在您的绑定(bind)中,指定要使用的转换器(使用它的键) {Binding YourElement,Converter={StaticResource ElemOfList},ConverterParameter=0}

这种方法的优点是可以直接在xaml中指定元素的索引。您还可以使用 MultiBinding 将此值绑定(bind)到其他某个值和一个 MultiValueConverter . (如果您需要更多信息,请参阅 this question on S.O.

关于c# - 如何将图表的轴绑定(bind)到 List() 的特定索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17979956/

相关文章:

c# - LayoutTransform 中的 ScaleTransform 不起作用,但适用于 RenderTransform

c# - Viewbox中的文本 block 不是多行

c# - 如何使用 ADO.NET 调用存储过程?

c# - ASP.NET MVC 模型类更新功能

c# - 固定间隔后刷新更新面板

.net - 修复了 "A type parameter is missing a constraint ' 时非常可怕的 'N : equality'“错误

c# - 如何删除超过 7 天的文件夹 C#/NET

c# - 为什么 CollectionViewSource.GetDefaultView(...) 从任务线程内部返回错误的 CurrentItem?

c# - 在没有反射的情况下迭代 C# 中对象的所有字段

.net - 想在Core Project中放一个4.6目录