我在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)。
答案 0 :(得分:1)
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) { }
}
CanExecuteChanged将命令订阅委派给CommandManager