【发布时间】:2015-12-20 12:15:33
【问题描述】:
在 MS VS2015 Professional 中,我使用 MVVM 模式使用 Caliburn.Micro 开发 WPF 应用程序。我在我的应用程序中使用用户控件。用户控件也是用 Caliburn.Micro 开发的,但不是在 MVVM 中开发的。用户控件有两个依赖属性:
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); }
}
和
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;
}
}
在我的应用程序中,我定义了两个属性:
public BindableCollection<LineSeriesControl> DataCollection { get; set; }
和
public Double Xmax { get; set; }
在我的应用程序的 MainWindowView.xaml 文件中,我将用户控件包含到我的应用程序中
xmlns:local="clr-namespace:ChartControl;assembly=ChartControl"
其中 ChartControl 是控件的名称并创建绑定:
<local:LineChart Grid.Row="2" Grid.Column="0" DataCollection="{Binding DataCollection}" Xmax="{Binding Xmax}"/>
然后通过按钮点击下一段代码执行:
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 由 150 分配(我已在调试器中检查过),并且在 DataCollection 中添加了 LineSeriesControl 的一个实例。 DataCollection 绑定得很好,用户控件中的代码也很好地使用了它但是 Xmax 的绑定不成功。 usercontrol 中的属性 Xmax 的值为 0。为什么它有位置?我做错了什么?请帮忙。
【问题讨论】:
标签: c# wpf mvvm caliburn.micro