XAML触发按钮设置的可见性focusmanager.focusedelement仅触发一次

时间:2016-08-09 20:52:58

标签: .net wpf xaml triggers

我试图根据按钮的折叠可见性在XAML中更改焦点。触发器将运行我创建的转换器一次,而不是再次。我无法使用代码,无法命名UIElements。

我尝试了很多事情,例如使用Datatrigger而不是使用AutoCodeSuccess绑定来查看下面的可见性。同样,它只会运行转换器一次。对按钮可见性的绑定没有问题,所以我假设我可以将该属性与Datatrigger一起使用。

                    <Button Grid.Column="2" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="5" Visibility="{Binding AutoCodeSuccess, Converter={StaticResource ReverseBoolToVisCollapse}, FallbackValue=Visible}" Content="Auto-Code" Width="75" Command="{Binding AutoCodeClick}">
                        <Button.Style>
                            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource AccentedSquareButtonStyle}">
                                <Style.Triggers>
                                    <Trigger Property="Visibility" Value="Collapsed">
                                        <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource GetNextLogicalFocus}}"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                    </Button>

这是转换器:

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
    Dim curUIElement = DirectCast(value, UIElement)
    curUiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next))
    Return FocusManager.FocusedElementProperty
End Function

任何可以使数据触发器正常工作的建议?我已检查过我之前涉及数据触发器的代码(不是可见性),之前从未遇到任何问题。这是在.NET Framework 4中。

1 个答案:

答案 0 :(得分:1)

PredictFocus有一种解决方法,但您必须使用路线(向上/向下/向左/向右),因为下一步不是有效选择。

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
    Dim curUIElement = DirectCast(value, UIElement)
    Return curUiElement.PredictFocus(FocusNavigationDirection.Down)
End Function

使用DataTrigger

<Button Grid.Column="2" Margin="5" Width="75" 
        Content="Auto-Code" Command="{Binding AutoCodeClick}">
    <Button.Style>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource AccentedSquareButtonStyle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding AutoCodeSuccess}" Value="false">
                    <Setter  Property="Visibility" 
                             Value="Collapsed" />
                    <Setter  Property="FocusManager.FocusedElement" 
                             Value="{Binding RelativeSource={RelativeSource Self}, 
                                     Converter={StaticResource GetNextLogicalFocus}}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>