[英]Refresh ListView item on property changed
I have a listView like this : 我有一个这样的listView:
<ListView Grid.Row="1" x:Name="itemsListView"
BorderBrush="White" HorizontalAlignment="Stretch"
ItemsSource="{Binding Path=Items}"
SelectedItem="{Binding Path=ActualItem}">
<ListView.View>
<GridView>
<GridViewColumn Header="{x:Static p:Resources.STATUS}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<local:StatusElement State="{Binding Path=Status,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
Height="20"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{x:Static p:Resources.NAME}"
DisplayMemberBinding="{Binding Path=Name,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
</GridView>
</ListView.View>
</ListView>
It binds a list of items named Items
with many fields. 它将具有许多字段的名为
Items
的项列表绑定在一起。 A thread parse the items and update the fields when it finishes. 线程解析项目并在完成时更新字段。 I call method
OnPropertyChanged
when fields are updated. 当字段更新时,我调用方法
OnPropertyChanged
。 It works fine for all fields except the one using my UserControl local:StatusElement
. 对于除使用我的UserControl
local:StatusElement
字段以外的所有字段,它都local:StatusElement
。 I've try to display my STATUS like the NAME, it's refreshes correctly but with local:StatusElement
there is no refresh. 我试着像NAME一样显示我的STATUS,它会正确刷新,但是使用
local:StatusElement
没有刷新。 breakpoints on get/set for StatusElement.State are never reached. 永远不会达到获取/设置StatusElement.State的断点。
My userControl : 我的userControl:
<UserControl ...
x:Name="mainControl">
<Grid Name="LabelGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Name="MyImage"
Source="{Binding Source, Source={StaticResource MyImage}}"
Width="{Binding Height, ElementName=mainControl}"
Height="{Binding Height, ElementName=mainControl}"/>
<Label Grid.Column="1" Name="statusLabel"/>
</Grid>
</UserControl>
and: 和:
public partial class StatusElement : UserControl
{
// Dependency property backing variables
public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State",
typeof(String), typeof(StatusElement), new UIPropertyMetadata(null));
private string _state = "";
public String State
{
get
{
return _state;
}
set
{
_state = value;
RefreshState();
}
}
private void RefreshState()
{
switch (State)
{
case "":
MyImage.Visibility = Visibility.Hidden;
break;
default:
MyImage.Visibility = Visibility.Visible;
break;
}
statusLabel.Content = State;
}
public StatusElement()
{
InitializeComponent();
RefreshState();
}
}
Why the Content of my statusLabel doesn't refresh ? 为什么statusLabel的内容没有刷新?
Your definition of the State
dependency property is wrong. 您对
State
依赖项属性的定义是错误的。
It has to look like shown below, where the CLR property wrapper must call the GetValue
and SetValue
methods of the DependencyObject that owns the property. 它必须看起来如下所示,其中CLR属性包装器必须调用拥有该属性的DependencyObject的
GetValue
和SetValue
方法。
public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
"State",
typeof(string),
typeof(StatusElement),
new PropertyMetadata(null, (o, e) => ((StatusElement)o).RefreshState()));
public string State
{
get { return (string)GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}
Note the second argument to the PropertyMetadata
constructor. 请注意
PropertyMetadata
构造函数的第二个参数。 It is a static PropertyChangedCallback
, implemented as lambda expression. 它是一个静态的
PropertyChangedCallback
,实现为lambda表达式。
Your class doesnt implement the INotifyPropertyChanged event. 您的类未实现INotifyPropertyChanged事件。 Implement it for the update to happen.
实施它以进行更新。
Notifies clients that a property value has changed.
通知客户端属性值已更改。
public partial class StatusElement : UserControl,INotifyPropertyChanged
{
....
public event PropertyChangedEventHandler PropertyChanged;
private void RefreshState([CallerMemberName]string prop = "")
{
switch (State)
{
case "":
MyImage.Visibility = Visibility.Hidden;
break;
default:
MyImage.Visibility = Visibility.Visible;
break;
}
statusLabel.Content = State;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.