开发者

WPF: Can't get my control to take focus

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).


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


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


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