我有我的对象集合:
public class Data
{
string name {get; set;}
int progres {get; set;}
}
public ObservableCollection<Data> dataFiles { get; set; }
我的ListView:
<ListView Name="lvDataFiles"
ItemsSource="{Binding dataList}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Foreground" Value="White"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.Resources>
<DataTemplate x:Key="MyDataTemplate">
<Grid Margin="-6">
<ProgressBar Name="prog" Maximum="100" Value="{Binding Progress}"
Width="{Binding Path=Width, ElementName=ProgressCell}"
Height="16" Margin="0" Foreground="#FF5591E8" Background="#FF878889" />
<TextBlock Text="{Binding Path=Value, ElementName=prog, StringFormat={}{0}%}" VerticalAlignment="Center"
HorizontalAlignment="Center" FontSize="11" Foreground="White" />
</Grid>
</DataTemplate>
<ControlTemplate x:Key="ProgressBarTemplate">
<Label HorizontalAlignment="Center" VerticalAlignment="Center" />
</ControlTemplate>
</ListView.Resources>
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource ListViewHeaderStyle}">
<GridViewColumn Width="425" Header="File name" DisplayMemberBinding="{Binding FileName}" />
<GridViewColumn x:Name="ProgressCell" Width="50" Header="Progress"
CellTemplate="{StaticResource MyDataTemplate}" />
</GridView>
</ListView.View>
</ListView>
我的ListView
有2列:文件名和进度(包含进度条)
我的数据集合具有每隔几秒钟更改一次的Progress属性。
我的ListView
ProgressBar
是否有可能在每次特定对象(或多个同时......)发生变化时自动更新?
或者我需要查看我的收藏并更新?
答案 0 :(得分:3)
您的Data类必须从INotifyPropertyChanged继承,添加NotifyPropertyChange方法并为每个setter调用它。
public class Data : INotifyPropertyChanged
{
private string _name;
public string name
{
get { return _name; }
set
{
_name= value;
NotifyPropertyChanged("name");
}
}
private int _progress;
public int progress
{
get { return _progress; }
set
{
_progress = value;
NotifyPropertyChanged("progress");
}
}
public event PropertyChangedEventHandler PropertyChanged;
virtual public void NotifyPropertyChange( string propertyName )
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}