File ListView, change name direct in ListView, WPF C# MVVM

徘徊边缘 提交于 2019-12-13 07:53:06

问题


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");
        }
    }

回答1:


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);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/32200155/file-listview-change-name-direct-in-listview-wpf-c-sharp-mvvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!