WPF:在ItemsControl中将CalendarTime类型的viewmodel属性绑定到Calendar [英] WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl

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

问题描述

我有一个WPF绑定的问题。我想绑定一个月的列表到一个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
Title = 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> 
{
}

现在,结果:



>


$ b $



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

解决方案

问题似乎在于日历如何初始化DisplayDate属性。它目前是这样的:

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

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



您可以使用类似的工具:

  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:在ItemsControl中将CalendarTime类型的viewmodel属性绑定到Calendar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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