在WPF中使用MVVM进行的curd操作 [英] Curd opration using MVVM in WPF

查看:93
本文介绍了在WPF中使用MVVM进行的curd操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在viewmodel中获取文本框值



已添加代码块 - OriginalGriff [/ edit]



我尝试过:



ui code - >





unable to get textbox values in viewmodel

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

ui code-->


<Grid Margin="0,0,0,20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListView Name="UserGrid" Grid.Row="1" Margin="4,242,12,-21"  ItemsSource="{Binding Users}" Grid.RowSpan="2"  >
            <ListView.View>
                <GridView x:Name="grdTest">
                    <GridViewColumn Header="UserId" DisplayMemberBinding="{Binding UserId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  Width="50"/>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}"  Width="80" />
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="100" />
                    <GridViewColumn Header="City" DisplayMemberBinding="{Binding City}" Width="80" />
                    <GridViewColumn Header="State" DisplayMemberBinding="{Binding State}" Width="80" />
                    <GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" Width="100" />
                </GridView>
            </ListView.View>
        </ListView>
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,7,0,0" Name="txtUserId" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid,Path=SelectedItem.UserId,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,35,0,0" Name="txtFirstName" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid,Path=SelectedItem.FirstName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,62,0,0" Name="txtLastName" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=UserGrid,Path=SelectedItem.LastName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Label Content="UserId" Grid.Row="1" HorizontalAlignment="Left" Margin="12,12,0,274" />
        <Label Content="Last Name" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,60,0,0"  VerticalAlignment="Top" />
        <Label Content="First Name" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,35,0,0" VerticalAlignment="Top" />
        <Button Content="Update" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="184,199,0,0"  VerticalAlignment="Top" Width="95"
                Command="{Binding UpdateCommand,Source={StaticResource VM}}"  />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,87,0,0"  VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.City, ElementName=UserGrid,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Label Content="Country" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,141,0,0"  VerticalAlignment="Top" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,150,0,0"  VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.Country, ElementName=UserGrid,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Label Content="City" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,86,0,0"  VerticalAlignment="Top" />
        <TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,115,0,0" VerticalAlignment="Top" Width="178" Text="{Binding SelectedItem.State, ElementName=UserGrid,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Label Content="State" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,113,0,0"  VerticalAlignment="Top" />
        <Button Content="Save" HorizontalAlignment="Left" Margin="80,199,0,0" Grid.Row="1" VerticalAlignment="Top" Width="99"
                Command="{Binding SaveCommand,Source={StaticResource VM}}" />
        <Button Content="Delete" HorizontalAlignment="Left" Margin="297,199,0,0" Grid.Row="1" VerticalAlignment="Top" Width="80" Command="{Binding DeleteCommand,Source={StaticResource VM}}" />
        <Button Content="Canceal" Command="{Binding CancealComand,Source={StaticResource VM}}"
                HorizontalAlignment="Left" Margin="394,199,0,0" Grid.Row="1" VerticalAlignment="Top" Width="74"/>
    </Grid>





型号类别: -



Model Class:-

public class User : INotifyPropertyChanged
    {
        private int userId;
        private string firstName;
        private string lastName;
        private string city;
        private string state;
        private string country;

        public int UserId
        {
            get
            {
                return userId;
            }
            set
            {
                userId = value;
                OnPropertyChanged("UserId");
            }
        }
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
                OnPropertyChanged("FirstName");
            }
        }
        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                lastName = value;
                OnPropertyChanged("LastName");
            }
        }
        public string City
        {
            get
            {
                return city;
            }
            set
            {
                city = value;
                OnPropertyChanged("City");
            }
        }
        public string State
        {
            get
            {
                return state;
            }
            set
            {
                state = value;
                OnPropertyChanged("State");
            }
        }
        public string Country
        {
            get
            {
                return country;
            }
            set
            {
                country = value;
                OnPropertyChanged("Country");
            }
        }


        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }

