Reputation: 217
I have Hyperlink
in a Grid
. I bind command to hyperlink that Enables/Disables it. Then I disable it using the command. Then on its parent (Grid
) I set IsEnabled=False
property. After that I enable my Hyperlink with my command and enable Grid, but hyperlink doesn't activate!
Here is sample:
Command testCommand = new Command();
public MainWindow() {
InitializeComponent();
hl.Command = testCommand;
}
private void Start(object sender, RoutedEventArgs e) {
//Disable Hyperlink
testCommand.Enabled = false;
//Disable Grid
grid.IsEnabled = false;
//Enable Hyperlink
testCommand.Enabled = true;
//Enable Grid
grid.IsEnabled = true;
//hl.IsEnabled = true; //if uncomment this all will be work
}
XAML:
<Window x:Class="WpfApplication25.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="172"
Width="165">
<StackPanel>
<Grid x:Name="grid">
<TextBlock>
<Hyperlink x:Name="hl">Test</Hyperlink>
</TextBlock>
</Grid>
<Button Content="Start"
Name="button1"
Click="Start" />
</StackPanel>
</Window>
And register an ICommand:
public class Command : ICommand {
private bool enabled;
public bool Enabled {
get {
return enabled;
}
set {
enabled = value;
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
public bool CanExecute(object parameter) {
return Enabled;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter) { }
}
Update:
If Hyperlink is replaced with Button, it will be enabled even if its parent is disabled (grid.IsEnabled = false).
Upvotes: 3
Views: 1811
Reputation: 672
Wow I got it Here's what you are missing
public class Command : ICommand
{
private bool enabled;
public bool Enabled
{
get
{
return enabled;
}
set
{
enabled = value;
//if (CanExecuteChanged != null)
// CanExecuteChanged(this, EventArgs.Empty);
}
}
public bool CanExecute(object parameter)
{
return Enabled;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter) { }
}
The CanExecuteChanged delegates the command subscription to the CommandManager
Upvotes: 1