我有Hyperlink
一个Grid
. 我将命令绑定到启用/禁用它的超链接。然后我使用命令禁用它。然后在其父 ( Grid
) 上设置IsEnabled=False
属性。之后我用我的命令启用我的超链接并启用网格,但超链接没有激活!
这是示例:
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>
并注册一个 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) { }
}
更新:
如果 Hyperlink 被 Button 替换,即使它的父级被禁用(grid.IsEnabled = false)也会启用它。