我正在构建文件资源管理器,当用户使用TreeView触发此事件时。其中填充绑定到名为ObservableCollection<T>
的ListView的File_List
.FillCollection方法确实用正确的信息填充FileList。
我已经将这个从填充c#更改为绑定,因为我认为它更容易维护。谢谢你的时间。有this告诉我使用INotifyPropertyChanged
方法进行刺穿RaisedPropertyChanged
。
private void folderItems_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
try
{
TreeViewItem item = (TreeViewItem)folderItems.SelectedItem;
FileAttributes attr = File.GetAttributes(item.Tag.ToString());
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
_ViewModel.FileList = FillFileModel.FillCollection(item.Tag.ToString());
}
else
{
//Do something else
}
}
catch (Exception) { }
}
FillFileModel类
public static ObservableCollection<Files> FillCollection(string Path)
{
ObservableCollection<Files> files = new ObservableCollection<Files>();
var searchDirectory = new DirectoryInfo(Path);
try
{
Files getFile;
foreach (var file in searchDirectory.GetFiles())
{
getFile = new Files
{
FileName = file.Name,
FileSize = file.Length.ToString("#,##0 KB"),
DateModified = file.LastWriteTime,
FileType = getFileType(file.FullName)
};
files.Add(getFile);
}
}
catch (Exception)
{
}
return files;
}
绑定属性:
public ObservableCollection<Files> FileList
{
get { return _FileList; }
set
{
if (_FileList!=value)
{
_FileList = value;
RaisedPropertyChanged("FileList");
}
}
}
RaisedPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisedPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
handler(this, args);
}
}
**XAML:**
<Window.Resources>
<uc:ucNav x:Key="Nav"/>
<uc:FileExplorerViewModel x:Key="viewModel"/>
<local:FileImageConverter x:Key="ImageConverter"/>
</Window.Resources>
<ListView Width="580" Margin="205,32,9,10" x:Name="File_List" ItemsSource="{Binding Path=FileList}">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="Name" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Converter={StaticResource ImageConverter}}" Height="20" Width="20" Stretch="Fill"/>
<TextBlock x:Name="file_Name" Text="{Binding FileName}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="150">
<GridViewColumnHeader>
<TextBlock Text="Date Modified" />
</GridViewColumnHeader>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Date_Modified" Text="{Binding DateModified}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="150">
<GridViewColumnHeader>
<TextBlock Text="File Type" />
</GridViewColumnHeader>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="File_Type" Text="{Binding FileType}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="130">
<GridViewColumnHeader>
<TextBlock Text="File Size" />
</GridViewColumnHeader>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="File_Size" Text="{Binding FileSize}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
答案 0 :(得分:0)
听起来您的DataContext设置不正确,因为您发布的其他代码看起来是正确的。
在点击处理程序中放置一个断点,然后仔细检查File_List.DataContext == _ViewModel
DataContext
通常从父控件继承,或者如果这是最顶层的窗口,则可以从代码隐藏中设置它。在极少数情况下,您也可以绑定或设置值,但通常不建议这样做,因为它可能会导致混淆和调试问题。