Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to access Textbox GetBindingExpression in ViewModel

$
0
0

Hi,

Model- Person

ViewModel-MainWindowVW

View-MainWindow

Please help how to acces the textbox.getbindingexpression in viewmodel. Please refer sample code.

i have used updatesourcetrigger=Explcit. After click the save button how to get the New Value in CurrentPerson.

Note: Please don't change updatesourcetrigger=Explcit

public class Person : INotifyPropertyChanged { private string name; public string Name { get { return name; } set { name=value; PropChanged("Name"); } } private int age; public int Age { get { return age; } set { age = value; PropChanged("Age"); } } public event PropertyChangedEventHandler PropertyChanged; public void PropChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public class RelayCommand : ICommand { Action<object> action; Predicate<object> predicate; public RelayCommand() { } public RelayCommand(Action<object> action,Predicate<object> predicate) { this.action = action; this.predicate = predicate; } public bool CanExecute(object parameter) { return predicate(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { action(parameter); } } public class MainWindowVW { Person p; RelayCommand saveCommand ; RelayCommand cancelCommand; public MainWindowVW() { p = new Person() { Name = "Amru", Age = 24 }; saveCommand = new RelayCommand(SaveMethod, ValidateSave); cancelCommand = new RelayCommand(CloseMethod, ValidateClose); } public Person CurrentPerson { get { return p; } } public RelayCommand SaveCommand { get { return saveCommand; } } public RelayCommand CloseCommand { get { return cancelCommand; } } private bool ValidateSave(object p) { return true; } private bool ValidateClose(object p) { return true; } private void SaveMethod(object p) { this.CurrentPerson.PropChanged("Name"); this.CurrentPerson.PropChanged("Age"); MessageBox.Show("[After] Name: " + this.CurrentPerson.Name + " Age: " + this.CurrentPerson.Age.ToString()); } private void CloseMethod(object p) { Application.Current.MainWindow.Close(); } } public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new MainWindowVW(); } }<Window x:Class="Command_Explicit.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="125" Width="230" WindowStartupLocation="CenterScreen"><Grid FocusManager.FocusedElement="{Binding ElementName=txtName}"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="40"/><ColumnDefinition Width="168"/></Grid.ColumnDefinitions><TextBlock Text="Name:" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="3" Grid.Row="0"/><TextBlock Text="Age:" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="3" Grid.Row="1"/><TextBox Name="txtName" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="3" Grid.Row="0" Grid.Column="1" Width="160" TabIndex="0" Text="{Binding Path=CurrentPerson.Name,UpdateSourceTrigger=Explicit}"/><TextBox Name="txtAge" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="3" Grid.Row="1" Grid.Column="1" Width="160" TabIndex="1" Text="{Binding Path=CurrentPerson.Age,UpdateSourceTrigger=Explicit}"/><StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Right"><Button Name="btnSave" VerticalAlignment="Center" Width="60" Height="25" Margin="3" Content="Save" IsDefault="True" Command="{Binding SaveCommand}"/><Button Name="btnCancel" VerticalAlignment="Center" Width="60" Height="25" Margin="3" Content="Cancel" IsCancel="True" Command="{Binding CloseCommand}" /></StackPanel></Grid></Window>




Ahmed


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>