[英]Binding to a dictionary in Silverlight with INotifyPropertyChanged
[英]Silverlight: Problem with INotifyPropertyChanged
我遇到一些困难。 我正在构建Windows Phone 7应用程序。
我有一些实现可编辑列表框的类,可以切换到编辑模式并允许用户添加或删除项目。
EditableListBox.xaml
和后面的代码是ListBox
的包装。
EditableListBoxController<T>
是控制行为的类。 我将其设为通用以提高类型安全性,尽管这意味着我需要将其分为一个单独的类,而不是在EditableListBox.xaml.cs
中实现功能,这有点麻烦。
EditableListItem
是列表中的单个项目。 它包装了要添加或删除的项目,并负责知道该项目是否属于列表,是否应显示“编辑”图标等。
EditableListBox.xaml:
<Grid x:Name="LayoutRoot">
<ListBox x:Name="ContentListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid ManipulationCompleted="Grid_ManipulationCompleted">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="{Binding IconSource}"
Grid.Column="0"
Width="96"
Height="96"
VerticalAlignment="Center"
Visibility="{Binding Editing, Converter={StaticResource visibilityConverter}}"
/>
<TextBlock Text="{Binding Name}"
Grid.Column="1"
Foreground="{Binding Enabled, Converter={StaticResource enabledConverter}}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
在数据模板中,编辑图标绑定到EditableListItem.Editing
:
public bool Editing
{
get
{
return _parentListController.Editing;
}
}
EditableListItem.Editing
绑定到EditableListBoxController<T>.Editing
:
private bool _editing;
public bool Editing
{
get
{
return _editing;
}
set
{
_editing = value;
NotifyPropertyChanged("Editing");
refreshItemsSource();
}
}
EditableListItem
实现INotifyPropertyChanged
,因此它可以适当地更新UI。 它在父控制器上侦听PropertyChanged
,因此可以在需要时更新Editing
:
private EditableListBoxController<T> _parentListController;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public EditableListItem(T item, EditableListBoxController<T> parentListController)
{
_parentListController = parentListController;
_parentListController.PropertyChanged += PropertyChanged;
_parentListController.PropertyChanged += new PropertyChangedEventHandler(_parentListBox_PropertyChanged);
this.ContainedItem = item;
}
void _parentListBox_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(sender, e);
}
}
EditableListItem
的构造函数在这里被调用:
private void refreshItemsSource()
{
if (_sectionsSource == null)
{
return;
}
_editableListBox.ContentListBox.ItemsSource = (from section in _sectionsSource
where Editing || section.Enabled
select new EditableListItem<T>(section, this)).ToList();
}
但是由于某种原因,这是行不通的。 当我更改控制器的Editing
属性时, PropertyChanged
事件处理程序为null。 因此,该事件不会被触发。 这很奇怪,因为我进入调试器并看到EditableListItem<T>
正在注册其事件处理程序,但是以后似乎以某种方式取消了注册?
我在这里做错了什么?
问题可能是您正在绑定到EditableListItem.Editing,并且没有引发任何事件来指示这种情况正在改变,只有EditableListBoxController
的基础属性正在改变,但是不受ListBox
监视。
我想说的是,您应该拥有EditableListItem
实现INotifyPropertyChanged
。 然后, EditableListItem
应该将其自身连接到EditableListBoxController
的PropertyChange事件。
当控制器触发事件时, EditableListItem
将处理该事件并触发其自身的PropertyChanged' event indicating that the
Editing属性已更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.