【问题标题】:WPF Unleashed error?WPF 释放错误?
【发布时间】:2011-03-02 01:02:20
【问题描述】:

书上说

WPF Button 类只增加了两个 简单的概念在什么之上 ButtonBase 已经提供:作为一个 取消按钮或默认按钮。 这两种机制很方便短 对话的剪辑。 If Button.IsCancel 在 a 内的 Button 上设置为 true 对话框(即,通过 它的 ShowDialog 方法),窗口是 自动关闭 对话结果为假。如果 Button.IsDefault 设置为 true, 按 Enter 会使 Button 变为 除非明确显示焦点,否则单击 带走它。

但是在这个示例窗口中

<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
Background="OrangeRed" >
    <StackPanel>
        <Label  FontWeight="Bold" FontSize="20" Foreground="White">
            WPF Unleashed (Version 3.0)
        </Label>
        <Label> 2006 SAMS Publishing</Label>
        <Label>Installed Chapters:</Label>
        <ListBox>
            <ListBoxItem>Chapter 1</ListBoxItem>
            <ListBoxItem>Chapter 2</ListBoxItem>
        </ListBox>
        <TextBox AcceptsReturn="False">HELLO TEXT</TextBox>
        <RadioButton>HELLO RADIO BUTTON</RadioButton>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button IsCancel="True" MinWidth="75" Margin="10">Cancel</Button>
            <Button x:Name="OKBUTTON" IsDefault="True" MinWidth="75" Margin="10">OK</Button>
        </StackPanel>
        <StatusBar>You have successfully registered this product.</StatusBar>
    </StackPanel>
</Window>

如果我按 Enter 键甚至单击它,模态窗口(通过 ShowDialog())不会关闭(将返回值放在一边)。 这是书上的错误吗?

【问题讨论】:

  • 取消按钮的 Esc 有效吗?
  • 不,当我们按下 Esc 或 Enter 时,似乎引发了相应按钮的单击事件。表单不会自动关闭。很不幸,这么简单的概念在本书中写错了。不知道书中有多少错误。

标签: wpf wpf-controls


【解决方案1】:

我认为这是一个错误。 IsDefault 和 IsCancel 只是意味着在创建窗口时应用了一些访问键魔法,因此当您分别按“Enter”和“ESC”时单击按钮。

如果您想关闭一个窗口 - 您需要: (来自MSDN) 当一个对话框被接受时,它应该返回一个对话框结果为真,这是通过在单击确定按钮时设置 DialogResult 属性来实现的 ... 请注意,设置 DialogResult 属性也会导致窗口自动关闭,从而减少显式调用 Close 的需要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2014-11-10
    • 2012-10-15
    相关资源
    最近更新 更多