【问题标题】:Default button does not react to enter pressing on window dialog默认按钮对输入窗口对话框没有反应
【发布时间】:2013-01-28 11:12:19
【问题描述】:

我有一个带有“浏览”按钮的小型用户控件,用于选择一些文件,并带有用于可视化所选文件路径的文本框:

<TextBox Text="{Binding FilePath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Browse" cal:Message.Attach="SelectInstallationPointsFile" />

从窗口对话框开始的控件作为带有两个按钮的窗口 - OKCancel 定义为窗口资源样式(代码的简化版本):

<Style TargetType="{x:Type Window}">
    ...
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                ...
                <ContentControl Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" /> <!-- here will be the user control -->
                <Button IsDefault="True" Content="OK" cal:Message.Attach="Apply" />
                <Button IsCancel="True" cal:Message.Attach="Cancel" />
            ...
</Style>

对话框视图(绿色部分 - 窗口,红色部分 - 用户控件):

接下来是问题 - 当您单击 Enter 按钮时,然后从用户控件中对 Browse 按钮做出反应,而不是从窗口对话框中对 OK 按钮做出反应,尽管 OK 按钮具有 IsDefault="True"

【问题讨论】:

  • 您是否尝试过在浏览按钮中使用 IsDefault="false"?
  • 是否要将“浏览”按钮设为默认按钮。我认为这是不好的做法。您应该设置标签 indexex。如果当前选定的项目是文本框,则选项卡将突出显示浏览,然后按 Enter。
  • 我试了没问题。
  • Ramesh Muthiah,是的,我试过了......但它不起作用......
  • Faisal Hafeez,实际上我想将 OK 按钮设为默认按钮。请仔细阅读

标签: wpf xaml dialog window


【解决方案1】:

在 YourUserControl.xaml.cs 中:

public partial class YourUserControl : UserControl
{
    public YourUserControl()
    {
        InitializeComponent();
        this.OkBtn.Focus();
    }
}

对不起,我的错,你能试试吗:

<ControlTemplate TargetType="{x:Type Window}" FocusManager.FocusedElement="{Binding ElementName=OkBtn}">

?

【讨论】:

  • 用户控件没有确定按钮。 OK 按钮是对话窗口的一部分。
【解决方案2】:

所以,我只是在“浏览”按钮中尝试了 Focusable="False",它确实有效!)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多