ViewModel Class:-
 private ObservableCollection<user> _UsersList;
        public UserViewModel()
        {
            

            getUSerList();
        }
        public ObservableCollection<user> Users
        {
            get { return _UsersList; }
            set { _UsersList = value; }
        }
         #region SaveCommand
        private ICommand saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(SaveExecuted, CanSaveExecute);
                }
                return saveCommand;
            }

        }

        public bool CanSaveExecute(object parameter)
        {
            // for save command 
            User _uuser=new User ();

            int str = _uuser.UserId;

            return true;
        }
        public void SaveExecuted(object parameter)
        {
            string str = userId;



            MessageBox.Show("Save Button clicked");
        }
        #endregion
  #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

推荐答案

{Binding ElementName = UserGrid,Path = SelectedItem.UserId,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}





ListView.SelectedItem不是用户。它是ListViewItem的实例(或类似的东西)。

您应该使用SelectedValue而不是SelectedItem。

将SelectedUser属性添加到UserViewModel并且

1)绑定也是一个好主意ListView的SelectedValue到这个属性

2)绑定你的TextBox,如{Binding SelectedUser.UserId,Mode = TwoWay}

在这种情况下,你不会在绑定中使用ElementName。通过ElementName绑定不是最佳选择。
{Binding ElementName=UserGrid,Path=SelectedItem.UserId,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}


ListView.SelectedItem is not a User. It is an Instance of ListViewItem (or somthing like this).
You should use SelectedValue instead of SelectedItem.
It also could be a good idea to add SelectedUser property to the UserViewModel and
1) bind SelectedValue of ListView to this property
2) bind your TextBoxes like {Binding SelectedUser.UserId,Mode=TwoWay}
In this case you will not use ElementName in binding. Binding by ElementName not a best choice.


您的代码在Xaml中存在一些导致问题的问题。保持尽可能干净以使其可读是一个好主意。所以这里有一个修改版本:

Your code has a few issues in the Xaml that caused problems. It is alway a good idea to keep it as clean as possible to make it readable. So here is a revised version for you:
<Window x:Class="WpfListViewEdit.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfListViewEdit"

        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">

    <Window.DataContext>
        <local:UserViewModel/>
    </Window.DataContext>

    <Grid Margin="20">
        <Grid.Resources>
            <Style TargetType="Label">
                <Setter Property="HorizontalAlignment" Value="Right"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Margin" Value="0 0 4 0"/>
            </Style>
            <Style TargetType="TextBox">
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="VerticalAlignment" Value="Bottom"/>
                <Setter Property="Margin" Value="0 4"/>
            </Style>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="10 0"/>
                <Setter Property="Padding" Value="10 5"/>
            </Style>
        </Grid.Resources>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="200" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Label Content="_Id"

               Target="{Binding ElementName=txtUserId}" />
        <TextBox Name="txtUserId" Grid.Column="1" 

                 Text="{Binding ElementName=UserGrid, Path=SelectedItem.UserId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <Label Content="_First Name" Grid.Row="1"

               Target="{Binding ElementName=txtFirstName}" />
        <TextBox Name="txtFirstName" Grid.Row="1" Grid.Column="1" 

                 Text="{Binding ElementName=UserGrid, Path=SelectedItem.FirstName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <Label Content="_Last Name" Grid.Row="2"

               Target="{Binding ElementName=txtLastName}" />
        <TextBox Name="txtLastName" Grid.Row="2" Grid.Column="1" 

                 Text="{Binding ElementName=UserGrid, Path=SelectedItem.LastName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <Label Content="C_ity" Grid.Row="3"

               Target="{Binding ElementName=txtCity}" />
        <TextBox Name="txtCity" Grid.Row="3" Grid.Column="1" 

                 Text="{Binding ElementName=UserGrid, Path=SelectedItem.City, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <Label Content="S_tate" Grid.Row="4"

               Target="{Binding ElementName=txtState}"/>
        <TextBox Name="txtState" Grid.Row="4" Grid.Column="1"

                 Text="{Binding ElementName=UserGrid, Path=SelectedItem.State, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <Label Content="C_ountry" Grid.Row="5"

               Target="{Binding ElementName=txtCountry}" />
        <TextBox Name="txtCountry" Grid.Row="5" Grid.Column="1"

                 Text="{Binding ElementName=UserGrid, Path=SelectedItem.Country ,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <StackPanel Orientation="Horizontal" Grid.Row="6" Grid.ColumnSpan="3" Margin="0 10" HorizontalAlignment="Center">
            <Button Content="_Save"  Command="{Binding SaveCommand}" />
            <Button Content="_Update" Command="{Binding UpdateCommand}" />
            <Button Content="_Delete" Command="{Binding DeleteCommand}" />
            <Button Content="_Cancel" Command="{Binding CancelComand}"/>
        </StackPanel>

        <ListView Name="UserGrid" Grid.Row="7" ItemsSource="{Binding Users}" Grid.ColumnSpan="3"  >
            <ListView.View>
                <GridView x:Name="grdTest">
                    <GridViewColumn Header="UserId" DisplayMemberBinding="{Binding UserId}" Width="50"/>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="80" />
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="100" />
                    <GridViewColumn Header="City" DisplayMemberBinding="{Binding City}" Width="80" />
                    <GridViewColumn Header="State" DisplayMemberBinding="{Binding State}" Width="80" />
                    <GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" Width="100" />
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>

