【问题标题】:WpfToolkit AutoCompleteBox style trigger IsFocused NOT firingWpfToolkit AutoCompleteBox 样式触发器 IsFocused 未触发
【发布时间】:2013-06-14 03:52:08
【问题描述】:

我正在使用最新版本的 WPF 工具包。我正在尝试设置 AutoCompleteBox 的样式,但似乎无法让“IsFocused”触发器工作。基本上我希望它表现得像我的 TextBox 风格,所以我为 AutoCompleteBox 做了一个。我什至尝试将我的 TextBox 样式分配给 AutoCompleteBox 的 TextBoxStyle 属性,但我仍然没有看到 IsFocused 触发器触发。

我确实尝试在后面的代码中玩耍,并注意到如果我覆盖 OnGotFocus 和 OnLostFocus,它们就永远不会被调用。但是,如果我将一些事件处理程序连接到 GotFocus 和 LostFocus 事件,那么我终于看到发生了一些事情。如果连接到事件是查看 IsFocused 更改的唯一方法,那么这似乎是一个丑陋的 hack。是否有一些解决方法或我应该做一些不同的事情?

我的 TexBox 样式

<Style TargetType="TextBox" x:Key="TextBoxStyle">
    <Setter Property="Background" Value="{StaticResource TextBoxBackground}"/>
    <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
    <Setter Property="CaretBrush" Value="{StaticResource Foreground}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                   <ScrollViewer x:Name="PART_ContentHost" Margin="1"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="{StaticResource TextBoxBackgroundSelected}"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="true">
                        <Setter Property="Background" Value="{StaticResource TextBoxBackgroundSelected}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我的自动完成框样式

<Style TargetType="WpfToolkitInput:AutoCompleteBox" x:Key="AutoCompleteBoxStyle">
<Setter Property="Background" Value="{StaticResource TextBoxBackground}"/>
<Setter Property="Foreground" Value="{StaticResource Foreground}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="TextBoxStyle" Value="{StaticResource TextBoxStyle}"/>
<Style.Triggers>
  <Trigger Property="IsMouseOver" Value="true">
    <Setter Property="Background" Value="{StaticResource TextBoxBackgroundSelected}"/>
  </Trigger>
  <Trigger Property="IsFocused" Value="true">
    <Setter Property="Background" Value="{StaticResource TextBoxBackgroundSelected}"/>
  </Trigger>
</Style.Triggers>

有什么想法吗?

感谢您的宝贵时间!

【问题讨论】:

    标签: wpf wpftoolkit


    【解决方案1】:

    我没能找到一个优雅的解决方案。我最终继承自 AutoCompleteBox 并使用事件处理程序执行我的逻辑。

    public class AutoCompleteTextBox : AutoCompleteBox
    
    ...
    
    public AutoCompleteTextBox()
    {
       // Register the event handler for GotFocus.
       base.GotFocus += this.AutoCompleteTextBoxGotFocus;
    
       // Register the event handler for LostFocus.
       base.LostFocus += this.AutoCompleteTextBoxLostFocus;
    }
    

    这里没什么特别的。我只是做了我自己的类型。我唯一要做的就是在构造函数中连接 get 和 lost 的焦点事件处理程序。我不知道为什么,但 IsFocused 触发器不接收这些事件。此外,如果您尝试覆盖这些:“OnGotFocus”或“OnLostFocus”,它们也不起作用。因此,如果 AutoCompleteBox 聚焦,我能找到的唯一方法是直接注册到事件。如果以后有人遇到这种情况并有更好的方法,请发表评论。

    【讨论】:

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