C# 装订不';不能在ListView中工作
我有一个名为DataPicker的wpf控件,它有一个名为SelectedDate的依赖属性 在简单的情况下,它工作得很好,但有一种情况是绑定失败,我不明白为什么:当我尝试在ListView中绑定它时 例如,我有一个类(实现了INotifyPropertyChanged) 并尝试将样本集合绑定为C# 装订不';不能在ListView中工作,c#,wpf,binding,controls,dependency-properties,C#,Wpf,Binding,Controls,Dependency Properties,我有一个名为DataPicker的wpf控件,它有一个名为SelectedDate的依赖属性 在简单的情况下,它工作得很好,但有一种情况是绑定失败,我不明白为什么:当我尝试在ListView中绑定它时 例如,我有一个类(实现了INotifyPropertyChanged) 并尝试将样本集合绑定为 public ObservableCollection<TestClass> Items { get; set; } 确保属性引发INotifyPropertyChanged事件: pub
public ObservableCollection<TestClass> Items { get; set; }
确保属性引发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));
}
}
}
并以两种方式对日期进行约束:
<local:DatePicker SelectedDate="{Binding Date, Mode=TwoWay}"/>
检查输出窗口是否存在任何数据绑定错误 可能的错误:
- DataContext设置不正确
Items不是窗口或ViewModel类的属性吗ListView ItemsSource=“{Binding ElementName=this,Path=Items}
- 问题解决了
它不是在错误的绑定或其他困难和不均匀的东西中,而是在遗留代码中,程序员在代码中的某个地方犯了一个错误
谢谢大家!问题到底出在哪里?是日期选择器中没有显示该日期,还是日期选择器中选定的日期没有传播到TestClass对象?在后一种情况下,您可能需要向日期选择器绑定添加Mode=TwoWay(即-SelectedDate=“{binding date,Mode=TwoWay}"数据采集器中的SelectedDate无法访问我的TestClass对象,是的,模式设置为双向。你在哪里修改日期采集器的源代码?或者你只是为了我们的方便才发布它?是的,我只是发布了更多信息;我已经提到,我只是试图减少他们在POST中抛出的代码量,当然,我刚刚发布了不要在帖子中提及减少ITI中代码大小的问题。您的类型似乎不匹配。您在控件中使用可为null的类型,在对象中使用不可为null的日期时间。此外,请尝试使用普通的PropertyMetadata对象。我的经验是FrameworkPropertyMetadata对象的行为有时确实非常奇怪。DataContext设置正确(集合的项绑定到列表框的项资源),这只是一个简单的测试应用程序,属性是Window类的一部分,问题其实是更深层次的,我认为不,我在输出窗口中没有看到数据绑定错误,这太疯狂了)我们不是都会不时出错吗;)是的)但我花了大约两天的时间来解决这个问题,变得相当疯狂)
<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>
[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
}
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));
}
}
}
<local:DatePicker SelectedDate="{Binding Date, Mode=TwoWay}"/>