Опрация творога с использованием MVVM в WPF
невозможно получить значения текстового поля в viewmodel
[edit]добавлен блок кода-OriginalGriff [/edit]
Что я уже пробовал:
код пользовательского интерфейса-->
<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>
класс моделей:-
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
Graeme_Grant
Вы хотите сказать, что когда вы нажимаете на кнопки, ничего не происходит?
Foothill
Не могли бы вы изменить UpdateSourceTrigger=PropertyChanged в текстовом поле на UpdateSourceTrigger=SelectionChanged и посмотреть, что произойдет? Я предполагаю, что вы хотите отобразить дополнительную информацию об элементе, который пользователь выбирает в ListView.