6

假设我有一个绑定到 VM 上的项目列表的项目控件。数据模板内部是一个文本框。如何将焦点设置到 XAML 或 VM 中的第一个文本框?

提前感谢您的帮助!

<ItemsControl ItemsSource="{Binding UsageItems}" Grid.Row="1" Focusable="False">
<ItemsControl.ItemTemplate>
    <DataTemplate>
            <Grid Margin="0,0,0,3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <TextBlock Text="{Binding Month}" Style="{StaticResource Local_MonthLabel}" />
            <core:NumericTextBox Value="{Binding Actual, Mode=OneWay}" Style="{StaticResource Local_ActualUsageEntry}" Grid.Column="2"/>

            <core:ValidationControl Instance="{Binding Model}" Grid.Column="4" PropertyName="{Binding MonthNumber, StringFormat=AdjustedUsage{0}}">
                <core:NumericTextBox Value="{Binding Adjusted}"  DefaultValueIfNull="0" Style="{StaticResource Local_AdjustUsageEntry}" x:Name="AdjustmentEntry" inventoryLocationSetup:InitialFocusBehavior.Focus="True" />
            </core:ValidationControl>

            <telerik:RadComboBox ItemsSource="{Binding Converter={StaticResource Converter_EnumToEnumMemberViewModel}, Mode=OneTime, Source={x:Type Enums:UsageAdjustmentTypes}}" SelectedValue="{Binding Code, Mode=TwoWay}" Grid.Column="6" Style="{StaticResource Local_CodeSelector}"/>

        </Grid>
    </DataTemplate>
</ItemsControl.ItemTemplate>

4

1 回答 1

5

我使用附加行为:

public static class InitialFocusBehavior
{
    public static bool GetFocus(DependencyObject element)
    {
        return (bool)element.GetValue(FocusProperty);
    }

    public static void SetFocus(DependencyObject element, bool value)
    {
        element.SetValue(FocusProperty, value);
    }

    public static readonly DependencyProperty FocusProperty =
        DependencyProperty.RegisterAttached(
        "Focus",
        typeof(bool),
        typeof(InitialFocusBehavior),
        new UIPropertyMetadata(false, OnElementFocused));

    static void OnElementFocused(
        DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = depObj as FrameworkElement;
        if (element == null)
            return;
        element.Focus();
    }
}

然后在 XAML 中将它绑定到 True 以获取您想要关注的元素:

<TextBox Width="200" Height="20" local:InitialFocusBehavior.Focus="True" />

=== 更新 ===

抱歉,上面的代码只是展示了如何使用行为来将焦点放在页面上的控件上,如果您想对 ItemControl 中第一项中的元素执行此操作,那么您必须改为将行为应用于ItemsControl 本身,然后在您的更新处理程序中通过执行以下操作找到子项:

static void OnElementFocused(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
    ItemsControl itemsControl = depObj as ItemsControl;
    if (itemsControl == null)
        return;
    itemsControl.Loaded += (object sender, RoutedEventArgs args) =>
    {
        // get the content presented for the first listbox element
        var contentPresenter = (ContentPresenter)itemsControl.ItemContainerGenerator.ContainerFromIndex(0);

        // get the textbox and give it focus
        var textbox = contentPresenter.ContentTemplate.FindName("myTextBox", contentPresenter) as TextBox;
        textbox.Focus();
    };
}

您会注意到我将焦点设置在 OnLoaded 处理程序中,因为我假设在首次创建控件时尚未附加项目。

此外,您可能已经通过“local”发现它只是 InitialFocusBehavior 类定义的命名空间,您需要在 xaml 顶部添加类似这样的内容:

xmlns:local="clr-namespace:YourProjectNamespace"
于 2013-05-01T23:44:31.127 回答