简体   繁体   中英

Extended WPF Toolkit Wizard - Set IsDefault = true for Next button

I've created a Wizard using the Extended WPF Toolkit by Xceed and need to set the IsDefault property of the Next button to true (Clicks button when Enter is pressed).

We have a user credentials page where clicking the Next button validates the credentials and performs other actions. I'd like the the page to behave like people would expect, type in credentials and press Enter to continue. Looking through the documentation, the only button properties that seem to be exposed for the built-in buttons are Visibility and Content . I can't figure out how to access all the other available properties for the Button Class on those built-in Wizard Buttons.

The Wizard control does not expose any property to set the default button. However, you can copy the default style from GitHub and adapt it like below. I set the IsDefault property for Next and Finish .

Copy this style to any parent resource dictionary. As it is and implicit style, it will be applied automatically to all Wizard s in scope. You can alternatively add an x:Key and reference it explicitly.

<Style TargetType="{x:Type xctk:Wizard}">
   <Style.Resources>
      <xctk:WizardPageButtonVisibilityConverter x:Key="WizardPageButtonVisibilityConverter"/>
   </Style.Resources>
   <Setter Property="Background" Value="Transparent" />
   <Setter Property="BorderBrush" Value="{DynamicResource {x:Static themes:ResourceKeys.ControlNormalBorderKey}}"/>
   <Setter Property="BorderThickness" Value="1" />
   <Setter Property="ItemsPanel">
      <Setter.Value>
         <ItemsPanelTemplate>
            <Grid/>
         </ItemsPanelTemplate>
      </Setter.Value>
   </Setter>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type xctk:Wizard}">
            <Border Background="{TemplateBinding Background}"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}"
                             Padding="{TemplateBinding Padding}">
               <Grid>
                  <Grid.RowDefinitions>
                     <RowDefinition Height="*" />
                     <RowDefinition Height="Auto" />
                  </Grid.RowDefinitions>

                  <ContentPresenter Content="{Binding CurrentPage, RelativeSource={RelativeSource TemplatedParent}}" />

                  <Border Grid.Row="1"
                                   BorderBrush="{TemplateBinding BorderBrush}"
                                   BorderThickness="0,1,0,0"
                                   Padding="7">
                     <StackPanel>
                        <Grid>
                           <Grid.ColumnDefinitions>
                              <ColumnDefinition Width="Auto" />
                              <ColumnDefinition Width="*" />
                           </Grid.ColumnDefinitions>
                           <Button Name="HelpButton"
                                            Grid.Column="0"
                                            MinWidth="75"
                                            Command="xctk:WizardCommands.Help"
                                            Content="{TemplateBinding HelpButtonContent}">
                              <Button.Visibility>
                                 <MultiBinding Converter="{StaticResource WizardPageButtonVisibilityConverter}">
                                    <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                      Path="HelpButtonVisibility"/>
                                    <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                      Path="CurrentPage.HelpButtonVisibility"/>
                                 </MultiBinding>
                              </Button.Visibility>
                           </Button>
                           <StackPanel Grid.Column="1"
                                                Orientation="Horizontal"
                                                HorizontalAlignment="Right">
                              <Button Name="BackButton"
                                               MinWidth="75"
                                               Command="xctk:WizardCommands.PreviousPage"
                                               Content="{TemplateBinding BackButtonContent}">
                                 <Button.Visibility>
                                    <MultiBinding Converter="{StaticResource WizardPageButtonVisibilityConverter}">
                                       <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                         Path="BackButtonVisibility"/>
                                       <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                         Path="CurrentPage.BackButtonVisibility"/>
                                    </MultiBinding>
                                 </Button.Visibility>
                              </Button>
                              <Grid MinWidth="75"
                                             Margin="2,0,0,0">
                                 <Button Name="NextButton"
                                                  IsDefault="True"
                                                  Command="xctk:WizardCommands.NextPage"
                                                  Content="{TemplateBinding NextButtonContent}">
                                    <Button.Visibility>
                                       <MultiBinding Converter="{StaticResource WizardPageButtonVisibilityConverter}">
                                          <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                            Path="NextButtonVisibility"/>
                                          <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                            Path="CurrentPage.NextButtonVisibility"/>
                                       </MultiBinding>
                                    </Button.Visibility>
                                 </Button>
                                 <Button Name="FinishButton"
                                                  IsDefault="True"
                                                  Command="xctk:WizardCommands.Finish"
                                                  Content="{TemplateBinding FinishButtonContent}">
                                    <Button.Visibility>
                                       <MultiBinding Converter="{StaticResource WizardPageButtonVisibilityConverter}">
                                          <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                            Path="FinishButtonVisibility"/>
                                          <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                            Path="CurrentPage.FinishButtonVisibility"/>
                                       </MultiBinding>
                                    </Button.Visibility>
                                 </Button>
                              </Grid>
                              <Button Name="CancelButton"
                                               Margin="7,0,0,0"
                                               MinWidth="75"
                                               Command="xctk:WizardCommands.Cancel"
                                               Content="{TemplateBinding CancelButtonContent}">
                                 <Button.Visibility>
                                    <MultiBinding Converter="{StaticResource WizardPageButtonVisibilityConverter}">
                                       <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                         Path="CancelButtonVisibility"/>
                                       <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                         Path="CurrentPage.CancelButtonVisibility"/>
                                    </MultiBinding>
                                 </Button.Visibility>
                              </Button>
                           </StackPanel>
                        </Grid>
                     </StackPanel>
                  </Border>
               </Grid>
            </Border>
            <ControlTemplate.Triggers>
               <Trigger Property="IsEnabled" Value="True" SourceName="FinishButton">
                  <Setter Property="Visibility" Value="Visible" TargetName="FinishButton"/>
               </Trigger>
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

If you need a more flexible approach, you will have to create a custom control derived from Wizard that exposes a dependency property, eg of an enum type for the button, to be able to specify the default button. You could then get the template parts (the buttons) in the control and set their IsDefault property accordingly in the property changed callback of the dependency property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM