Hi,@Lakshmanan B. Welcome to Microsoft Q&A.
After testing, your cs code could successfully enable or disable the button. Please make sure that your cs code is executed. Below are the test code and steps.
<Grid Background="#FFEDEDED" Height="80">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button IsEnabled="True" Click="addUserButton_Click" x:Name="addUserButton" Width="66" Margin="10,0,0,0" BorderThickness="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Height="58" >
<Button.Effect>
<DropShadowEffect ShadowDepth="3" BlurRadius="5" Color="LightGray"/>
</Button.Effect>
<StackPanel Width="62" Height="56">
<Image Source="/resources/add_user_icon.png" Height="32" Width="32"/>
<Label Content="Add User" Height="23" Width="60" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</StackPanel>
</Button>
<Button Grid.Column="1" Width="120" Height="58" Margin="10,0,0,0" Content="Change IsUserAdmin" Click="Button_Click" HorizontalAlignment="Left"></Button>
</Grid>
public partial class controlOptionsMenu : UserControl
{
public bool isUserAdmin;
public controlOptionsMenu()
{
InitializeComponent();
isUserAdmin = true;
}
private void addUserButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("AAA");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
isUserAdmin = !isUserAdmin;
if (isUserAdmin)
{
addUserButton.IsEnabled = true;
}
else
{
addUserButton.IsEnabled = false;
}
}
}
When you click Change IsUserAdmin button, you could enable or disable the button.When the enabled button is clicked, a pop-up window will pop up
Optimization
You could directly bind isUserAdmin to IsEnabled of Button, so that Button could be enabled or disabled in real time according to isUserAdmin. The reference code is as follows.
Change IsEnabled="True" to IsEnabled="{Binding IsUserAdmin}"
<Grid Background="#FFEDEDED" Height="80">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button IsEnabled="{Binding IsUserAdmin}" Click="addUserButton_Click" x:Name="addUserButton" Width="66" Margin="10,0,0,0" BorderThickness="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Height="58" >
<Button.Effect>
<DropShadowEffect ShadowDepth="3" BlurRadius="5" Color="LightGray"/>
</Button.Effect>
<StackPanel Width="62" Height="56">
<Image Source="/resources/add_user_icon.png " Height="32" Width="32"/>
<Label Content="Add User" Height="23" Width="60" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</StackPanel>
</Button>
<Button Grid.Column="1" Width="120" Height="58" Margin="10,0,0,0" Content="Change IsUserAdmin" Click="Button_Click" HorizontalAlignment="Left"></Button></Grid>
public partial class controlOptionsMenu : UserControl,INotifyPropertyChanged
{
private bool isUserAdmin;
public bool IsUserAdmin
{
get { return isUserAdmin; }
set { isUserAdmin = value; OnPropertyChanged("IsUserAdmin"); }
}
public controlOptionsMenu()
{
InitializeComponent();
this.DataContext = this;
IsUserAdmin = true;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void addUserButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("AAA");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
IsUserAdmin = !IsUserAdmin;
}
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.