2011-08-27 122 views
0

反应我有一个简单的WPF窗口,很简单,一个TextBlockButton。 但是,按钮不响应任何事情。不是,如果我将鼠标移到它上面,或者如果我点击它。从ButtonWPF按钮没有对任何事件

线:

<Button Margin="3" Command="Close" Content="Ok" Width="50"/> 

全窗口代码:

<Window x:Class="Launcher.XAML.MessageWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Name="self" 
    Title="{Binding ElementName=self, Path=Caption}" Height="194" Width="477"> 
<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <TextBlock VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Text="{Binding ElementName=self, Path=Message}" Margin="10" TextWrapping="Wrap" /> 

    <StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal"> 
     <Button Margin="3" Command="Close" Content="Ok" Width="50"/> 
    </StackPanel> 
</Grid> 

+1

你对此有何反应? – svick

+0

假设随着鼠标点击/点击发生某种视觉变化。至少,这是我的假设。 –

回答

1

你需要指定化CommandBindings和按钮,像这样:

<Window.CommandBindings> 
    <CommandBinding Command="ApplicationCommands.Close" 
       Executed="CloseCommandHandler" 
       CanExecute="CanExecuteHandler" 
       /> 
</Window.CommandBindings> 

.... 

<Button Margin="3" Command="ApplicationCommands.Close" Content="Ok" Width="50"/> 

然后设置你的执行,CanExecute处理程序:

private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e) 
{ 
    //Do something 
} 

private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e) 
{ 
    //Determine whether handler can execute 
    e.CanExecute = true; 
} 

希望这有助于。

+0

是的,是CanExecute处理程序。此外,它是那些标准命令之一,所以我需要写我自己的。 – skeleten

+0

您可以在XAML中将'ApplicationCommands.Close'缩短为'Close'。 – svick

0

这一切都取决于什么在你的Styles.xaml。试着评论一下,看看它是否有效。

0

migth是绑定到Close命令的问题。事情在期待(或许我们展示):

  • 该视图的DataContext的正确设置
  • 如何被关闭属性在视图模型返回命令实现...