Hi all,
I need to find-out is there any change in my page when Clicking Exit Button?
Here i have solution, but its not 100% perfect, so that i am expecting some other solutions.
so i have used INotifyPropertyChanged Interface then just set that boolean Property = true;
Below my Code:-
My MainWindow.Xaml:-
<Window x:Class="DataContextTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="502" Width="892" Name="SampleWinduw" Initialized="SampleWinduw_Initialized" ><Grid><Label Content="Name:" Height="28" Margin="193,45,604,0" Name="label1" VerticalAlignment="Top" /><Label Content="Address:" Height="28" Margin="193,82,599,0" Name="label2" VerticalAlignment="Top" /><Label Content="Gender:" Height="28" Margin="193,120,597,0" Name="label3" VerticalAlignment="Top" /><TextBox Text="{Binding Path=Name,Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="341,46,0,0" Name="textBox1" VerticalAlignment="Top" Width="225" /><TextBox Text="{Binding Path=Address,Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="341,83,0,0" Name="textBox2" VerticalAlignment="Top" Width="225" /><ComboBox Text="{Binding Path=Gender,Mode=TwoWay}" Height="23" Margin="341,120,0,0" Name="comboBox1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="144" ><ComboBoxItem Content="Male"/><ComboBoxItem Content="Fenale"/></ComboBox><Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="341,177,0,0" Name="SaveButton" VerticalAlignment="Top" Width="93" /><Button Content="Refresh" Height="23" HorizontalAlignment="Left" Margin="455,177,0,0" Name="RefreshButton" VerticalAlignment="Top" Width="75" Click="RefreshButton_Click" /><Button Content="Exit" Height="23" HorizontalAlignment="Left" Margin="552,177,0,0" Name="ExitButton" VerticalAlignment="Top" Width="104" Click="ExitButton_Click" /></Grid></Window>My MainWindow.Xaml.CS
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void SampleWinduw_Initialized(object sender, EventArgs e)
{
this.DataContext = new Person();
}
private void RefreshButton_Click(object sender, RoutedEventArgs e)
{
this.DataContext = new Person();
}
private void ExitButton_Click(object sender, RoutedEventArgs e)
{
Person personEntity = this.DataContext as Person;
if (personEntity.IsModified)
{
if (MessageBox.Show("Close the page and abondan changes","Confirm?",MessageBoxButton.YesNo,MessageBoxImage.Question) == MessageBoxResult.Yes)
{
this.Close();
}
}
else
{
this.Close();
}
}
}
public class Person : INotifyPropertyChanged
{
public bool IsModified { get; set; }
private string _name = string.Empty;
private string _address = string.Empty;
private string _gender = "Male";
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
public string Address
{
get { return _address; }
set
{
_address = value;
RaisePropertyChanged("Address");
}
}
public string Gender
{
get { return _gender; }
set
{
_gender = value;
RaisePropertyChanged("Gender");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
IsModified = true;
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
} I hope most of the people will tell the above code is working fine. yes, i too will tell thats working fine.
Please do my Scenario:-
1. Just run the Project.
2. Change Gender value Male to Female. actually here i changed wrongly. so that i will change once again to male.
3. Click Exit Button. this time it will tell that "Close the page and abondon changes?". originally i didn't change any value here.
i hope i am telling correct.
so any legends have any ideas kindly share with me. In this type of case i don't like to say this type of message to user.