WPF MVVM Clear textbox bound to Listview SelectedI

2019-09-07 07:16发布

问题:

I am trying to learn about C# mvvm and wpf, so i decided to make a simple project(a book&readers managing application for a library) and now i am a little lost so i would really appreciate some help. In my view i have a listview displaying the readers, and a bunch of textboxes bound to the selected item of the listview(name, adress,etc). The listview is bound to an observable collection of readers created from a database table(Readers) with Linq2SQL.

Below the textboxes i have a few buttons (Clear/Add/Save/Delete).

The problems i am having are that the "selected item" of the listview, seems null if i try to execute some commands when i click the buttons.

  • when i click the clear button, if i have a breakpoint on the selected item(reader), it shows that it gets null/empty spaced, but the textboxes dont clear; If in the Clear method in use the SelectedReader property, and assign empty spaces to its attributes, the selected item in the listview also clears(because its bound to Reader's FullName property)

  • i have other problems on adding a new reader, and saving changes, and i assume its because of wrong linq queries but i hope i can fix the current ones and move on afterwards.

What should i do?

ViewModel

  public class MainWindowViewModel : ViewModelBase
{
    private Reader selectedReader;       

    private ObservableCollection<Reader> readerList;

    public MainWindowViewModel()
    {
        SelectedReader = new Reader()
                        {
                            FullName = "",
                            SerialNumber = "",
                            IdNumber = "",
                            Adress = "",
                            AltContactMethods = ""
                        };

        BookDBDataContext rdb = new BookDBDataContext();
        ReadersList = new ObservableCollection<Reader>(rdb.Readers);

        AddR = new TblQryCommand(AddToDb);
        EditR = new TblQryCommand(EditToDb);
        DeleteR = new TblQryCommand(DeleteFromDb);
        ClearR = new TblQryCommand(ClearReaderFields);
    }

    public TblQryCommand AddR { get; private set; }
    public TblQryCommand EditR { get; private set; }
    public TblQryCommand DeleteR { get; private set; }
    public TblQryCommand ClearR { get; private set; }

    //Reader List
    public ObservableCollection<Reader> ReadersList
    {
        get { return readerList; }
        set
        {
            if (readerList != value)
            {
                readerList = value;
                RaisePropertyChanged();
            }
        }
    }

    public Reader SelectedReader
    {
        get { return selectedReader; }
        set
        {
            if (selectedReader != value)
            {
                selectedReader = value;
                RaisePropertyChanged();
            }
        }
    }

    public void AddToDb()
    {
        BookDBDataContext db = new BookDBDataContext();
        Reader r = new Reader
        {
            FullName = SelectedReader.FullName,
            SerialNumber = SelectedReader.SerialNumber,
            IdNumber = SelectedReader.IdNumber,
            Adress = SelectedReader.Adress,
            AltContactMethods = SelectedReader.AltContactMethods
        };

        db.Readers.InsertOnSubmit(r);
        db.Readers.Context.SubmitChanges();
    }

    public void DeleteFromDb()
    {

    }

    public void EditToDb()
    {

    }

    public void ClearReaderFields()
    {
        SelectedReader = new Reader
        {
            Id = 0,
            FullName = string.Empty,
            SerialNumber = string.Empty,
            IdNumber = string.Empty,
            Adress = string.Empty,
            AltContactMethods = string.Empty
        };
    }

View:

     <ListView Name="listviewReaders" ItemsSource="{Binding ReadersList}" SelectedItem="{Binding SelectedReader,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="140" Height="180" Margin="10,68,492,281">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FullName}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>


  <TextBox x:Name="txtBxFullName" HorizontalAlignment="Left" Height="22"  Margin="211,68,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.FullName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>
        <TextBox x:Name="txtBxSerialNumber" HorizontalAlignment="Left" Height="22"  Margin="211,95,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.SerialNumber, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>
        <TextBox x:Name="txtBxIdNumber" HorizontalAlignment="Left" Height="22"  Margin="211,122,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.IdNumber, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>
        <TextBox x:Name="txtBxAdress" HorizontalAlignment="Left" Height="22"  Margin="211,149,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.Adress, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>
        <TextBox x:Name="txtBxAltContactMethods" HorizontalAlignment="Left" Height="22"  Margin="211,176,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.AltContactMethods, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>

 <Button x:Name="btnReader_Clear" Content="Clear" Command="{Binding ClearR}" HorizontalAlignment="Left" Margin="211,228,0,0" VerticalAlignment="Top" Width="75"/>
    <Button x:Name="btnReader_Save" Content="Save" Command="{Binding EditR}" HorizontalAlignment="Left" Margin="291,228,0,0" VerticalAlignment="Top" Width="75"/>
    <Button x:Name="btnReader_Add" Content="Add New" Command="{Binding AddR}" HorizontalAlignment="Left" Margin="371,228,0,0" VerticalAlignment="Top" Width="75"/>
    <Button x:Name="btnReader_Delete" Content="Delete" Command="{Binding DeleteR}" HorizontalAlignment="Left" Margin="451,228,0,0" VerticalAlignment="Top" Width="75"/>

回答1:

Have you tried to set the Binding of the Text-Property of your textboxes to your ViewModel's SelectedReader-Property directly? So instead of writing this:

<TextBox x:Name="txtBxFullName" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.FullName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" ... />

you would write:

<TextBox x:Name="txtBxFullName" Text="{Binding Path=SelectedReader.FullName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" ... />