【发布时间】:2012-10-25 15:24:11
【问题描述】:
我在Grid 中有Hyperlink。我将命令绑定到启用/禁用它的超链接。然后我使用命令禁用它。然后在其父级 (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)也会启用它。
【问题讨论】:
-
我没有看到任何命令绑定设置来实际将您的 testCommand 的 Execute 和 CanExecute 实现绑定到命令本身。您只是没有包含这部分代码吗?如果您没有设置命令绑定,则无论 IsEnabled 设置如何,控件都将永远不会激活。
-
通过覆盖
Start按钮中与命令的绑定,您的 XAML 永远不会像@DanaCartwright 指出的那样收到命令通知。 -
我简化了我的例子。这不是因为
DataBinding。
标签: wpf xaml data-binding hyperlink