Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding doesn't work in ListView
    primarykey
    data
    text
    <p>I have a wpf control named DataPicker which has a dependency property named SelectedDate.</p> <p>In simple cases it works well but there is one case where binding fails and I can't understand why: when i try to bind it inside a ListView. </p> <p>For example, I have class (INotifyPropertyChanged is implemented)</p> <pre><code>public class TestClass : INotifyPropertyChanged { public string Name { get; set; } public DateTime Date { get; set; } } </code></pre> <p>and try to bind sample collection like </p> <pre><code>public ObservableCollection&lt;TestClass&gt; Items { get; set; } </code></pre> <p>which has one element in it.</p> <p>Binding looks like</p> <pre><code> &lt;Window x:Class="Neverov.Test.Window1" x:Name="this" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Neverov.Framework;assembly=Neverov.Framework"&gt; &lt;Grid&gt; &lt;ListView ItemsSource="{Binding ElementName=this, Path=Items}"&gt; &lt;ListView.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding Name}"/&gt; &lt;local:DatePicker SelectedDate="{Binding Date, Mode=TwoWay}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListView.ItemTemplate&gt; &lt;/ListView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>and Name property works fine.</p> <p>inside my DatePicker date value is shown this way:</p> <pre><code>&lt;TextBox x:Name="PART_TextBox"&gt; &lt;TextBox.Text&gt; &lt;Binding Path="SelectedDate" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:DatePicker}}" Mode="TwoWay" Converter="{StaticResource DateTimeConverter}" ConverterParameter="d"&gt; &lt;/Binding&gt; &lt;/TextBox.Text&gt; &lt;/TextBox&gt; </code></pre> <p>any ideas why this could happen?</p> <p>More code of the DatePicker class: (some specific properties that I need I'll rather miss to keep code size not so large)</p> <pre><code>[TemplatePart(Name = PartPopup, Type = typeof(Popup))] [TemplatePart(Name = PartMonthBack, Type = typeof(ButtonBase))] [TemplatePart(Name = PartMonthForward, Type = typeof(ButtonBase))] [TemplatePart(Name = PartDates, Type = typeof(Selector))] [TemplatePart(Name = PartTextBox, Type = typeof(TextBox))] [TemplatePart(Name = PartCheckBox, Type = typeof(CheckBox))] [TemplatePart(Name = PartToggleButton, Type = typeof(ToggleButton))] public class DatePicker : Control, INotifyPropertyChanged { ... public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register("SelectedDate", typeof(DateTime?), typeof(DatePicker), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (sender, e) =&gt; { var datePicker = sender as DatePicker; if (datePicker != null) { var oldValue = e.OldValue as DateTime?; DateTime selectedDateForPopup = datePicker.SelectedDate ?? DateTime.Now; datePicker.CurrentlyViewedMonth = selectedDateForPopup.Month; datePicker.CurrentlyViewedYear = selectedDateForPopup.Year; datePicker.OnDateChanged(datePicker.SelectedDate, oldValue); var popup = datePicker.GetTemplateChild(PartPopup) as Popup; if (popup != null) popup.IsOpen = false; } })); ... //a lot more not so important code here } </code></pre>
    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.
    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