简体   繁体   English

从WPF MVVM Caliburn绑定到用户控件的属性

[英]Binding to property of usercontrol from WPF MVVM Caliburn.Micro application doesn't work

In MS VS2015 Professional I develop WPF application with Caliburn.Micro using MVVM pattern. 在MS VS2015 Professional中,我使用MVVM模式使用Caliburn.Micro开发WPF应用程序。 I use usercontrol in my application. 我在应用程序中使用usercontrol。 The usercontrol was also developed with Caliburn.Micro but not in MVVM. 用户控件也是用Caliburn.Micro开发的,但不是在MVVM中开发的。 The usercontrol has two dependency properties: usercontrol具有两个依赖项属性:

public static DependencyProperty XmaxProperty =
                  DependencyProperty.Register("Xmax", typeof(double),
                  typeof(LineChart),
                  new FrameworkPropertyMetadata(10.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public double Xmax
{
    get { return (double)GetValue(XmaxProperty); }
    set { SetValue(XmaxProperty, value); }
}

and

public static readonly DependencyProperty DataCollectionProperty = DependencyProperty.Register("DataCollection",
          typeof(BindableCollection<LineSeriesControl>), typeof(LineChart),
          new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDataChanged));

public BindableCollection<LineSeriesControl> DataCollection
{
    get { return (BindableCollection<LineSeriesControl>)GetValue(DataCollectionProperty); }
    set { SetValue(DataCollectionProperty, value); }
}

private static void OnDataChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    var lc = sender as LineChart;
    var dc = e.NewValue as BindableCollection<LineSeriesControl>;
    if (dc != null)
        dc.CollectionChanged += lc.dc_CollectionChanged;
}

private void dc_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (DataCollection != null)
    {
        CheckCount = 0;
        if (DataCollection.Count > 0)
            CheckCount = DataCollection.Count;
    }
}

In my application I define two properties: 在我的应用程序中,我定义了两个属性:

public BindableCollection<LineSeriesControl> DataCollection { get; set; }

and

public Double Xmax { get; set; }

In my MainWindowView.xaml file in my application I include usercontrol to my application 在我的应用程序的MainWindowView.xaml文件中,我将用户控件包含在我的应用程序中

xmlns:local="clr-namespace:ChartControl;assembly=ChartControl"

where ChartControl is the name of the control and create binding: 其中ChartControl是控件的名称并创建绑定:

<local:LineChart Grid.Row="2" Grid.Column="0" DataCollection="{Binding DataCollection}" Xmax="{Binding Xmax}"/>

Then by button click the next code executes: 然后通过按钮单击,执行以下代码:

public void DisplayChart()
{
    this.DataCollection.Clear();
    LineSeriesControl ds = new LineSeriesControl();
    . . . . . . . . . . . . . . . . . .
    for (int i = 0; i < 50; i++)
    {
        double x = i / 5.0;
        double y = Math.Sin(x);
        ds.LinePoints.Add(new Point(x, y));
    }
    . . . . . . . . . . . . . . . . . . . . . . .
    this.Xmax = ds.LinePoints.Count + 100;
    . . . . . . . . . . . . . . . . . . . . . . .
    this.DataCollection.Add(ds);
}

Xmax is assigned by 150 (I've checked it in debugger) and in DataCollection one instance of LineSeriesControl is added. Xmax由150分配(我已经在调试器中检查了它),并且在DataCollection中添加了LineSeriesControl的一个实例。 DataCollection binds perfectly well and code in the usercontrol uses it perfectly well but the binding of Xmax is not successful . DataCollection可以很好地绑定,并且用户控件中的代码可以很好地使用它, 但是Xmax的绑定不成功 The property Xmax in usercontrol has the value of 0. Why it has place? usercontrol中的属性Xmax的值为0。为什么放置它? What I've done wrongly? 我做错了什么? Please help. 请帮忙。

You have to call PropertyChanged event on your property in order for the bound DP to get a new value via binding. 您必须在属性上调用PropertyChanged事件,以使绑定的DP通过绑定获取新值。

 private Double _xmax;
 public Double Xmax 
 { 
    get{ return _xmax;} 
    set
    {
         _xmax = value;
         NotifyOfPropertyChange(() => Xmax);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM