当绑定到viewModel属性时
我有一个带有 3 个依赖属性的简单 UserControl。日、月和年。
public static readonly DependencyProperty DayProperty = DependencyProperty.Register("Day", typeof(int), typeof(BoardPanel), new FrameworkPropertyMetadata(0));
public int Day
{
get { return (int)GetValue(DayProperty); }
set { SetValue(DayProperty, value); }
}
public static readonly DependencyProperty MonthProperty = DependencyProperty.Register("Month", typeof(int), typeof(BoardPanel), new FrameworkPropertyMetadata(0));
public int Month
{
get { return (int)GetValue(MonthProperty); }
set { SetValue(MonthProperty, value); }
}
public static readonly DependencyProperty YearProperty = DependencyProperty.Register("Year", typeof(int), typeof(BoardPanel), new FrameworkPropertyMetadata(0));
public int Year
{
get { return (int)GetValue(YearProperty); }
set { SetValue(YearProperty, value); }
}
和另一个字符串属性 DayOfWeek。
public string DayOfWeek
{
get { return new DateTime(Year, Month, Day).DayOfWeek.ToString(); }
}
Day 和 DayOfWeek 属性绑定到 XAML 中的两个文本框。
当使用此用户控件并手动设置所有值时,一切正常。
<c:DatePanel Day="1" Month="4" Year="2022"/>
绑定到我的 ViewModel 中的属性时,它会抛出异常并显示月份和年份为 0。
<c:DatePanel Day="2" Month="{Binding Month}" Year="{Binding Year}"/>
System.ArgumentOutOfRangeException:“年、月和日参数描述了无法表示的日期时间。”
我对这个主题很陌生,但我假设在控件初始化时绑定尚未解析还是什么?
你会如何正确地做到这一点?
I have a simple UserControl with 3 Dependency Properties. Day, Month and Year.
public static readonly DependencyProperty DayProperty = DependencyProperty.Register("Day", typeof(int), typeof(BoardPanel), new FrameworkPropertyMetadata(0));
public int Day
{
get { return (int)GetValue(DayProperty); }
set { SetValue(DayProperty, value); }
}
public static readonly DependencyProperty MonthProperty = DependencyProperty.Register("Month", typeof(int), typeof(BoardPanel), new FrameworkPropertyMetadata(0));
public int Month
{
get { return (int)GetValue(MonthProperty); }
set { SetValue(MonthProperty, value); }
}
public static readonly DependencyProperty YearProperty = DependencyProperty.Register("Year", typeof(int), typeof(BoardPanel), new FrameworkPropertyMetadata(0));
public int Year
{
get { return (int)GetValue(YearProperty); }
set { SetValue(YearProperty, value); }
}
and another string Property DayOfWeek.
public string DayOfWeek
{
get { return new DateTime(Year, Month, Day).DayOfWeek.ToString(); }
}
The Day and DayOfWeek Properties are bound to two Textboxes in XAML.
When using this UserControl and setting all values manually everything works fine.
<c:DatePanel Day="1" Month="4" Year="2022"/>
When binding these to Properties from my ViewModel however it throws an exeption and shows that Month and Year are 0.
<c:DatePanel Day="2" Month="{Binding Month}" Year="{Binding Year}"/>
System.ArgumentOutOfRangeException: "Year, Month, and Day parameters describe an un-representable DateTime."
I'm quite new to this topic but I'd assume that the binding is not resolved yet at the time the control gets initialized or something?
How would you do this correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,您的绑定行不通。
原因是您已明确设置
usercontrol
的dataContext
本身,从而有效防止其继承dataContext
具有其特性。Don't do this:
You should also raise a property changed event for the
DayOfWeek
property whenever theDay
,Month
orYear< /code>属性已设置,但我在代码中没有看到
propertyChangedCallback
。Apparently your bindings don't work.
The reason is that you have explicitly set the
DataContext
of theUserControl
to itself which effectively prevents it from inheriting theDataContext
from its parent element and bind to the properties of it.Don't do this:
You should also raise a property changed event for the
DayOfWeek
property whenever theDay
,Month
orYear
property is set but I see noPropertyChangedCallback
in your code.