我有一个简单的WPF窗口,非常简单,TextBlock
和Button
。
但是按钮不会对任何东西做出反应。如果我将鼠标移到它上面,也不是我点击它。
来自Button的行:
<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>
答案 0 :(得分:1)
您需要像这样指定CommandBindings和Button:
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CloseCommandHandler"
CanExecute="CanExecuteHandler"
/>
</Window.CommandBindings>
....
<Button Margin="3" Command="ApplicationCommands.Close" Content="Ok" Width="50"/>
然后设置Executed和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;
}
希望这有帮助。
答案 1 :(得分:0)
这一切都取决于你的Styles.xaml中的内容。尝试对其进行评论,看看它是否有效。
答案 2 :(得分:0)
migth是绑定到Close命令的问题。要查看的内容(也许可以告诉我们):