2011-08-27 132 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的正確設置
  • 如何被關閉屬性在視圖模型返回命令實現...