WPF:无法让我的控件获得焦点

发布于 2024-10-30 03:34:42 字数 840 浏览 1 评论 0 原文

我似乎无法让我的控件真正获得焦点:

XAML:

<Button Command={Binding SetGridToVisibleCommand} />
<Grid Visibility="{Binding IsGridVisible, Converter={con:VisibilityBooleanConverter}}">
    <TextBox Text={Binding MyText} IsVisibleChanged="TextBox_IsVisibleChanged" />
</Grid>

XAML.cs:

private void TextBox_IsVisibleChanged(Object sender, DependencyPropertyChangedEventArgs e)
{
    UIElement element = sender as UIElement;

    if (element != null)
    {
        Boolean success = element.Focus(); //Always returns false and doesn't take focus.
    }
}


The ViewModel does it's job of setting the IsGridVisible to true, and the converter does it's job by converting that value to Visibility.Visible (I snooped it).

I can't seem to get my control to actually take the focus:

XAML:

<Button Command={Binding SetGridToVisibleCommand} />
<Grid Visibility="{Binding IsGridVisible, Converter={con:VisibilityBooleanConverter}}">
    <TextBox Text={Binding MyText} IsVisibleChanged="TextBox_IsVisibleChanged" />
</Grid>

XAML.cs:

private void TextBox_IsVisibleChanged(Object sender, DependencyPropertyChangedEventArgs e)
{
    UIElement element = sender as UIElement;

    if (element != null)
    {
        Boolean success = element.Focus(); //Always returns false and doesn't take focus.
    }
}


The ViewModel does it's job of setting the IsGridVisible to true, and the converter does it's job by converting that value to Visibility.Visible (I snooped it).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光磨忆 2024-11-06 03:34:42

默认情况下,并非所有 UIElements 都可以聚焦,您是否尝试过设置 Focusable 设置为 true?

Not all UIElements can be focused by default, have you tried setting Focusable to true before trying Focus()?

琴流音 2024-11-06 03:34:42

我们在我们的应用程序中使用它:

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

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

    private static void HandleFocusPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var element = (UIElement)d;
        if ((bool)e.NewValue)
            element.Focus(); // Ignore false values.
    }
}

用法是:

<TextBox Text="{Binding FooProperty, UpdateSourceTrigger=PropertyChanged}"
         Grid.Column="1" HorizontalAlignment="Stretch"
         Style="{StaticResource EditText}" ui:Initial.Focus="True"/>

最初的想法来自SO,但找不到答案。
100% 免费代码背后:P

​​ HTH

We use this with in our application:

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

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

    private static void HandleFocusPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var element = (UIElement)d;
        if ((bool)e.NewValue)
            element.Focus(); // Ignore false values.
    }
}

And the usage is:

<TextBox Text="{Binding FooProperty, UpdateSourceTrigger=PropertyChanged}"
         Grid.Column="1" HorizontalAlignment="Stretch"
         Style="{StaticResource EditText}" ui:Initial.Focus="True"/>

The original idea came from SO, but couldn't find the answer.
100% free code behind :P

HTH

掩饰不了的爱 2024-11-06 03:34:42

我无法重现您的问题,它对我来说工作得很好,在新项目上尝试以下代码作为概念证明,看看它是否适合您:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <Button Content="Click" Click="Button_Click" />
        <Grid>
            <TextBox Name="NameTextBox" Text="ddd"
                     IsVisibleChanged="TextBox_IsVisibleChanged" />
        </Grid>
</StackPanel>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = sender as UIElement;

        if (element != null)
        {
            Boolean success = element.Focus(); //Always returns false and doesn't take focus.
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (NameTextBox.IsVisible)
            NameTextBox.Visibility = Visibility.Collapsed;
        else
            NameTextBox.Visibility = Visibility.Visible;
    }
}

I couldn't reproduce your problem, its working fine with me, try the following code on a new project as a proof of concept and see if it works with you:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <Button Content="Click" Click="Button_Click" />
        <Grid>
            <TextBox Name="NameTextBox" Text="ddd"
                     IsVisibleChanged="TextBox_IsVisibleChanged" />
        </Grid>
</StackPanel>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = sender as UIElement;

        if (element != null)
        {
            Boolean success = element.Focus(); //Always returns false and doesn't take focus.
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (NameTextBox.IsVisible)
            NameTextBox.Visibility = Visibility.Collapsed;
        else
            NameTextBox.Visibility = Visibility.Visible;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文