[英]Hyperlink doesn't work properly with associated command
I have Hyperlink
in a Grid
. 我在Grid
有Hyperlink
。 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. 然后在其父级( Grid
)上设置IsEnabled=False
属性。 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: 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: 并注册一个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). 如果将Hyperlink替换为Button,则即使禁用了其父级,也将启用它(grid.IsEnabled = false)。
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 CanExecuteChanged将命令预订委派给CommandManager
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.