Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl
    primarykey
    data
    text
    <p>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?</p> <p>This is what i have so far:</p> <p><strong>The MainWindow.xaml</strong></p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;ItemsControl x:Name="calendarList"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Calendar DisplayDate="{Binding CurrentDate}" /&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>** The place where the collection is assigned to the ItemsSource**</p> <pre><code> 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; } </code></pre> <p><strong>The CalendarMonth ViewModel</strong>:</p> <pre><code>public class CalendarMonth { private DateTime _currentDate; public DateTime CurrentDate { get { return _currentDate; } set { _currentDate = value; } } } </code></pre> <p><strong>And the Collection to bind to the ItemsControl</strong>:</p> <pre><code>public class CalendarList : ObservableCollection&lt;CalendarMonth&gt; { } </code></pre> <p>Now, the result:</p> <p><img src="https://i.stack.imgur.com/CsQ97.png" alt="enter image description here"></p> <p>Why is this happening?</p> <p>edit: When providing <code>&lt;Calendar DisplayDate="{Binding CurrentDate, Mode=OneWay}" /&gt;</code> the binding works. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload