Textbox Highlighting Always Starting at 0 - wpf

This is really annoying.
I have basic textboxes which do not allow highlighting of single words/characters. Every time I try to select single words/characters, the highlight immediately jumps to the front of the box and highlights everything up to where I clicked.
<TextBox Name="txtTaskName" AutoWordSelection="False" Grid.Row="0" Grid.Column="2" Text="{Binding TaskName, Mode=TwoWay}" MaxWidth="300" VerticalAlignment="Stretch" HorizontalContentAlignment="Left" TextWrapping="Wrap" VerticalContentAlignment="Center"></TextBox>
Suggestions?

Set AutoWordSelection="False" on your TextBox
<TextBox AutoWordSelection="False"/>

Related

What does controls:TextBoxHelper.IsWaitingForData do?

I have a small WPF application that is using mahapp.metro for styling. There are 7 textboxes in the window with code similar to the examples below. When I run the application, it takes a lot of cpu, more than 90% if possible. When I deleted the controls:TextBoxHelper.IsWaitingForData="True", cpu usage dropped to almost 0% and I have not been able to find any issues. What does the TextBoxHelper.IsWaitingForData really mean?
<Canvas Grid.Column="0" Grid.Row="1">
<TextBox x:Name="SecurityStatusTextBox" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" IsReadOnly="True" Text="{Binding SelectedSecurityStatus, Mode=OneWay}"
Margin="50,50,0,0" Height="50" Width="250" FontSize="16"
controls:TextBoxHelper.ClearTextButton="True"
controls:TextBoxHelper.IsWaitingForData="True"
controls:TextBoxHelper.UseFloatingWatermark="True"
controls:TextBoxHelper.Watermark="{x:Static properties:Resources.SecurityStatus}"/>
<TextBox x:Name="BagTypeTextBox" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" IsReadOnly="True" Text="{Binding SelectedBagType, Mode=OneWay}"
Margin="325,50,0,0" Height="50" Width="250" FontSize="16"
controls:TextBoxHelper.ClearTextButton="True"
controls:TextBoxHelper.IsWaitingForData="True"
controls:TextBoxHelper.UseFloatingWatermark="True"
controls:TextBoxHelper.Watermark="{x:Static properties:Resources.BagType}"/>
</Canvas>
When the IsWaitingForData property is set to true an Effect is applied to the border of the TextBox and animation is applied to the Opacity property: https://github.com/MahApps/MahApps.Metro/blob/8a87a1b6ee7376e22930e465e8f3e85f4c5a73bc/src/MahApps.Metro/MahApps.Metro/Styles/Controls.TextBox.xaml
If you pay attention you will see that the TextBox gets a slight shadow that fades in and out when the IsWaitingForData property is set to true.
If this causes any issues for you should probably just set this property back to false.
You may also want to report the issue at GitHub: https://github.com/MahApps/MahApps.Metro/issues

Tabindex not working for materialDesign:TimePicker

<StackPanel Orientation="Vertical" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="1">
<TextBlock Text="{x:Static meta:MetaCommon.Returned}" Style="{StaticResource SectionHeader}" Margin="0,0,0,10" />
<TextBlock Text="{x:Static meta:MetaCommon.Date}" Style="{StaticResource ContentHeader}" />
<DatePicker TabIndex="10" HorizontalAlignment="Left" MinWidth="200" Margin="0,0,0,10" />
<TextBlock Text="{x:Static meta:MetaCommon.Time}" Style="{StaticResource ContentHeader}" />
<materialDesign:TimePicker TabIndex="11" HorizontalAlignment="Left" Width="200" materialDesign:HintAssist.Hint="" Margin="0,0,0,0" />
</StackPanel>
So the TabIndex works properly for everything except TimePicker. It will just skip to the next TabIndex. I'd like it to tab to the TimePicker in the proper order.
I'm not sure what the issue is because I've tried various ways and it just doesn't follow the tab order I set. The only time it gets selected is if I keep "tabbing" through the controls it eventually gets selected.
I tried setting a TabIndex of both "0" and "1" and I tried setting TabStop to both "True" and "False" but nothing seems to work apart from continuous tabbing and hoping it gets selected.
I even tried setting it in the code behind to just to make sure.
As far as I can gather, it could be that, because it is a part of the Material Design XAML Toolkit, it doesn't support direct TabIndex like native controls.

Stop WPF TextBox from growing

