简体   繁体   English

突出显示已激活按钮WPF

[英]Highlight the Activated Button WPF

I have a wpf form that is called by a static method in much the same way as MessageBox.Show() ; 我有一个wpf表单,该表单由静态方法调用,与MessageBox.Show()大致相同; the button layout is defined in the method, and can be customised, within certain parameters. 按钮布局是在方法中定义的,并且可以在某些参数内进行自定义。 Each button is added with a style that is BasedOn a central style. 每个按钮都添加了这是一个风格的BasedOn中央风格。

Part of this style is a trigger that highlights the default button, so the user knows what will happen if they press enter . 这种样式的一部分是突出显示默认按钮的触发器,因此用户知道如果按enter将会发生什么。 However, if the user presses tab a few times, there is a little dotted border that moves into each of the buttons in turn and then this is the button they will get when they press enter. 但是,如果用户几次按下Tab键,则会有一个虚线虚线框依次移入每个按钮,然后就是他们按下Enter键时将得到的按钮。 I would like my highlight to start on the default button but follow this... ?focus? 我希望我的突出显示从默认按钮开始,但要遵循此...焦点? around if the user chooses to tab between the buttons. 如果用户选择在按钮之间进行制表,则单击。

I had thought that the property to trigger on in the <ControlTemple.Triggers> would be IsFocused , but this does not seem to behave as I expected - here is a snippet of my xaml code as it stands: 我以为要在<ControlTemple.Triggers>触发的属性将是IsFocused ,但这似乎不符合我的预期-这是我的xaml代码的片段:

<!--....-->
   <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="border" 
                        BorderThickness="1"
                        Padding="0" 
                        BorderBrush="WhiteSmoke" 
                        CornerRadius="2" 
                        Background="{TemplateBinding Background}">
                    <ContentPresenter x:Name="Contents" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="RenderTransform" TargetName="Contents">
                            <Setter.Value>
                                <TranslateTransform X="0" Y="2"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsDefault" Value="true">
                        <Setter Property="BorderThickness" TargetName="border" Value="3"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="White"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="IsDefault" Value="True"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="False">
                        <Setter Property="IsDefault" Value="False"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
<!--....-->

I thought that the focus would set the default when it triggered, and this would cause the highlight (3px white border) to show as the user pressed tab. 我认为焦点将在触发时设置默认值,这将导致突出显示(3px白色边框)随着用户按下选项卡而显示。

What is the property that changes when the user presses tab (and/or why is it not responding for me?) 用户按下Tab键时属性会发生什么变化(和/或为什么它不响应我?)

-------------- ************ EDIT ************------------- -------------- ************编辑************ ----------- -

Attempting to add lines to the code behind to add triggers directly. 尝试在后面的代码中添加行以直接添加触发器。 The foreach loop tracks through a list of custom class items that are basically slightly modified buttons; foreach循环会跟踪自定义类项目的列表,这些类基本上是稍作修改的按钮; the b.Style is of type Style, but is looked up from some resources b.Style类型为Style,但是从某些资源中查找

        foreach (Buttons b in buttonList)
        {
            if (i > 0)
            {
                totalWidthNeeded = totalWidthNeeded + gapPixels;
                ColumnDefinition gapColumn = new ColumnDefinition();
                gapColumn.Width = gapWidth;
                ButtonRowGrid.ColumnDefinitions.Add(gapColumn);
                i++;
            }

            Button thisButton = new Button();

            var setter = new Setter() { Property = Button.IsDefaultProperty, Value = true};
            var trigger = new Trigger() { Property = Button.IsFocusedProperty, Value=true, Setters = { setter } };

            b.Style.Triggers.Add(trigger);

            thisButton.Style = b.Style;

Sadly, this code does not make a trigger that has a visible effect from the IsDefault trigger shown in the xaml above (I have removed the two IsFocused triggers). 令人遗憾的是,此代码不会从上面的xaml中显示的IsDefault触发器中产生可见效果的触发器(我已删除了两个IsFocused触发器)。

I'm not exactly sure what you want but from my understanding you need: 我不确定您要什么,但据我了解您需要:

  1. Highlight default button 高亮显示默认按钮
  2. When used tab-out existing button to next one highlight currently selected button 当使用跳出现有按钮到下一个突出显示当前选择的按钮时

Based on my understanding I've created an example which may help you out: 根据我的理解,我创建了一个示例,可以为您提供帮助:

<Window.Resources>
    <ResourceDictionary>
        <Style TargetType="Button">
            <Setter Property="Padding" Value="5" />
            <Setter Property="Margin" Value="5" />
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="IsDefault" Value="True" />
                    <Setter Property="BorderThickness" Value="3" />
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</Window.Resources>
<StackPanel FocusManager.FocusedElement="{Binding ElementName=btnDefault}">
    <Button>Button 1</Button>
    <Button>Button 2</Button>
    <Button x:Name="btnDefault">Button 3</Button>
    <Button>Button 4</Button>
    <Button>Button 5</Button>
    <Button>Button 6</Button>
    <Button>Button 7</Button>
</StackPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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