WPF - set focus on textbox - c#

In my WPF application I have many text boxes on one page. I want to set focus on the first text box. I've Googled it and tried different solutions.
XAML Code :
<StackPanel Width="350" HorizontalAlignment="Left" >
<TextBlock x:Name="CustNamelbl" Text="C U S T O M E R N A M E"
Style="{StaticResource LightBoldTxtblkStyle }"/>
<Grid Height="35">
<TextBox x:Name="CustName" Style="{StaticResource Txtbox}"
LostFocus="CustName_LostFocus_1" TabIndex="1"
CommandManager.PreviewExecuted="CustName_PreviewExecuted"
ContextMenu="{x:Null}" PreviewTextInput="CustName_PreviewTextInput"
Margin="0,0,0,0" GotFocus="CustName_GotFocus"/>
<Rectangle Fill="White" Height="2" Opacity="0.2"
VerticalAlignment="Bottom"></Rectangle>
</Grid>
</StackPanel>
C# Code :
CustName.Focus();
Keyboard.Focus(CustName);
Using this code I am getting Focus on that TextBox. I'm also able to open system keyboard. However, I am not able to type anything in that TextBox. Also, that focused cursor is not a blinking cursor, but just a steady cursor.

Give your TextBox a name and then you can call in your Window-Tag
FocusManager.FocusedElement="{Binding ElementName=YOURTBNAME}"

Related

Why can I not use .AutoSize in a label

In my program, I have a TextBlock where my result text is shown. In a textbox I can just use TextWrapping="Wrap" and I can scroll down if it's still too much for the textbox. Now in my TextBlock that I talked about I wanted to have the same thing and used TextWrapping="Wrap" again. That works except for the scrolling thing. What can I do now to let it scroll like in the textbox?
Use following structure
<ScrollViewer HorizontalScrollBarVisibility="Auto" x:Name="scrView" VerticalScrollBarVisibility="Auto" Width="100" Height="100">
<TextBlock Width="{Binding Path=ActualWidth, ElementName=scrView}" TextWrapping="Wrap">your text</TextBlock>
</ScrollViewer>

How can I disable automatic focus on an element in wpf?

I am writing a WPF application using Infragistics package, which functionality requires opening new windows. One of the windows I open has 5 buttons on it. Buttons become highlighted when I focus on them. When I do not focus on any button, the first button is automatically in focus (it is highlighted like a focused button). I just want all the buttons have same style until I focus one of them.
I just didn't manage to find the solution.
Here is the part of code with the button
<DockPanel>
<ToolBarTray DockPanel.Dock="Top" IsLocked="True">
<ToolBar Name="toolBar" Band="1" BandIndex="1" BorderThickness="1" BorderBrush="LightSteelBlue">
<Button Name="button1" Click="button1_Click" BorderBrush="LightSteelBlue" Padding="3" IsEnabled="False" HorizontalContentAlignment="Left">
<StackPanel Orientation="Horizontal" >
<TextBlock VerticalAlignment="Center">Button 1</TextBlock>
</StackPanel>
</Button>
<Button HorizontalContentAlignment="Left" Margin="5,0,0,0" Name="button2" Click="button2_Click" BorderBrush="LightSteelBlue" Padding="5,3,5,3" ToolTip="Load and display debug data shot to check filtering process"
IsEnabled="False">
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center">Button 2</TextBlock>
</StackPanel>
</Button>
</ToolBar>
</ToolTray>
</DockPanel>
Now when I do not focus the mouse on any button, the button1 looks like it is focused. I want to look it as a button2.

WPF Richtextbox unable to get focus, use scrollbars etc

Issue: I am unable to select the Richtextbox 'rtbx_COMMENTS' either using keyboard or mouse. Having tried and retried various properties and values for this control I am at a loss as to why this is not selectable
The controls useage is read only but it needs to be selectable so it can be vertically scrolled
I have manually added the properties IsEnabled="True" and Focusable ="True" into the XAML definition for the control as a belts and braces effort to try and get this control selectable. I even set the stackpanel as focusable wondering if the richtextbox was inheriting properties from it
In the codebehind, I use ONLY the follow to manipulate this control, clearing the control, appending text etc
rtbx_COMMENTS.Document.Blocks.Clear();
rtbx_COMMENTS.AppendText(_x.COMMENT);
where _x.COMMENT refers to a string, example being
"Select If : Gas Issue affecting every burner\r Smell of gas when appliance not in use"
and the XAML
<StackPanel x:Name="spnl_COMMENTS" Orientation="Horizontal" Margin="5,2.5,5,0" HorizontalAlignment="Center" VerticalAlignment="Center" Width="780" Focusable="True">
<Label x:Name="lbl_COMMENTS" Content="COMMENTS" Margin="5,1,0,0" BorderThickness="1" BorderBrush="#FF45CDDA" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="18" Foreground="#FFABA63A" Width="115" TabIndex="-1" Height="145"/>
<RichTextBox x:Name="rtbx_COMMENTS" IsEnabled="True" Focusable ="True" BorderBrush="#FF45CDDA" Padding="2" Margin="0,1,5,0" Background="{x:Null}" Foreground="#FFE48989" IsReadOnly="False" FontSize="16" MinWidth="655" Width="655" Height="145" TabIndex="19" IsHitTestVisible="False" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" MinHeight="145" IsDocumentEnabled="True" Cursor="Arrow" UseLayoutRounding="False" AllowDrop="False" IsTabStop="True" >
</RichTextBox>
</StackPanel>
Your IsHitTestVisible="False" property in XAML, causes the problem of not selecting the text in rich text box.