</Window>



With these changes in place, the Window works as expected if (Mock) data is supplied. As you select rows in the listview, the TextBoxes reflect the selection; any changes made in the TextBoxes is reflected in the ListView; all in real time.



The layout seems to be upside-down. I would have expected the Listview to be at the top, buttons to Add, Edit, Delete to be with the ListView (side or bottom), and the edit pane (edit fields with Save & Cancel buttons) to appear (come into view) separated at the bottom or pop up in an edit window when one of the ListView buttons were used. Something like this:


With these changes in place, the Window works as expected if (Mock) data is supplied. As you select rows in the listview, the TextBoxes reflect the selection; any changes made in the TextBoxes is reflected in the ListView; all in real time.

The layout seems to be upside-down. I would have expected the Listview to be at the top, buttons to Add, Edit, Delete to be with the ListView (side or bottom), and the edit pane (edit fields with Save & Cancel buttons) to appear (come into view) separated at the bottom or pop up in an edit window when one of the ListView buttons were used. Something like this:

<Window x:Class="WpfListViewEdit.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfListViewEdit"

        mc:Ignorable="d"

        Title="MainWindow" Height="480" Width="600" WindowStartupLocation="CenterScreen">

    <Window.DataContext>
        <local:UserViewModel/>
    </Window.DataContext>

    <Grid Margin="20">
        <Grid.Resources>
            <Style TargetType="Label">
                <Setter Property="HorizontalAlignment" Value="Right"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Margin" Value="0 0 4 0"/>
            </Style>
            <Style TargetType="TextBox">
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="VerticalAlignment" Value="Bottom"/>
                <Setter Property="Margin" Value="0 4"/>
            </Style>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="10 0 0 0"/>
                <Setter Property="Padding" Value="10 5"/>
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition MinHeight="140"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ListView Name="UserGrid" ItemsSource="{Binding Users}" Grid.ColumnSpan="3"  >
                <ListView.View>
                    <GridView x:Name="grdTest">
                        <GridViewColumn Header="UserId" DisplayMemberBinding="{Binding UserId}" Width="50"/>
                        <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="80" />
                        <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="100" />
                        <GridViewColumn Header="City" DisplayMemberBinding="{Binding City}" Width="80" />
                        <GridViewColumn Header="State" DisplayMemberBinding="{Binding State}" Width="80" />
                        <GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" Width="100" />
                    </GridView>
                </ListView.View>
            </ListView>

            <StackPanel Orientation="Horizontal" Grid.Row="1" Margin="0 10"

                        HorizontalAlignment="Right">
                <Button Content="_Add"  Command="{Binding AddCommand}" />
                <Button Content="_Update" Command="{Binding UpdateCommand}" />
                <Button Content="_Delete" Command="{Binding DeleteCommand}" />
            </StackPanel>
        </Grid>

        <GroupBox Header="EDIT DETAILS" Grid.Row="1" Padding="10">

            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Label Content="_Id"

                       Target="{Binding ElementName=txtUserId}" />
                <TextBox Name="txtUserId" Grid.Column="1" IsReadOnly="True"

                         Text="{Binding ElementName=UserGrid, Path=SelectedItem.UserId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="_First Name" Grid.Row="1"

                       Target="{Binding ElementName=txtFirstName}" />
                <TextBox Name="txtFirstName" Grid.Row="1" Grid.Column="1" 

                         Text="{Binding ElementName=UserGrid, Path=SelectedItem.FirstName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="_Last Name" Grid.Row="2"

                       Target="{Binding ElementName=txtLastName}" />
                <TextBox Name="txtLastName" Grid.Row="2" Grid.Column="1" 

                         Text="{Binding ElementName=UserGrid, Path=SelectedItem.LastName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="C_ity" Grid.Row="3"

                       Target="{Binding ElementName=txtCity}" />
                <TextBox Name="txtCity" Grid.Row="3" Grid.Column="1" 

                         Text="{Binding ElementName=UserGrid, Path=SelectedItem.City, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="S_tate" Grid.Row="4"

                       Target="{Binding ElementName=txtState}"/>
                <TextBox Name="txtState" Grid.Row="4" Grid.Column="1"

                         Text="{Binding ElementName=UserGrid, Path=SelectedItem.State, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="C_ountry" Grid.Row="5"

                       Target="{Binding ElementName=txtCountry}" />
                <TextBox Name="txtCountry" Grid.Row="5" Grid.Column="1"

                         Text="{Binding ElementName=UserGrid, Path=SelectedItem.Country ,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <StackPanel Orientation="Horizontal" Grid.Row="6" Grid.ColumnSpan="3" Margin="0 10" HorizontalAlignment="Right">
                    <Button Content="_Save"  Command="{Binding SaveCommand}" />
                    <Button Content="_Cancel" Command="{Binding CancelComand}"/>
                </StackPanel>

            </Grid>
        </GroupBox>

    </Grid>
