简体   繁体   English

为什么在wpf中此用户控件未正确绑定(使用caliburn micro)

[英]why this user control is not bind correctly in wpf (using caliburn micro)

I have a user control that I placed it on another user control as follow: 我有一个用户控件,将其放在另一个用户控件上,如下所示:

<Grid>
    <Control:ViewerControl  x:Name="ViewerControl" />
</Grid>

and in VM I have: 在VM中,我有:

public ImageSourceViewModel()
{
    this.ViewerControl = new ViewerControl();

}

#endregion

#region Public Properties

/// <summary>
/// Gets the viewer control.
/// </summary>
public ViewerControl ViewerControl { get; private set; }

But it is not bindded. 但是它不是绑定的。

I also tested this and did not get the correct result: 我也对此进行了测试,但未获得正确的结果:

<Grid>
    <Control:ViewerControl  DataContext="{Binding ViewerControl}" />
</Grid>

The reason that I think it is not bind correctly is as follow: 我认为它没有正确绑定的原因如下:

To test that it is binded correctly, I placed a textblock on ViewerControl as follow: 为了测试它是否正确绑定,我在ViewerControl上放置了一个文本块,如下所示:

 <TextBlock Grid.Row="1"  Foreground="Blue"  x:Name="TextData" x:FieldModifier="public"/>

and in ViewerControl contructor I have: 在ViewerControl构造函数中,我具有:

public ViewerControl()
{
   TextData.Text = DateTime.Now.ToShortTimeString();   
}

public 

and I changed VM so it sets TextData to a new value as follow: 我更改了VM,因此将TextData设置为新值,如下所示:

public ImageSourceViewModel()
{
    this.ViewerControl = new ViewerControl();
    ViewerControl.TextData.Text = "test";
}   

If the viewer control bind correctly, I should see the test on screen but I am still seeing the time. 如果查看器控件正确绑定,我应该在屏幕上看到测试,但是我仍然看到时间。 so it is not bind correctly. 因此无法正确绑定。

I am using Caliburn micro but the control which is written by somebody else is not using caliburn micro. 我使用的是Caliburn micro,但其他人编写的控件未使用caliburn micro。

You must be very confused about data binding because you are trying to data bind your UserControl to another instance of your UserControl : 你一定很困惑有关数据绑定的,因为你想你的数据绑定UserControl到你的另一个实例UserControl

<Control:ViewerControl DataContext="{Binding ViewerControl}" />

... ...

ViewerControl = new ViewerControl();

In WPF, we data bind data elements to UI elements , so your problem has nothing to do with Caliburn and I'd really recommend that you learn to use WPF properly before you add a further level of complexity (Caliburn). 在WPF中,我们会将数据元素绑定到UI元素 ,因此您的问题与Caliburn无关,我真的建议您在进一步增加复杂性(Caliburn)之前学习正确使用WPF。 Please refer to the Data Binding Overview page on MSDN for further help with understanding data binding. 请参考MSDN上的“ 数据绑定概述”页面,以获取有关数据绑定的更多帮助。

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

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