PublicAffair
PublicAffair

Reputation: 55

WPF Get the Cursor in the box

Hi all im having a Radio Button and Text box in front it, once i check the button the text box will become visible!

But i also want to my Cursor be visible in a text box to so easier for user to type \ how i can do these?!

thats my function

   private void SelectQualityChecked(object sender, RoutedEventArgs e)
    {
        txtSelectedQuantity.IsEnabled = true;

    }

Upvotes: 0

Views: 417

Answers (4)

sa_ddam213
sa_ddam213

Reputation: 43606

You can do this all in Xaml. No code behind is required.

First bind the RadioButton IsChecked property to the TextBox IsEnabled property, then use a Trigger on TextBox IsEnabled true to set the focus on the TextBox using the FocusManager.FocusedElement property.

Example:

<Window x:Class="WpfApplication13.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" Name="UI">
    <Grid>
        <StackPanel>
            <RadioButton x:Name="radioBtn" Content="Click me!"  />
            <TextBox x:Name="txtbox" IsEnabled="{Binding ElementName=radioBtn, Path=IsChecked}">
                <TextBox.Style>
                    <Style TargetType="{x:Type TextBox}">
                        <Style.Triggers>
                            <Trigger Property="IsEnabled" Value="True">
                                <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=txtbox}"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TextBox.Style>
            </TextBox>
        </StackPanel>
    </Grid>
</Window>

Upvotes: 3

saeed
saeed

Reputation: 2497

Try this code:

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
          textBox1.IsEnabled = true;
          textBox1.Focus();
        } 

        private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
            textBox1.IsEnabled = false;
        }

Upvotes: 2

Clinton Ward
Clinton Ward

Reputation: 2511

Try adding this. I think you should pass the boolean value after the check box click event

   private void SelectQualityChecked(object sender, RoutedEventArgs e)
    {
        txtSelectedQuantity.IsEnabled = SelectQuality.Checked;
        txtSelectedQuantity.Focusable = SelectQuality.Checked;
        if(SelectQuality.Checked)
        {
            txtSelectedQuantity.Focus();
        }
    }

Upvotes: 1

Uthistran Selvaraj
Uthistran Selvaraj

Reputation: 1371

Try this code:

private void radioButton1_Checked(object sender, RoutedEventArgs e)
    {
        textBox1.Visibility = System.Windows.Visibility.Visible;
        textBox1.Focus();
    }

Upvotes: 1

Related Questions