WPF:将DateTime的绑定视图模型属性添加到ItemsControl中的Calendar内 [英] WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl

查看:207
本文介绍了WPF:将DateTime的绑定视图模型属性添加到ItemsControl中的Calendar内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WPF绑定的问题。我想绑定一个Months列表到ItemsControl,显示每个月的日历控件。但是每个渲染的日历显示DateTime.Now,而不是绑定的DateTimes。有人知道为什么会发生这种情况吗?



这是我迄今为止所做的:



MainWindow.xaml

 < Window x:Class =CalendarListTest.MainWindow
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
标题= MainWindowHeight =350Width =525>
< Grid>
< ItemsControl x:Name =calendarList>
< ItemsControl.ItemTemplate>
< DataTemplate>
< Calendar DisplayDate ={Binding CurrentDate}/>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ItemsControl>
< / Grid>



**集合被分配给ItemsSource **

  private void Window_Loaded(object sender,RoutedEventArgs e)
{
CalendarList list = new CalendarList();
list.Add(new CalendarMonth(){CurrentDate = DateTime.Parse(1.1.1979)});
list.Add(new CalendarMonth(){CurrentDate = DateTime.Parse(1.2.1979)});
list.Add(new CalendarMonth(){CurrentDate = DateTime.Parse(1.3.1979)});

calendarList.ItemsSource = list;
}

CalendarMonth ViewModel

  public class CalendarMonth 
{
private DateTime _currentDate;

public DateTime CurrentDate
{
get {return _currentDate; }
set {_currentDate = value; }
}

}

绑定到ItemsControl

  public class CalendarList:ObservableCollection< CalendarMonth> 
{
}

现在,结果是:





为什么会发生这种情况?



编辑:提供< Calendar DisplayDate ={Binding CurrentDate,Mode = OneWay}/ < / code>绑定工作。

解决方案

这个问题似乎与日历如何初始化DisplayDate属性有关。它目前是这样的:

  public Calendar(){
// ...
base .SetCurrentValueInternal(DisplayDateProperty,DateTime.Today);
}

看来尽管DisplayDate在绑定建立之前被初始化,它仍然会被推回到绑定源,就好像它被设置好了。这很可能是一个错误。



您可以使用以下方式解决问题:

 code> public class MyCalendar:Calendar {
public MyCalendar(){
this.ClearValue(DisplayDateProperty);
}
}

或者你可以稍后建立绑定即加载日历)使用事件处理程序或附加行为。


i have a problem with WPF Binding. I want to bind a list of Months to a ItemsControl that shows a Calendar Control for each month. But each rendered Calendar shows DateTime.Now,not the bound DateTimes. Does anyone know why this is happening?

This is what i have so far:

The MainWindow.xaml

<Window x:Class="CalendarListTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ItemsControl x:Name="calendarList">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Calendar DisplayDate="{Binding CurrentDate}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

** The place where the collection is assigned to the ItemsSource**

        private void Window_Loaded( object sender, RoutedEventArgs e )
    {
        CalendarList list = new CalendarList( );
        list.Add( new CalendarMonth( ) { CurrentDate = DateTime.Parse( "1.1.1979" ) } );
        list.Add( new CalendarMonth( ) { CurrentDate = DateTime.Parse( "1.2.1979" ) } );
        list.Add( new CalendarMonth( ) { CurrentDate = DateTime.Parse( "1.3.1979" ) } );

        calendarList.ItemsSource = list;
    }

The CalendarMonth ViewModel:

public class CalendarMonth
{
    private DateTime _currentDate;

    public DateTime CurrentDate
    {
        get { return _currentDate; }
        set { _currentDate = value; }
    }

}

And the Collection to bind to the ItemsControl:

public class CalendarList : ObservableCollection<CalendarMonth>
{
}

Now, the result:

Why is this happening?

edit: When providing <Calendar DisplayDate="{Binding CurrentDate, Mode=OneWay}" /> the binding works.

解决方案

The issue appears to be with how the Calendar initializes the DisplayDate property. It currently does it like this:

public Calendar() {
    // ...
    base.SetCurrentValueInternal(DisplayDateProperty, DateTime.Today);
}

It appears that even though the DisplayDate is being initialized before the binding is established, it will still be pushed back to the binding source as if it were set after. This is most likely a bug.

You can work around it using something like:

public class MyCalendar : Calendar {
    public MyCalendar() {
        this.ClearValue(DisplayDateProperty);
    }
}

Or you could establish the binding at a later time (i.e. when the Calendar is loaded) using an event handler or attached behavior.

这篇关于WPF:将DateTime的绑定视图模型属性添加到ItemsControl中的Calendar内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