window 中有两个按钮时聚焦第一个按钮

Focus first button when there are two buttons in a window

我用Window显示一个对话框,对话框有两个按钮:确认和取消,但取消按钮总是焦点,即使我设置了它的Focusable=False,而我想让确认按钮默认聚焦,然后我可以在按下回车键时执行确认处理程序。

  <Button x:Name="btnConfirm" Content="Confirm" HorizontalAlignment="Right" Margin="0,0,100,10" VerticalAlignment="Bottom" Width="75" Click="btnConfirm_Click" Focusable="True"/>
  <Button x:Name="btnCancel" Content="Cancel" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="75" IsDefault="True" Click="btnCancel_Click" Focusable="False"/> 

您需要将 IsDefault="True"btnCancel 移动到 btnConfirm:

<Button x:Name="btnConfirm" ... IsDefault="True" ... />

对于btnCancel你可以设置IsCancel:

<Button x:Name="btnCancel" ... IsCancel="True" ... />