Hi,
I want to Enable a button based on somecondition in runtime so i am trying with PropertyChanged event as shown in below code and itseems not to work.
MyUIPage.xaml
<UserControl x:Class="MyNameSpace.MyUIPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ModelSpace="clr-namespace:MyNameSpace.Models"
mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="300" FontFamily="Segoe UI" Background="White">
<Button x:Name="MyButton" IsEnabled="{Binding ModelSpace.MyModel.IsMyButtonEnabled,Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" RenderOptions.BitmapScalingMode="HighQuality" Style="{DynamicResource MyButtonStyle}" />
I have this code in my model class
namespace MyNameSpace.Models
{
class MyModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
private bool _isMyButtonEnabled = false;
public bool IsMyButtonEnabled
{
get
{
return _isMyButtonEnabled;
}
set
{
_isMyButtonEnabled = value;
OnPropertyChanged("IsMyButtonEnabled");
}
}
public PrecedentDetailsModel()
{
//I set the default value
IsMyButtonEnabled = true;
}
public void ChangePropertyStyle()
{
//Based on some condition, I change the property here
IsMyButtonEnabled = false;
}
}
}
Please let me know if i am missing anything in the code, Am i doing the binding correctly or not ? I am calling ChangePropertyStyle() method on a Button Click event.
Regards,
Chetan Rajakumar