gpt4 book ai didi

c# - 绑定(bind)在 ListView 中不起作用

转载 作者:行者123 更新时间:2023-11-30 21:20:55 24 4
gpt4 key购买 nike

我有一个名为 DataPicker 的 wpf 控件,它有一个名为 SelectedDate 的依赖属性。

在简单的情况下,它运行良好,但有一种情况绑定(bind)失败,我不明白为什么:当我尝试将它绑定(bind)到 ListView 中时。

比如我有类(实现了INotifyPropertyChanged)

public class TestClass : INotifyPropertyChanged
{
public string Name { get; set; }
public DateTime Date { get; set; }
}

并尝试像这样绑定(bind)样本集合

public ObservableCollection<TestClass> Items { get; set; }

其中有一个元素。

绑定(bind)看起来像

 <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">
<Grid>
<ListView ItemsSource="{Binding ElementName=this, Path=Items}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<local:DatePicker SelectedDate="{Binding Date, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>

并且 Name 属性工作正常。

在我的 DatePicker 中,日期值是这样显示的:

<TextBox x:Name="PART_TextBox">
<TextBox.Text>
<Binding Path="SelectedDate"
RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:DatePicker}}"
Mode="TwoWay"
Converter="{StaticResource DateTimeConverter}"
ConverterParameter="d">
</Binding>
</TextBox.Text>
</TextBox>

知道为什么会发生这种情况吗?

DatePicker 类的更多代码:(我宁愿错过一些我需要的特定属性以保持代码量不要太大)

[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) =>
{
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
}

最佳答案

确保您的属性引发 INotifyPropertyChanged 事件:

public class TestClass : INotifyPropertyChanged
{
private DateTime date;

public DateTime Date
{
get { return date; }
set { date = value; NotifyPropertyChanged("Date"); }
}

private void NotifyPropertyChanged(string info)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}

并使您对日期的绑定(bind)也是双向的:

<local:DatePicker SelectedDate="{Binding Date, Mode=TwoWay}"/>

关于c# - 绑定(bind)在 ListView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2998358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com