您的位置:首页 > 其它

WPF ListView 分组数据绑定 TreeView递归数据绑定

2013-08-07 17:47 831 查看
WPF窗体代码:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converter="clr-namespace:FileEncryptUIWpf.Converters"
xmlns:models="clr-namespace:FileEncryptUIWpf.Model"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="FileEncryptUIWpf.MainWindow"
Title="MainWindow" Height="408" Width="846">
<Window.Resources>
<CollectionViewSource x:Key="viewSource">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Directory"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="ItemCheckboxDataTemple">
<Grid>
<CheckBox IsChecked="{Binding IsUIChecked, Mode=TwoWay}" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="ItemImageDataTemple">
<Grid>
<Image Margin="1" Width="33" Height="15" Source="/FileEncryptUIWpf;component/Images/new.png" Stretch="None" />
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="249*" />
<RowDefinition Height="62*" />
</Grid.RowDefinitions>
<GroupBox x:Name="groupBox1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="154" />
<ColumnDefinition Width="539*" />
<ColumnDefinition Width="119" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="40" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Grid.Row="2" Name="grid2" Grid.ColumnSpan="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<TreeView Name="treeView1" HorizontalAlignment="Stretch" SelectedItemChanged="treeView1_SelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="models:LocalDirectoryInfoModel" ItemsSource="{Binding ListDirectoryInfo}" >

<StackPanel Orientation="Horizontal">
<Image Source="Images/folder1.png" Stretch="Fill" Width="16" Height="16" Margin="5,0"/>
<TextBlock Grid.Column="1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding DirectoryName}" VerticalAlignment="Center" Margin="10,0"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<GridSplitter Name="gridSplitter1" Width="3" HorizontalAlignment="Center" Margin="0,7,538,0" Grid.Column="1" />
<ListView x:Name="listView1" Grid.Column="1">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<DockPanel>
<CheckBox Content="全选" Margin="10,0,0,0" Click="CheckBox_Click" Tag="{Binding Name}"></CheckBox>
<TextBlock FontWeight="Bold" Text="{Binding Name}" Margin="20,0,0,0" />
<TextBlock FontWeight="Bold" Text="{Binding ItemCount}" Margin="20,0,0,0" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
<ListView.View>
<GridView>
<GridViewColumn CellTemplate="{DynamicResource ItemCheckboxDataTemple}" />
<GridViewColumn CellTemplate="{DynamicResource ItemImageDataTemple}" Width="53" />
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="名称" Width="300" />
<GridViewColumn DisplayMemberBinding="{Binding Encrypted}" Header="已加密" />
<GridViewColumn DisplayMemberBinding="{Binding LocalFileLastUpdateTimeString}" Header="上一次修改时间" />
<GridViewColumn DisplayMemberBinding="{Binding FileType}" Header="类型" />
<GridViewColumn DisplayMemberBinding="{Binding LengthString}" Header="大小" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Grid>
</GroupBox>
</Grid>
</Window>

Model代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace FileEncryptUIWpf.Model
{
class BaseModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyValueChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Collections.ObjectModel;

namespace FileEncryptUIWpf.Model
{
class LocalDirectoryInfoModel : BaseModel
{
public string DirectoryName { get; private set; }

public ObservableCollection<LocalDirectoryInfoModel> ListDirectoryInfo { get; private set; }

public ObservableCollection<LocalConfigFileInfoModel> ListConfigFileInfo { get; private set; }

public LocalDirectoryInfoModel(DirectoryInfo dirInfo)
{
this.DirectoryName = dirInfo.Name;

this.ListConfigFileInfo = new ObservableCollection<LocalConfigFileInfoModel>();
this.ListDirectoryInfo = new ObservableCollection<LocalDirectoryInfoModel>();
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;

namespace FileEncryptUIWpf.Model
{
class LocalConfigFileInfoModel : BaseModel
{
#region Properties

private string _name;

public string Name
{
get { return _name; }
}

public string AbsolutePath
{
get { return this._directory + Path.DirectorySeparatorChar + this._name; }
}

private string _directory;

public string Directory
{
get { return _directory; }
}

private bool _hadEncrypted;

/// <summary>
/// 已加密
/// </summary>
public bool Encrypted
{
get { return _hadEncrypted; }
set
{
if (value != _hadEncrypted)
{
_hadEncrypted = value;
NotifyPropertyValueChanged("Encrypted");
}
}
}

/// <summary>
/// 后缀名
/// </summary>
private string _extension;

public string Extension
{
get { return _extension; }
}

private string _fileType;//TODO: 该值应该从注册表里取

/// <summary>
///
/// </summary>
public string FileType
{
get { return _fileType; }
}

private DateTime _localFileLastUpdateTime;

/// <summary>
/// 本地文件上一次更新时间
/// </summary>
public DateTime LocalFileLastUpdateTime
{
get { return _localFileLastUpdateTime; }
private set
{
if (value != _localFileLastUpdateTime)
{
_localFileLastUpdateTime = value;
NotifyPropertyValueChanged("LocalFileLastUpdateTime");
NotifyPropertyValueChanged("LocalFileLastUpdateTimeString");
NotifyPropertyValueChanged("IsNew");
}
}
}

public string LocalFileLastUpdateTimeString
{
get { return _localFileLastUpdateTime.ToString(); }
}

private DateTime _lastSaveToListTime;

/// <summary>
/// 上一次保存到配置文件清单的时间
/// </summary>
public DateTime LastSaveToListTime
{
get { return _lastSaveToListTime; }
set { _lastSaveToListTime = value; }
}

/// <summary>
/// 表示该配置文件为新添加文件,或者在上一次保存清单后又后被修改过
/// </summary>
public bool IsNew
{
get
{
return LocalFileLastUpdateTime > LastSaveToListTime;
}
}

private bool _isUIChecked;

public bool IsUIChecked
{
get { return _isUIChecked; }
set
{
if (value != _isUIChecked)
{
_isUIChecked = value;
NotifyPropertyValueChanged("IsUIChecked");
}
}
}

private long _length;

public long Length
{
get { return _length; }
set
{
if (value != Length)
{
_length = value;
NotifyPropertyValueChanged("Length");
NotifyPropertyValueChanged("LengthString");
}
}
}

public string LengthString
{
get
{
return (Length / 1024.0).ToString("0.0") + " KB";
}
}

#endregion

public LocalConfigFileInfoModel(FileInfo info)
{
this._name = info.Name;
this._directory = info.DirectoryName;
this.LocalFileLastUpdateTime = info.LastWriteTime;
this._extension = info.Extension;
this._fileType = info.Extension;
this.Deleted = !info.Exists;
this.Length = info.Length;

}

public override int GetHashCode()
{
return this.AbsolutePath.GetHashCode();
}

public override bool Equals(object obj)
{
LocalConfigFileInfoModel model = obj as LocalConfigFileInfoModel;
if (model == null)
{
return false;
}

return (this._directory.Equals(model._directory) && this._name.Equals(model._name));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: