I dont know if this works but is there any chance to move the cursor inside a Textbox automaticaly for one position(1 space bar click). For example:
I run the application and the cursor inside the Textbox moves automatically for one space bar
my textbox:
<TextBox TextChanged="Searchbox_TextChanged" x:Name="Testing" Margin="2" Grid.Row="1" Text="{Binding SearchSmthg, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalContentAlignment="Center" Controls:TextboxHelper.ClearTextButton="True" Controls:TextboxHelper.Watermark="Search ..."/>
use CaretIndex and FocusManager:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid FocusManager.FocusedElement="{Binding ElementName=Focustext}">
<TextBox x:Name="Focustext" Text=" " CaretIndex="1" MaxHeight="25" />
</Grid>
</Window>
Edit: add a GotFocus event handler and set CaretIndex=1 there
XAML:
<TextBox GotFocus="Testing_GotFocus"
Code Behind:
private void Testing_GotFocus(object sender, RoutedEventArgs e)
{
//make sure your Textbox is not empty..
Testing.CaretIndex = 1;
}
Related
I have a textbox in WPF window to show the path of file and browse button beside the textbox, I am selecting a file from (tfs) browser and I want to show the full path aligned from right side but it is showing from left side only.
The Image show the path showed from left
I tried HorizontalAlignment,VerticalAlignment,HorizontalContentAlignment but nothing is helping.
I want to display it from right end.
I have referred
Show right end of text line in TextBox
TextBox.TextAlign right-side alignment has no effect in certain conditions?
Can anyone Help?
Here is the code
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label x:Name="label" Content="Source :
" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="1.144,1.871" Margin="10,27,0,0"/>
<TextBox x:Name="txtSourceFile" HorizontalAlignment="Left" Height="23" Margin="67,34,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="400" TextAlignment="Justify" HorizontalContentAlignment="Right" />
<Button x:Name="btnBrowseSource" Content="Browse" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="379,62,0,0" Click="btnBrowseSource_Click" />
</Grid>
</Window>
OK, so its not that trivial as setting a property on text box what you can do is declare this method:
private void ScrollToEnd()
{
var sv = (ScrollViewer)txtSourceFile.Template.FindName("PART_ContentHost", txtSourceFile);
sv.ScrollToHorizontalOffset(sv.ExtentWidth);
}
And call it whenever your textbox value is changed.
I had the same problem and added a method for the Loaded and TextChanged event
<TextBox Loaded="FolderTB_ScrolltoEnd" TextChanged="FolderTB_ScrolltoEnd" ... />
and for setting the offset
private void FolderTB_ScrolltoEnd(object sender, EventArgs e)
{
((TextBox)sender).ScrollToHorizontalOffset(((TextBox)sender).ExtentWidth);
}
how to set focus on this when i log in to my application ,when log in window will open i want to type the password on this without using mouse
<PasswordBox x:Name="passwordbox" Grid.Row="1" Grid.Column="1" Margin="5,35,5,5" Width="280" Height="27" app:PasswordBoxAssistant.BindPassword="true" app:PasswordBoxAssistant.BoundPassword="{Binding TechUserModel.UserId,Mode=TwoWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center">
I have tried various solutions for this scenario and the most effective that I have found is using FocusManager.FocusedElement:
<Window x:Class="StackOverflow.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="300" Width="300"
FocusManager.FocusedElement="{Binding ElementName=Box}">
<Grid>
<TextBox x:Name="Box"/>
</Grid>
</Window>
SOLUTION:
passwordbox.Focus();
or in .xaml file include the following in the element as its attributes.
FocusManager.FocusedElement="{Binding ElementName=passwordbox}">
NOTE:
If you want to decide which control to give focus to when the form first appears, put the code in the Loaded event.
Loaded += (o, e) => {
passwordbox.Focus();
};
I have a window with a ContentControl binding:
<ContentControl Content="{Binding CurrentViewModel}" />
I also have an empty user control binds to the ContentControl:
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</UserControl>
When I run and press the tab keyboard I get a dotted rectangle around the content control. How can I disable this?
I tried using Focusable="False" and FocusVisualStyle="{x:Null}" without success...
Have you tried setting IsTabStop="False", for example...
<ContentControl Height="200" Width="200" IsTabStop="False">
<ContentControl.Content>
<TextBox/>
</ContentControl.Content>
</ContentControl>
and I would suggest you combine it with this trick:
Loaded += (sender, e) =>
MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
from the answer to this question: WPF and initial focus
Override the ContentControl Style and add property FocusVisualStyle as null and implement by using style property
<ContentControl Height="200" Width="200" FocusVisualStyle="{x:Null}">
<ContentControl.Content>
<local:UserControl1/>
</ContentControl.Content>
</ContentControl>
I met a Focus related problem in UserControl:
Suppose we have a UserControl like this:
<UserControl x:Class="_20130826.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<ListBox>
<ListBoxItem>
<TextBlock Text="text1" />
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="text2" />
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="text3" />
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="text4" />
</ListBoxItem>
</ListBox>
</StackPanel>
</UserControl>
And the MainWindow.xaml like this:
<Window x:Class="_20130826.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:temp="clr-namespace:_20130826"
Title="MainWindow">
<StackPanel>
<Button Content="Deactive UserControl" />
<StackPanel>
<Button Name="Button1" Content="Active UserControl" />
<ContentControl>
<temp:UserControl1 />
</ContentControl>
</StackPanel>
</StackPanel>
</Window>
Step 1: Click text1 TextBlock inside UserControl, and default the
background changes to deeper.
Step 2: Click 'Deactive UserControl' button, so the text1 background
turns to lighter.
Step 3: Click 'Active UserControl' button, then ...
I want the text1 background changed to deeper, which means the UserControl has been focused/actived.
How can I achieve this?
If you want to set the Focus to the UserControl when the Button("Active UserControl") gets pressed, we can add a Click event handler and assign focus to the UserControl.
So say something like:
<Window x:Class="_20130826.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:temp="clr-namespace:_20130826"
Title="MainWindow">
<StackPanel>
<Button Content="Deactive UserControl" />
<StackPanel>
<Button Name="Button1" Content="Active UserControl" Click="Button1_OnClick" />
<ContentControl>
<temp:UserControl1 x:Name="myUserControl" />
</ContentControl>
</StackPanel>
</StackPanel>
</Window>
and in MainWindow.xaml.cs:
private void Button1_OnClick(object sender, RoutedEventArgs e) {
FocusManager.SetFocusedElement(this, myUserControl);
}
Now when you click the "Active UserControl" Button, Focus will switch to the UserControl. However this will not give you that "deeper" state on the ListBoxItem as it still doesnt have Focus. So to sort that
In UserControl1.xaml.cs add:
protected override void OnGotFocus(RoutedEventArgs e) {
base.OnGotFocus(e);
if (!Equals(e.OriginalSource, this))
return;
TraversalRequest tRequest = new TraversalRequest(FocusNavigationDirection.Next);
MoveFocus(tRequest);
}
What this does is when the UserControl gets focus, it moves Focus to the next element within it which would be the element you clicked initially.
You can get a demo of your code tweaked with this: Here
I have seen this question:
How to keep WPF TextBox selection when not focused?
And have implemented the solution there, so that my textbox shows the selection, even when it does not have focus.
However, when I change the selection start or length, nothing changes visually in the textbox. Also, when I scroll the textbox programatically and it does not have focus, the selection brush does not move with the text as it scrolls.
If you define a separate focus scope in XAML to maintain the selection (see StackPanel below) and you set the focus in the TextBox once (in this case when the Window opens using FocusManager.FocusedElement) then you should see your selection change programatically.
Here is some sample code to get you started:
<Window x:Class="RichTextFont2.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window"
Height="400" Width="400"
FocusManager.FocusedElement="{Binding ElementName=myTextBox}"
FontSize="20">
<DockPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox x:Name="myTextBox"
Grid.Row="0"
Text="Text that does not loose selection."
TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto">
</TextBox>
<StackPanel Grid.Row="1" FocusManager.IsFocusScope="True">
<Button Content="Select Text" Click="Button_Click_MoveTextBox"/>
</StackPanel>
</Grid>
</DockPanel>
</Window>
Here is some code to handle the button click event:
private void Button_Click_MoveTextBox(object sender, RoutedEventArgs e)
{
if (myTextBox.SelectionStart >= myTextBox.Text.Length)
{
myTextBox.SelectionStart = 0;
}
else
{
myTextBox.SelectionStart += 9;
}
myTextBox.SelectionLength = 6;
myTextBox.LineDown();
}