Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF MVVM图表更改轴_Wpf_Mvvm_Binding - Fatal编程技术网

WPF MVVM图表更改轴

WPF MVVM图表更改轴,wpf,mvvm,binding,Wpf,Mvvm,Binding,我是WPF和MVVM的新手。我正在努力确定改变图表视图的最佳方式。也就是说,最初图表可能有轴:X-ID,Y-Length,然后在用户更改视图(通过lisbox,radiobutton等)后,图表将显示信息:X-Length,Y-ID,并且在用户第三次更改后,它可能会显示新内容:X-ID,Y-Quality 我最初的想法是,最好的方法是更改绑定本身。但我不知道如何告诉XAML中的控件使用ViewModel中的绑定对象进行绑定,或者在运行时更改该绑定是否安全 然后我想也许我可以有一个普通的模型,其中

我是WPF和MVVM的新手。我正在努力确定改变图表视图的最佳方式。也就是说,最初图表可能有轴:X-ID,Y-Length,然后在用户更改视图(通过lisbox,radiobutton等)后,图表将显示信息:X-Length,Y-ID,并且在用户第三次更改后,它可能会显示新内容:X-ID,Y-Quality

我最初的想法是,最好的方法是更改绑定本身。但我不知道如何告诉XAML中的控件使用ViewModel中的绑定对象进行绑定,或者在运行时更改该绑定是否安全

然后我想也许我可以有一个普通的模型,其中包含成员X和Y,并根据需要在viewmodel中填充它们

我的最后一个想法是,我可以有3个不同的图表控件,并根据需要隐藏和显示它们

在MVVM模式中,正确/建议的方法是什么?任何代码示例都将不胜感激

谢谢

以下是bind to bindings方法的内容:

XAML:

DP:

视图模型:

public class BubbleViewModel : BindableObject
{
    private IEnumerable<SessionPerformanceInfo> data;
    public IEnumerable<SessionPerformanceInfo> Data { ... }

    public AxisGroup AxisChoice;
}
公共类BubbleViewModel:BindableObject
{
私有可数数据;
公共IEnumerable数据{…}
公共选择;
}
这将生成以下异常: +$exception{“值不能为空。\r\n参数名称:binding“}System.exception{System.ArgumentNullException}


与bubbleSeries中的4个绑定有关。我很可能会在绑定路径上出错,但正如我所说的,我是绑定和wpf新手,因此任何提示都将不胜感激。

您最初的想法是正确的:您可以绑定到绑定,例如,如果您想同时更改两个轴,您可能会有如下组合框:

<ComboBox SelectedItem="{Binding AxisChoice}">
  <my:AxisChoice XBinding="{Binding ID}" YBinding="{Binding Length}" />
  <my:AxisChoice XBinding="{Binding Length}" YBinding="{Binding ID}" />
  <my:AxisChoice XBinding="{Binding ID}" YBinding="{Binding Quality}" />
</ComboBox>
理想情况下,您可以简单地绑定图表的DependentValueBinding或IndendentValueBinding:

<Chart ...>
  <LineSeries
    DependentValueBinding="{Binding AxisChoice.XBinding}"
    IndependentValueBinding="{Binding AxisChoice.YBinding}" />
</Chart>
因此,您的XAML变成:

<Chart ...>
  <LineSeries
    my:MakeDP.DependentValueBinding="{Binding AxisChoice.XBinding}"
    my:MakeDP.IndependentValueBinding="{Binding AxisChoice,YBinding}" />
</Chart>

我试图简化事情,所以我让ComboBox(Y1轴)的ItemsSource由可观察的绑定集合组成,我将“YBinding”属性直接放在ViewModel中,并将public binding属性设置为ComboBox SelectedItem

dependentvaluebinding正在使应用程序崩溃,但在使用选定的公共绑定1时:

<ComboBox Height="22" Name="comboBox1" 
            DisplayMemberPath="Source.MetricVarName"                           
            ItemsSource="{Binding AllY1Choices}"   
            SelectedIndex="0" 
            SelectedItem="{Binding SelectedY1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</ComboBox>

<chartingToolkit:LineSeries
        ItemsSource="{Binding AllY1Axis}"
        IndependentValueBinding="{Binding AccumDate}"                            
    my:MakeDP.DependentValueBinding="{Binding SelectedY1}">
有什么想法吗?绑定对象“SelectedY1”具有Source.MetricID=“OldA”,这是依赖值绑定的有效值

错误: System.Windows.Controls.DataVisualization.Toolkit.dll中发生“System.InvalidOperationException”类型的异常,但未在用户代码中处理

其他信息:无法使用指定的从属轴。这可能是由于轴的“未设置方向”属性或正在打印的值与轴支持的值之间的类型不匹配造成的


谢谢