</Window>



Now the Window starts to make sense.



Lastly, the button won’t work as you have not implemented your RelayCommand, well not in the code supplied.



UPDATE: Re-reading your question, I noticed that the above failed to answer the following:


Now the Window starts to make sense.

Lastly, the button won't work as you have not implemented your RelayCommand, well not in the code supplied.

UPDATE: Re-reading your question, I noticed that the above failed to answer the following:

Satya Praksh Mishra wrote:
Satya Praksh Mishra wrote:

unable to get textbox values in viewmodel

unable to get textbox values in viewmodel



So, instead of binding the edit fields directly to the Listview, bind then and the ListView.SelectedItem to a property in the ViewModel.



In the following example, to keep code clean, I have bound the ViewModel’s SelectedUser property to the GroupBox. Bindings Cascade from parent to child in XAML. So the Textboxes’ DataContext is bound to the SelectedUser of the UserViewModel. Now we only need to bind to the properties of the SelectedUser property of type User.



Updated XAML:


So, instead of binding the edit fields directly to the Listview, bind then and the ListView.SelectedItem to a property in the ViewModel.

In the following example, to keep code clean, I have bound the ViewModel's SelectedUser property to the GroupBox. Bindings Cascade from parent to child in XAML. So the Textboxes' DataContext is bound to the SelectedUser of the UserViewModel. Now we only need to bind to the properties of the SelectedUser property of type User.

Updated XAML:

