gpt4 book ai didi

wpf - WPF-默认按钮无法按预期工作

转载 作者:行者123 更新时间:2023-12-03 14:37:44 24 4
gpt4 key购买 nike

我在以下xaml代码中的默认Button遇到问题:

<Window x:Class="WebSiteMon.Results.Views.GraphicSizeSelectPopUp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:commonWPF="http://rubenhak.com/common/wpf"
xmlns:WPF="clr-namespace:Bennedik.Validation.Integration.WPF;assembly=Bennedik.Validation.Integration.WPF"
ResizeMode="NoResize"
WindowStyle="ThreeDBorderWindow"
SizeToContent="WidthAndHeight">
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red"
BorderThickness="2">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError"
Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<WPF:ErrorProvider x:Name="UrlDataErrorProvider"
RulesetName="RuleSetA">
<Grid Background="{DynamicResource WindowBackgroundBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<RadioButton Grid.ColumnSpan="2"
Margin="10"
Name="radioButton1"
Content="640 x 480 pixels"
Command="{Binding SelectSizeRb}"
CommandParameter="640,480" />
<RadioButton Grid.ColumnSpan="2"
Margin="10"
Name="radioButton2"
Content="800 x 600 pixels"
Command="{Binding SelectSizeRb}"
CommandParameter="800,600"
Grid.Row="1"
IsChecked="True" />
<RadioButton Grid.ColumnSpan="2"
Margin="10"
Name="radioButton3"
Content="1024 x 768 pixels"
Command="{Binding SelectSizeRb}"
CommandParameter="1024,768"
Grid.Row="2" />
<RadioButton Grid.ColumnSpan="2"
Margin="10"
Name="radioButton4"
Command="{Binding SelectSizeRb}"
CommandParameter="0,0"
Grid.Row="3" />

<Button Grid.Column="1"
Grid.ColumnSpan="1"
Grid.Row="5"
Margin="5"
Name="BtnOk"
IsDefault="True">Ok</Button>
<Button Grid.Column="2"
Grid.ColumnSpan="1"
Grid.Row="5"
Margin="5"
Name="BtnCancel"
IsCancel="True">Cancel</Button>

</Grid>
</WPF:ErrorProvider>
</Window>


我使用以下代码调用上述窗口:

var p = new GraphicSizeSelectPopUp();
var result = p.ShowDialog() ?? false;
p.Close();


我将其用作应用程序中的 Popup窗口,以从用户那里获取一些信息。我的问题是,当我单击 OK按钮时,什么也没有发生。 Cancel按钮完全按预期工作,这意味着控件从 ShowDialog方法返回到调用程序中。

据我了解WPF(仍然是新手),我要做的就是将 IsDefault属性设置为true,以使默认按钮执行相同的操作。但是,这不是我所看到的。当我在 ShowDialog方法之后的行上设置断点时,当我按下“确定”按钮时,它不会被击中。仅当我按下 Cancel按钮或关闭窗口时。

对不知情的建议?

最佳答案

IsDefault属性仅表示在按ENTER键时“单击”此按钮。它不会关闭对话框,也不会设置DialogResult,您必须在代码隐藏中手动进行操作:

private void BtnOK_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}


(设置 DialogResult也会关闭窗口)

关于wpf - WPF-默认按钮无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1975144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com