我确实在使用WPFToolkit,因此我使用了您建议的这一部分。不幸的是,axischoice元素存在绑定问题。>System.Windows.Data错误:2:找不到目标元素的治理FrameworkElement或FrameworkContentElement。BindingExpression:Path=TotalCount;DataItem=null;目标元素是“AxisGroup”(HashCode=66152109);目标属性为“SizeBinding”(类型为“Binding”),我还必须使AxisChoice对象从dependencyobject继承以获取GetValue和SetValue函数。我的原始答案无效,因为Binding.ProvideValue忽略任何DependencyProperty的类型。我已经更新了我的答案来解决这个问题。
public class AxisChoice
{
  public Binding XBinding { get; set; }
  public Binding YBinding { get; set; }
}
<Chart ...>
  <LineSeries
    DependentValueBinding="{Binding AxisChoice.XBinding}"
    IndependentValueBinding="{Binding AxisChoice.YBinding}" />
</Chart>
public class MakeDP : DependencyObject
{
  public static Binding GetIndependentValueBinding(DependencyObject obj) { return (Binding)obj.GetValue(IndependentValueBindingProperty); }
  public static void SetIndependentValueBinding(DependencyObject obj, Binding value) { obj.SetValue(IndependentValueBindingProperty, value); }
  public static readonly DependencyProperty IndependentValueBindingProperty = DependencyProperty.RegisterAttached("IndependentValueBinding", typeof(Binding), typeof(MakeDP), new PropertyMetadata
  {
    PropertyChangedCallback = (obj, e) =>
    {
      ((DataPointSeries)obj).IndependentValueBinding = (Binding)e.NewValue;
    }
  });

  public static Binding GetDependentValueBinding(DependencyObject obj) { return (Binding)obj.GetValue(DependentValueBindingProperty); }
  public static void SetDependentValueBinding(DependencyObject obj, Binding value) { obj.SetValue(DependentValueBindingProperty, value); }
  public static readonly DependencyProperty DependentValueBindingProperty = DependencyProperty.RegisterAttached("DependentValueBinding", typeof(Binding), typeof(MakeDP), new PropertyMetadata
  {
    PropertyChangedCallback = (obj, e) =>
      {
        ((DataPointSeries)obj).DependentValueBinding = (Binding)e.NewValue;
      }
  });

}
<Chart ...>
  <LineSeries
    my:MakeDP.DependentValueBinding="{Binding AxisChoice.XBinding}"
    my:MakeDP.IndependentValueBinding="{Binding AxisChoice,YBinding}" />
</Chart>
<TextBlock Text="{Binding FirstName}" />
<TextBlock Text="{Binding LastName}" />
<TextBlock my:BindingHelper.TextBinding="{Binding XBinding}" />
public class BindingHelper : DependencyObject
{
  public static BindingBase GetTextBinding(DependencyObject obj) { return (BindingBase)obj.GetValue(TextBindingProperty); }
  public static void SetTextBinding(DependencyObject obj, BindingBase value) { obj.SetValue(TextBindingProperty, value); }
  public static readonly DependencyProperty TextBindingProperty = DependencyProperty.RegisterAttached("TextBinding", typeof(BindingBase), typeof(BindingHelper), new PropertyMetadata
  {
    PropertyChangedCallback = (obj, e) =>
      BindingOperations.SetBinding(obj, TextBlock.TextProperty, (BindingBase)e.NewValue)
  });
}
<ComboBox Height="22" Name="comboBox1" 
            DisplayMemberPath="Source.MetricVarName"                           
            ItemsSource="{Binding AllY1Choices}"   
            SelectedIndex="0" 
            SelectedItem="{Binding SelectedY1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</ComboBox>

<chartingToolkit:LineSeries
        ItemsSource="{Binding AllY1Axis}"
        IndependentValueBinding="{Binding AccumDate}"                            
    my:MakeDP.DependentValueBinding="{Binding SelectedY1}">
private Binding _Y1axisChoice = new Binding();
private ObservableCollection<Binding> _allY1Choices = new ObservableCollection<Binding>();
public ObservableCollection<Binding> AllY1Choices
{
    get { return _allY1Choices; }
    set
    {
    _allY1Choices = value;
    OnPropertyChanged("AllY1Choices");
    }
}

private Binding _selectedY1 = new Binding();
public Binding SelectedY1
{
    get { return _selectedY1; }
    set
    {
        if (_selectedY1 != value)
        {
            _selectedY1 = value;
            OnPropertyChanged("SelectedY1");                    
        }
    }
}
_Y1axisChoice = new Binding("MetricVarID");
_Y1axisChoice.Source = AllY1MetricVars[0];
_selectedY1 = _Y1axisChoice; // set default for combobox

_allY1Choices.Add(_Y1axisChoice);
_Y1axisChoice = new Binding("MetricVarID");
_Y1axisChoice.Source = AllY1MetricVars[1];

_allY1Choices.Add(_Y1axisChoice);