当绑定到viewModel属性时

发布于 2025-01-18 20:55:59 字数 1722 浏览 4 评论 0原文

我有一个带有 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:“年、月和日参数描述了无法表示的日期时间。”

UserControl with绑定值

我对这个主题很陌生,但我假设在控件初始化时绑定尚未解析还是什么?

你会如何正确地做到这一点?

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"/>

UserControl with static values

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."

UserControl with bound values

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2025-01-25 20:55:59

显然,您的绑定行不通。

原因是您已明确设置usercontroldataContext本身,从而有效防止其继承dataContext具有其特性。

Don't do this:

this.DataContext = this;

You should also raise a property changed event for the DayOfWeek property whenever the Day, Month or Year< /code>属性已设置,但我在代码中没有看到propertyChangedCallback

Apparently your bindings don't work.

The reason is that you have explicitly set the DataContext of the UserControl to itself which effectively prevents it from inheriting the DataContext from its parent element and bind to the properties of it.

Don't do this:

this.DataContext = this;

You should also raise a property changed event for the DayOfWeek property whenever the Day, Month or Year property is set but I see no PropertyChangedCallback in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文