How to create a floating button in a WPF

I am trying to create a floating button in my WPF application. Currently, I have the following:
<Grid>
<Button Style="{DynamicResource MetroCircleButtonStyle}" Background="#E0E010" Width="50" Height="50" Name="testBtn" Click="testBtn_Click" Margin="374,184,93,35">
<Rectangle Height="20" Width="20" RenderTransformOrigin="0.47,0.456">
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_add}" />
</Rectangle.Fill>
</Rectangle>
</Button>
<ListBox Width="200" Height="200" Name="lbox1" BorderBrush="Black" BorderThickness="5">
<Grid Width="175">
<ScrollViewer CanContentScroll="False" VerticalScrollBarVisibility="Auto" Margin="107,0,0,0"></ScrollViewer>
</Grid>
</ListBox>
</Grid>
which has a list box with a scroll viewer and a button outside the list box. When I click the button it prints "Hello World" in the list box. I would like to add the button inside the list box and as I scroll down inside the list box have the button be floating. Is there any way to do this?
Note: I am a complete beginner when it comes to WPF.
EDIT: Here is essentially what I want:
So going off of #omarmallat's comment, the way to do this is
<Button x:Name="addRowButton"
Width="50"
Height="50"
Margin="1275,330,25,25"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Background="#2EFF00" />
I had a margin so the button wasn't directly in the corner and provided a little but of space. Make sure the button tag is inside your grid tag!

Open popup at the location of the button clicked

I am trying to create a popup that opens similar to the window preview option in Windows 8 taskbar. Basically, when I click or hover on a button, it opens a popup with basic information just above the button. Here is an example.
Right now, I am able to open the popup at either the extreme left or extreme right side of the page, using the code below.
<Frame x:Name="PopUpFrame" HorizontalAlignment="Left" Visibility="Hidden" Height="400" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Bottom" Width="400" Source="/BasicApp;component/StandingOrderPopUp.xaml" NavigationUIVisibility="Hidden"/>
I have 3 different buttons in the button bar, so I cannot set the fixed margin. I am trying to set the same in code but I am unable to get the absolute position and convert it to margin property. I am not sure, if there is a better solution either.
Edit:
Tried using popup but it doesn't open on button click.
<Popup x:Name="PopUpFrame" Placement="Top" PlacementTarget="{Binding ElementName=StandingOrderButton}" Width="400" Height="400">
<DockPanel Background="#770081a7">
<Canvas DockPanel.Dock="Bottom" x:Name="PopupButtonBar" Height="50" VerticalAlignment="Bottom">
<Button Height="30" Width="125" HorizontalAlignment="Right" VerticalAlignment="Center" Canvas.Top="10" Content="CLOSE" Foreground="White" Background="#ff0081a7" BorderBrush="White" FontFamily="Trebuchet MS" Canvas.Left="10" />
</Canvas>
<Label Margin="5,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DockPanel.Dock="Top" Content="STANDING ORDERS" Foreground="#ffffffff"></Label>
<Grid DockPanel.Dock="Top">
<Border BorderThickness="2" BorderBrush="#FFff7a00"></Border>
<RichTextBox Margin="2" Foreground="#FF0081a7" FontFamily="Trebuchet MS" IsEnabled="False"/>
</Grid>
</DockPanel>
</Popup>
And here is the event handler.
Private Sub StandingOrder_Click(sender As Object, e As RoutedEventArgs)
PopUpFrame.Visibility = Windows.Visibility.Visible
End Sub
Edit:
Never mind. I am an idiot. Instead of setting IsOpen property, I set the visibility. :(
It works perfectly, although, I had to copy the whole design from the separate page to this one. Still better than nothing. Only problem now is if I click on something else, I will have to write code to make sure popup is closed.
You can use a Popup control, coupled with it's Placement property to display your popup based on the current location of your buttons.
<Popup Placement="Top" PlacementTarget="{Binding ElementName=yourButton}" ... />
Inside your Popup, you can place your UserControl or any content element which will act as the popup's content.
See here for further information on popup placements, and here for a tutorial on WPF Popup controls.
Think about using ContextMenu, and set it's content you whatever you want. Thats is the solution for you.
This is how i defined one of my popups:
<Popup x:Name="btnPowerPopup" Placement="Mouse" StaysOpen="False">
....
</Popup>
you can user from the code behind on a button click or something :
btnPowerPopup.IsOpen = true;
and when the job is done:
btnPowerPopup.IsOpen = false;
But StaysOpen="False" let's you close the PopUp when clicking somewhere else.

Categories

Resources