使用视图模型中的属性绑定到用户控件的属性(包含逻辑)
我正在使用 WPF 和 MVVM 构建一个应用程序。我遇到过一种情况,我有一个包含用户控件(代表计时器)的视图。该用户控件在其代码隐藏中有一个属性,该属性在获取和设置数据之前执行一些计算。
TimerControl.xaml.cs:
public DateTime? DateTimeValue
{
get
{
string hours = this.txtHours.Text;
string minutes = this.txtMinutes.Text;
string amPm = this.txtAmPm.Text;
if (!string.IsNullOrWhiteSpace(hours) && !string.IsNullOrWhiteSpace(minutes) && !string.IsNullOrWhiteSpace(amPm))
{
string value = string.Format("{0}:{1} {2}", this.txtHours.Text, this.txtMinutes.Text, this.txtAmPm.Text);
DateTime time = DateTime.Parse(value);
return time;
}
else
{
return null;
}
}
set
{
DateTime? time = value;
if (time.HasValue)
{
string timeString = time.Value.ToShortTimeString();
//9:54 AM
string[] values = timeString.Split(':', ' ');
if (values.Length == 3)
{
this.txtHours.Text = values[0];
this.txtMinutes.Text = values[1];
this.txtAmPm.Text = values[2];
}
}
}
}
现在我想将此属性绑定到视图的视图模型中存在的属性。以下是 VM 中的属性:
public DateTime? StartTime
{
get
{
return _StartTime;
}
set
{
_StartTime = value;
RaisePropertyChanged("StartTime");
}
}
这就是我在 View 的 xaml 中执行绑定的方式。
MyView.xaml:
<my:TimeControl Background="White" Grid.Column="1" Grid.Row="2" Margin="3" x:Name="StartTimeControl" DateTimeValue="{Binding StartTime}" Width="150" Height="26" HorizontalAlignment="Left">
但它给了我一个错误: 无法在“TimeControl”类型的“DateTimeValue”属性上设置“Binding”。 “绑定”只能在 DependencyObject 的 DependencyProperty 上设置。
我花了几个小时努力寻找一种方法来使这种绑定发挥作用。我什至尝试在 TimeControl 的代码后面为 DateTimeValue 属性创建一个依赖属性,这解决了上述异常,但绑定仍然不起作用。每当我访问虚拟机代码后面的 StartTime 属性时,它都显示为空。尽管它应该通过获取 DateTimeValue 属性来显示有效值。
请建议我一种方法来完成这项工作。谢谢。
I'm building an application using WPF and MVVM. I've come across a situation where I have a view containing a usercontrol (representing a Timer). This usercontrol has a property in it's code behind which performs some calculations before getting and setting data.
TimerControl.xaml.cs:
public DateTime? DateTimeValue
{
get
{
string hours = this.txtHours.Text;
string minutes = this.txtMinutes.Text;
string amPm = this.txtAmPm.Text;
if (!string.IsNullOrWhiteSpace(hours) && !string.IsNullOrWhiteSpace(minutes) && !string.IsNullOrWhiteSpace(amPm))
{
string value = string.Format("{0}:{1} {2}", this.txtHours.Text, this.txtMinutes.Text, this.txtAmPm.Text);
DateTime time = DateTime.Parse(value);
return time;
}
else
{
return null;
}
}
set
{
DateTime? time = value;
if (time.HasValue)
{
string timeString = time.Value.ToShortTimeString();
//9:54 AM
string[] values = timeString.Split(':', ' ');
if (values.Length == 3)
{
this.txtHours.Text = values[0];
this.txtMinutes.Text = values[1];
this.txtAmPm.Text = values[2];
}
}
}
}
Now I wanted to bind this property to a property present in view model of the view. Following is property in the VM:
public DateTime? StartTime
{
get
{
return _StartTime;
}
set
{
_StartTime = value;
RaisePropertyChanged("StartTime");
}
}
This is how I am performing binding in the xaml of View.
MyView.xaml:
<my:TimeControl Background="White" Grid.Column="1" Grid.Row="2" Margin="3" x:Name="StartTimeControl" DateTimeValue="{Binding StartTime}" Width="150" Height="26" HorizontalAlignment="Left">
But it is giving me an error that:
A 'Binding' cannot be set on the 'DateTimeValue' property of type 'TimeControl'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
I've been struggling for hours trying to figure out a way to make this binding work. I have even tried to make a dependency property in the TimeControl's code behind for the DateTimeValue property, which has resolved the above exception, but the binding still doesn't work. Whenever I access StartTime property in the VM's code behind, it is showing null. Although it should show a valid value by getting the DateTimeValue property.
Kindly suggest me a way to make this work. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在这个问题中显示的 DateTimeValue 属性的实现肯定是错误的并导致异常,因为 DateTimeValue 应该是依赖属性。
但您提到您尝试使用依赖属性但没有成功。我想原因是 DataContexts 的冲突,并且您的 XAML 看起来像这样:
此代码不起作用。为什么? TimerControl 的 DataContext 是继承的(或者您可能根本替换它),同时,当您处理 StartTime 时,您会想到 ViewModel 作为 DataContext。因此,您应该清楚地指向正确的 DataContext:
===UPDATE===
我的 Timer 控件的整个代码(如您所见,我的 Timer 有文本框,当您输入一些文本时,文本框会引发适当的事件,我们处理并设置 Time 属性):
并且 XAML:
XAML 中时间控件的用法:
StartupView 的代码隐藏:
ViewModel 中的属性保持不变。在调试过程中,每当我更改计时器中的文本时,都会触发 StartTime 属性的设置器。
Your implementation of DateTimeValue property shown in this question is certainly wrong and leads to exception, because DateTimeValue should be dependency property.
But you mentioned that you have tried to use dependency property with no success. I suppose the reason is in collision of DataContexts and your XAML looks like this:
This code doesn't work. Why? DataContext of TimerControl is inherited (or maybe you replace it at all), meanwhile when you address to StartTime you have in mind ViewModel as DataContext. So you should clearly point to correct DataContext:
===UPDATE===
The whole code of my Timer control (as you can see my Timer has textbox, when you input some text, textbox raises appropriate event, which we handle and set Time property):
And XAML:
Usage of Time control in XAML:
Code behind of StartupView:
Property in ViewModel remains the same. During debugging setter of StartTime property fires every time when I change text in Timer.
你到底想做什么?
您无法绑定到标准属性。如果你想绑定,你应该使用依赖属性。
在 UserControl 内部:
直接绑定到 DateTimeValue 是不可能的,因为没有可用于 string->DateTime 的转换器,因此您必须编写 IValueConverter 并在绑定中指定它。
当然,您应该能够从外部直接绑定该值。
What excatly do you want to do?
You can't bind to a standard property. If you want to bind you should use a dependency property.
Inside the UserControl:
To bind directly to a DateTimeValue is not possible because there is no converter available for string->DateTime so you have to write an IValueConverter and specify this in your binding.
From outside of course you should be able to bind the value directly.