<Window x:Class="WpfListViewEdit.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfListViewEdit"

        mc:Ignorable="d"

        Title="Manage Users" Height="480" Width="600" WindowStartupLocation="CenterScreen">

    <Window.DataContext>
        <local:UserViewModel/>
    </Window.DataContext>

    <Grid Margin="20">
        <Grid.Resources>
            <Style TargetType="Label">
                <Setter Property="HorizontalAlignment" Value="Right"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Margin" Value="0 0 4 0"/>
            </Style>
            <Style TargetType="TextBox">
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="VerticalAlignment" Value="Bottom"/>
                <Setter Property="Margin" Value="0 4"/>
            </Style>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="10 0 0 0"/>
                <Setter Property="Padding" Value="10 5"/>
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition MinHeight="140"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ListView Name="UserGrid" Grid.ColumnSpan="3" 

                      ItemsSource="{Binding Users}" 

                      SelectedItem="{Binding SelectedUser}">
                <ListView.View>
                    <GridView x:Name="grdTest">
                        <GridViewColumn Header="UserId" DisplayMemberBinding="{Binding UserId}" Width="50"/>
                        <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="80" />
                        <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="100" />
                        <GridViewColumn Header="City" DisplayMemberBinding="{Binding City}" Width="80" />
                        <GridViewColumn Header="State" DisplayMemberBinding="{Binding State}" Width="80" />
                        <GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country}" Width="100" />
                    </GridView>
                </ListView.View>
            </ListView>

            <StackPanel Orientation="Horizontal" Grid.Row="1" Margin="0 10"

                        HorizontalAlignment="Right">
                <Button Content="_Add"  Command="{Binding AddCommand}" />
                <Button Content="_Update" Command="{Binding UpdateCommand}" />
                <Button Content="_Delete" Command="{Binding DeleteCommand}" />
            </StackPanel>
        </Grid>

        <GroupBox Header="EDIT DETAILS" Grid.Row="1" Padding="10" DataContext="{Binding SelectedUser}">

            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Label Content="_Id"

                       Target="{Binding ElementName=txtUserId}" />
                <TextBox Name="txtUserId" Grid.Column="1" IsReadOnly="True"

                         Text="{Binding UserId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="_First Name" Grid.Row="1"

                       Target="{Binding ElementName=txtFirstName}" />
                <TextBox Name="txtFirstName" Grid.Row="1" Grid.Column="1" 

                         Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="_Last Name" Grid.Row="2"

                       Target="{Binding ElementName=txtLastName}" />
                <TextBox Name="txtLastName" Grid.Row="2" Grid.Column="1" 

                         Text="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="C_ity" Grid.Row="3"

                       Target="{Binding ElementName=txtCity}" />
                <TextBox Name="txtCity" Grid.Row="3" Grid.Column="1" 

                         Text="{Binding City, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="S_tate" Grid.Row="4"

                       Target="{Binding ElementName=txtState}"/>
                <TextBox Name="txtState" Grid.Row="4" Grid.Column="1"

                         Text="{Binding State, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <Label Content="C_ountry" Grid.Row="5"

                       Target="{Binding ElementName=txtCountry}" />
                <TextBox Name="txtCountry" Grid.Row="5" Grid.Column="1"

                         Text="{Binding Country ,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                <StackPanel Orientation="Horizontal" Grid.Row="6" Grid.ColumnSpan="3" Margin="0 10" HorizontalAlignment="Right">
                    <Button Content="_Save"  Command="{Binding SaveCommand}" />
                    <Button Content="_Cancel" Command="{Binding CancelComand}"/>
                </StackPanel>

            </Grid>
        </GroupBox>

    </Grid>
</Window>



Updated UserViewModel:


Updated UserViewModel:

internal class UserViewModel : INotifyPropertyChanged
{
    private User selectedUser;
    private ObservableCollection<User> _UsersList;

    public UserViewModel()
    {
        GetUserList();
    }

    private void GetUserList()
    {
        Users = new ObservableCollection<User>();
        for (int i = 0; i < 100; i++)
        {
            Users.Add(new User
            {
                UserId = 1000 + i,
                FirstName =


\"FirstName {i}\",
LastName =
"FirstName {i}", LastName =


这篇关于在WPF中使用MVVM进行的curd操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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