0

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");
        }
    }
Noam M
  • 3,156
  • 5
  • 26
  • 41
esispaned
  • 283
  • 5
  • 20

1 Answers1

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);
            }
        }
    }
}
keymusicman
  • 1,281
  • 1
  • 10
  • 20
  • I update my current ViewModel. Thanks for yours, I will try it. I didnt know if I can made direct in ViewModel or not. But your code will update name simultaneously - in real time? Because now will change for every later typed in (or not?). It is true that I dont want some Buttons to confirm it, but maybe wait for 2 seconds without any change? Or something similar? ... I try your code and yes its update every single letter. Is possible to update on the end? Yes I know that is a lot more complicated? Or not? – esispaned Aug 25 '15 at 10:50
  • 1
    To avoid FileName updating with every enterred character change UpdateSourceTrigger for your TextBox to LostFocus in xaml. Now it's "PropertyChanged" – keymusicman Aug 25 '15 at 11:04