gpt4 book ai didi

wpf - 超链接不能与相关命令一起正常工作

转载 作者:行者123 更新时间:2023-12-04 18:10:45 26 4
gpt4 key购买 nike

我在 Grid 中有 Hyperlink。我将命令绑定(bind)到启用/禁用它的超链接。然后我使用命令禁用它。然后在其父级 (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),它也会被启用。

最佳答案

我知道了这就是你所缺少的

 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 将命令订阅委托(delegate)给 CommandManager

关于wpf - 超链接不能与相关命令一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13071931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com