刷新属性更改后的列表视图项
本文关键字:列表 视图 属性 刷新 | 更新日期: 2023-09-27 18:02:08
我有一个这样的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>
它将一个名为Items
的项目列表与许多字段绑定在一起。线程解析条目并在完成时更新字段。当字段更新时,我调用方法OnPropertyChanged
。它适用于所有字段,除了使用我的UserControl local:StatusElement
。我试着像NAME一样显示我的状态,它正确刷新,但local:StatusElement
没有刷新。在获取/设置StatusElement的断点。状态永远不会到达。
My 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>
:
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();
}
}
为什么我的statusLabel的内容不刷新
您对State
依赖属性的定义是错误的。
它必须看起来像下面所示,其中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); }
}
注意PropertyMetadata
构造函数的第二个参数。它是一个静态的PropertyChangedCallback
,实现为lambda表达式。
你的类没有实现INotifyPropertyChanged事件。实现它以使更新发生
通知客户端属性值发生了变化。
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));
}
}
}