I have spent two hours researching how to avoid that my WPF TextBox Control grows when a long text has been typed in, but I have not been able to do it, even I have read some answers about it like these ones:
stopping-wpf-textbox-from-growing-with-text
wpf-allow-textbox-to-be-resized-but-not-to-grow-on-user-input
wpf-how-to-stop-textbox-from-autosizing
My code is the following:
<Grid>
<TextBox Margin="6,6,8,28" Name="textBox1" AcceptsTab="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" AcceptsReturn="True"/>
<CheckBox Content="Case sensitive" HorizontalAlignment="Left" Margin="7,0,0,2" Name="checkBox1" Height="16" VerticalAlignment="Bottom" />
</Grid>
One thing that I tried was:
MaxWidth={Binding ElementName=MyGrid, Path=ActualWidth}
But it did not work for me.
I also tried to add the following property to the Grid:
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Or
<Border x:Name="b" Grid.Column="1"/>
<TextBox Width="{Binding ActualWidth, ElementName=b}" ....... />
But it did not work either.
I need my control to grow when the user stretches the window, but not to when a long text is inserted.
Do you have a different answer for this problem that I might try?
UPDATE!
I have noticed something very strange: If I stretch manually the window, the textbox stops growing when a long text is inserted. That's fine. But I need to achieve this without having to stretch the window every time I run the program
Remove TextBox border and put it into ScrollViewer.
Try to do next:
<ScrollViewer MaxHeight={Binding ElementName=MyGrid, Path=ActualWidth} BorderThickness="0">
<TextBox Margin="6,6,8,28" Name="textBox1" AcceptsTab="True" TextWrapping="Wrap" AcceptsReturn="True"/>
</ScrollViewer>

Easy way to change Grid.Row and Grid.Column in many controls in WPF

I have this situation 1000 times already:
I have grid with 2 columns and many rows. Mostly i have TextBlock in first column and TextBox in second but not always. Now when I have 50 rows and I decide to move row 49 to the first row in grid, and I don't want to swap rows, I want to insert it to first place. Then I need to change values of Grid.Row and Grid.Column of all the rest of controls. This i driving me crazy. How can I make this easier?
You could use something like the markup extension i outlined in this question right from the beginning, if you then need to insert a new row that should be less trouble.
If you have formatted your xaml code well, you can hold Alt key to select and cut Grid.Row="x" in every line and hold Alt key to select and paste them one line down.
eg.
<TextBlock Grid.Row="0" Text="a"/>
<TextBlock Grid.Row="1" Text="b"/>
<TextBlock Grid.Row="2" Text="c"/>
<TextBlock Grid.Row="3" Text="d"/>
you want to move <TextBlock Grid.Row="3" Text="d"/> to the top. Just hold "Alt" key to select and copy:
Grid.Row="1"
Grid.Row="2"
Grid.Row="3"
Use Alt key again to paste them to replace the original
Grid.Row="0"
Grid.Row="1"
Grid.Row="2"
so you well get
<TextBlock Grid.Row="1" Text="a"/>
<TextBlock Grid.Row="2" Text="b"/>
<TextBlock Grid.Row="3" Text="c"/>
<TextBlock Grid.Row="3" Text="e"/>
Then just move <TextBlock Grid.Row="3" Text="d"/> to the top and change it Grid.Row to 0
It sounds like you'd be better off using a ListBox or ListView.

Label Target="{Binding ElementName=UIName}" bug, or is it just me?

I've been tring to get this example to work, but there seems to be a bug in the code Label Target code, in that when you click on the second label the focus is set to the DatePicker rather than the TextBox, regardless which label you click first, also when you have entered the date the second label still sets it focus to DatePicker.
<!-- Unbound Date of Birth field -->
<sdk:Label Content="Date of Birth" IsRequired="True" Margin="5" />
<StackPanel Orientation="Horizontal" Grid.Column="1">
<sdk:DatePicker Height="23" />
<sdk:DescriptionViewer Description="Please enter your date of birth."/>
</StackPanel>
<!-- ID Number field -->
<sdk:Label Grid.Row="1" Margin="5" Target="{Binding ElementName=tbIdNumber}" />
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1">
<TextBox x:Name="tbIdNumber" Height="23" Width="100"
Text="{Binding IdNumber, Mode=TwoWay,
ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<sdk:DescriptionViewer Target="{Binding ElementName=tbIdNumber}"/>
</StackPanel>
I found this example here on the MSDN link text. To me this seems to be a bug, or am I just losing it?
Apologies if this is a duplicate question... I have googled this and all the examples seem to be the same, in that the target element is not being focused to.
It seems a reasonable assumption that clicking the label ought to move the focus to the associated control. However the Label does not actually provide that function.

Resources