c# - 如何处理不支持绑定(bind)的图表?

标签 c# wpf mvvm

我正在使用不支持 mvvm 意义上的绑定(bind)的图表工具。所以我决定我会使用一种消息传递(如 MVVM Light 的消息传递框架)服务,这样每次更新 viewmodel observablecollection 时,都会发送一条消息,当接收到该消息时会将数据点添加到图表中(这将是不幸的是,在后面的代码中)。大家觉得这个计划有什么问题吗?

最佳答案

我个人认为,对于您想要实现的目标而言,消息传递有点过分了,尽管这与品味有关。你不能使用适配器或附加的行为模式吗?这就是他们通常用来替代缺失功能的东西。如果您可以在 Xaml 中实例化图表(我希望您这样做),我建议使用附加行为,否则使用和 apater(对于没有公共(public)构造函数或任何其他棘手内容的元素)并在代码中实例化它.

对于仅支持命令式调用的任何类,您总是可以提出补偿行为。这是一个快速示例:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public Dictionary<int, int> MyValues
        {
            get
            {
                return Enumerable.Range(1, 3).ToDictionary(k => k, v => v);
            }
        }
    }

    // component with the 'missing' property
    public class Imperative : FrameworkElement
    {
        public void Add(int x, int y)
        {
            MessageBox.Show(string.Format("{0}_{1}", x, y));
        }
    }

    // compensating behavior
    public class DeclarativeBehavior : DependencyObject
    {
        public static DependencyProperty MissingPropertyProperty =
            DependencyProperty.RegisterAttached("MissingProperty",
            typeof(Dictionary<int, int>),
            typeof(DeclarativeBehavior),
            new PropertyMetadata((o, e) => 
            { 
                //
                Imperative imperative = (Imperative)o;
                Dictionary<int, int> values = (Dictionary<int, int>)e.NewValue;


                if (imperative != null)
                {
                    foreach (KeyValuePair<int, int> value in values)
                    {
                        imperative.Add(value.Key, value.Value);
                    }
                }
            }));

        public static void SetMissingProperty(DependencyObject o, Dictionary<int, int> e)
        {
            o.SetValue(DeclarativeBehavior.MissingPropertyProperty, e);
        }

        public static Dictionary<int, int> GetMissingProperty(DependencyObject o)
        {
            return (Dictionary<int, int>)o.GetValue(DeclarativeBehavior.MissingPropertyProperty);
        }
    }
}

XAML
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <!--black box, which supports imperative calls is extended to support declarative calls too-->
        <local:Imperative local:DeclarativeBehavior.MissingProperty="{Binding MyValues, 
            RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
    </Grid>
</Window>

关于c# - 如何处理不支持绑定(bind)的图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8589227/

相关文章:

wpf - GDI 渲染到 WPF 窗口

c# - 在 ObservableCollection 内绑定(bind) ObservableCollection

mvvm - 如何从一个标签页导航到另一个标签页?

javascript - 使用异步获取的数据填充 KnockoutJS 支持的表单的惯用方法是什么?

c# - 转换集合的集合 - C#

c# - 如何从从另一个文件夹中加载的程序集中获取类型?

c# - C# 中的用户可扩展访问者模式

c# - 带参数的 slider 的 ThumbToolTipValueConverter

c# - 在不同的 View 模型之间共享PropertyChanged数据

c# - 抽象 ViewModel 在被继承时是否被视为模型?