文件的ListView,改名字的ListView直接,WPF C#MVVM [英] File ListView, change name direct in ListView, WPF C# MVVM

查看:183
本文介绍了文件的ListView,改名字的ListView直接,WPF C#MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的小程序在那里我有一个列表视图特定的文件夹和某些类型的所有文件。我想改变直接在ListView控件的文件的名称。

我和文本框在一列作了ListView和文件的名称写里面的文本框。我现在可以改变文本框的名字,但不会改变文件名。如何做到在ListView和方法文本框之间的联系,这将是变革的名字吗?是的,我这里一点丢失。我是pretty在MVVM WPF新鲜。

的ListView的XAML我的code:

 < ListView控件名称=lvfilesGrid.Column =0的ItemsSource ={结合fileslist}的SelectionMode =单的SelectedItem ={结合SelectedFiles}的DataContext = {结合}HEIGHT =140>
   < ListView.View>
      < GridView的X:名称=gridFiles>
           < GridViewColumn>
                < GridViewColumn.CellTemplate>
                     <&DataTemplate的GT;
                          < checkbox标签={绑定ID}=器isChecked{绑定的RelativeSource = {的RelativeSource AncestorType = {X:类型ListViewItem的}},路径= IsSelected}/>
                     < / DataTemplate中>
                < /GridViewColumn.CellTemplate>
             < / GridViewColumn>
           < GridViewColumn X:NAME =文件名标题=名称>                 < GridViewColumn.CellTemplate>
                      <&DataTemplate的GT;
                          <文本框的文本={绑定文件名,模式=双向,UpdateSourceTrigger =的PropertyChanged}/>
                       < / DataTemplate中>
                 < /GridViewColumn.CellTemplate>
          < / GridViewColumn>
       < GridViewColumn X:NAME =FileCreated标题=创造DisplayMemberBinding ={结合FileCreated}WIDTH =自动/>
     < GridViewColumn X:NAME =FileChanged标题=更改DisplayMemberBinding ={结合FileChanged}WIDTH =自动/>
   < / GridView的>
 < /ListView.View>
< /&的ListView GT;

我的视图模型是:

 公共类文件:ViewModelBase
{
    私人诠释FILEID;
    私人字符串的文件名;
    私人字符串filecreated;
    私人字符串filechanged;    公共字符串文件名
    {
        {返回的文件名;}
        组
        {
            文件名=值;
            NotifyPropertyChanged(文件名);
        }
    }


解决方案

在您的视图模型:

  //你应该存储文件扩展名的地方
私人字符串位置;
私人字符串fileExtension;私人字符串_filename;
公共字符串文件名
{
    {返回this._fileName;
    组
    {
        如果(_filename!=值)
        {
            尝试
            {
                无功源= Path.Combine(位置,_filename + + fileExtension。);
                VAR的目标= Path.Combine(位置值+ + fileExtension。);                //如果你存储与您的圆点文件扩展名,应删除从Path.Combine点                File.Move(源,目的);
                _filename =价值;
                // InvokePropertyChanged(文件名);如果需要的话
            }
            赶上(例外五)
            {
                MessageBox.Show(E.ToString(),错误,MessageBoxButton.OK,MessageBoxImage.Error);
            }
        }
    }
}

I made little program where I have all files of certain folder and certain type in one listview. I like to change name of file directly inside of ListView.

I made ListView with textboxes in one column and name of the file is written inside textbox. I can change name in textbox now, but will not change file name. How to do this connection between textbox in ListView and Method which will be change name? Yes I am little lost here. I am pretty fresh in MVVM WPF.

My XAML code of ListView:

<ListView Name="lvfiles" Grid.Column="0" ItemsSource="{Binding fileslist}" SelectionMode="Single" SelectedItem="{Binding SelectedFiles}" DataContext="{Binding }" Height="140">
   <ListView.View>
      <GridView x:Name="gridFiles">
           <GridViewColumn>
                <GridViewColumn.CellTemplate>
                     <DataTemplate>
                          <CheckBox Tag="{Binding ID}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}"/>
                     </DataTemplate>
                </GridViewColumn.CellTemplate>
             </GridViewColumn>
           <GridViewColumn x:Name="FileName" Header="Name">

                 <GridViewColumn.CellTemplate>
                      <DataTemplate>
                          <TextBox  Text="{Binding FileName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  />
                       </DataTemplate>
                 </GridViewColumn.CellTemplate>
          </GridViewColumn>
       <GridViewColumn x:Name="FileCreated" Header="Created" DisplayMemberBinding="{Binding FileCreated}" Width="Auto"/>
     <GridViewColumn x:Name="FileChanged" Header="Changed" DisplayMemberBinding="{Binding FileChanged}" Width="Auto"/>
   </GridView>
 </ListView.View>
</ListView>

My ViewModel is:

 public class Files : ViewModelBase
{
    private int fileid;
    private string filename;
    private string filecreated;
    private string filechanged;

    public string FileName
    {
        get { return filename;}
        set
        {
            filename = value;
            NotifyPropertyChanged("FileName");
        }
    }

解决方案

In your view model:

// you should store file extension somewhere
private string location;
private string fileExtension;

private string _fileName;
public string FileName
{
    get { return this._fileName; 
    set 
    {
        if (_fileName != value)
        {
            try
            {
                var source = Path.Combine(location, _fileName + "." + fileExtension);
                var destination = Path.Combine(location, value + "." + fileExtension);

                //if you store your file extension with dot, you should remove the dot from Path.Combine

                File.Move(source, destination);
                _fileName = value;
                // InvokePropertyChanged("FileName"); if needed
            }
            catch (Exception E)
            {
                MessageBox.Show(E.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}

这篇关于文件的ListView,改名字的ListView直接,WPF C#MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