I have a sample UI and trying to move focus to previous element on pressing Key Up. Can someone help me how to move focus to previous textbox from current Textbox or another elements.
Here is my Sample UI
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:Sample_Application"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel PreviewKeyDown="DockPanel_PreviewKeyDown" PreviewKeyUp="DockPanel_PreviewKeyUp" KeyUp="DockPanel_KeyUp" >
<TextBox DockPanel.Dock="Top" Margin="18,0,0,0" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox DockPanel.Dock="Top" Margin="18,8,0,0" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<CheckBox DockPanel.Dock="Top" Margin="18,8,0,0" Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox DockPanel.Dock="Top" Margin="18,8,0,0" Content="CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<RadioButton DockPanel.Dock="Top" Margin="18,8,0,0" Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<RadioButton DockPanel.Dock="Top" Margin="18,8,0,0" Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<StackPanel>
<TextBox HorizontalAlignment="Left" Margin="153,34,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Margin="153,10,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="153,10,0,0" VerticalAlignment="Top"/>
</StackPanel>
</DockPanel>
</Window>
Below is my code behind
private void DockPanel_PreviewKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up && e.OriginalSource is TextBox)
{
FocusNavigationDirection focusDirection = FocusNavigationDirection.Previous;
TraversalRequest request = new TraversalRequest(focusDirection);
UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
// Change keyboard focus.
if (elementWithFocus != null)
{
if (elementWithFocus.MoveFocus(request)) e.Handled = true;
}
e.Handled = true;
}
}
Related
Unable to call a grid inside a listbox anymore... my xaml is as follows.
<UserControl x:Class="WPFPurpleButtonTest.InstrumentUserControl"
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"
xmlns:local="clr-namespace:WPFPurpleButtonTest"
mc:Ignorable="d"
d:DesignHeight="750" d:DesignWidth="900">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TabControl x:Name="tabControl" HorizontalAlignment="Left" Height="706" Margin="24,34,0,-212" VerticalAlignment="Top" Width="850" Grid.RowSpan="2">
<TabItem Header="TabItem" Name="mainTab">
<Grid Background="#FFE5E5E5" Margin="0,0,-396,-255">
<Label x:Name="colourName" Content="PURPLE" HorizontalAlignment="Left" Height="93" Margin="284,88,0,0" VerticalAlignment="Top" Width="243" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="50" Foreground="#FFDC00FF"/>
<Button x:Name="testButton" Content="Button" HorizontalAlignment="Left" Margin="365,181,0,0" VerticalAlignment="Top" Width="75" Click="TestButton_Click"/>
<Label x:Name="label" Content="Row Size" HorizontalAlignment="Left" Margin="198,211,0,0" VerticalAlignment="Top" Foreground="#FFDC00FF"/>
<Label x:Name="label_Copy" Content="Column Size" HorizontalAlignment="Left" Margin="432,211,0,0" VerticalAlignment="Top" Foreground="#FFDC00FF"/>
<Button x:Name="createGrid" Content="Create Grid" HorizontalAlignment="Left" Margin="365,273,0,0" VerticalAlignment="Top" Width="75" Click="CreateGrid_Click"/>
<TextBox x:Name="rowSizeText" HorizontalAlignment="Left" Height="23" Margin="278,214,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="62"/>
<TextBox x:Name="columnSizeText" HorizontalAlignment="Left" Height="23" Margin="522,215,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="62"/>
</Grid>
</TabItem>
<TabItem Header="TabItem" Name="gridTab">
<ListBox x:Name="listbox1" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="20" HorizontalAlignment="Center">
<Viewbox>
<Grid x:Name="wellGrid" Grid.Row="1" ShowGridLines="True"
local:GridHelpers.RowCount="{Binding RowCount}"
local:GridHelpers.ColumnCount="{Binding ColumnCount}" Margin="15,15,15,15" />
</Viewbox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</TabItem>
</TabControl>
</Grid>
</UserControl>
I want to be able to call wellGrid.Children like I used to when the grid was not in a listbox but not entirely sure how to do that now the grid is in the listbox.
The Grid is part of theDataTemplatethat is assigned to theListBoxItem. You need to get theContentPresenterof the item. This is where theDataTemplate` is applied.
private void Button_Click(object sender, RoutedEventArgs e)
{
int selectedItemIndex = this.listbox1.SelectedIndex;
if (selectedItemIndex == -1)
{
return;
}
var itemContainer = this.listbox1.ItemContainerGenerator.ContainerFromIndex(selectedItemIndex) as ListBoxItem;
if (TryFindChildElement(itemContainer, out ContentPresenter contentPresenter))
{
// Get the Grid
var grid = contentPresenter.ContentTemplate.FindName("wellGrid", myContentPresenter) as Grid;
}
}
// Helper to traverse the visual tree
private bool TryFindChildElement<TElement>(DependencyObject parent, out TElement resultElement) where TElement : DependencyObject
{
resultElement = null;
for (var childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(parent); childIndex++)
{
DependencyObject childElement = VisualTreeHelper.GetChild(parent, childIndex);
if (childElement is Popup popup)
{
childElement = popup.Child;
}
if (childElement is TElement)
{
resultElement = childElement as TElement;
return true;
}
if (TryFindChildElement(childElement, out resultElement))
{
return true;
}
}
return false;
}
Hey all I have the following code that supposed to take my jpg image and place it on the background of the form. However, all I see is a black background.
Dim myBrush As New ImageBrush()
Dim image As New Image()
Dim grid As New Grid()
image.Source = New BitmapImage(New Uri("pack://application:,,,/WpfApplication1;component/Resources/1680-logoless.jpg"))
myBrush.ImageSource = image.Source
grid.Background = myBrush
And this is my current XAML:
<Window x:Class="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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Margin="157,145,0,0"/>
<Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Margin="68,59,0,0" VerticalAlignment="Top" Width="111"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="335,62,0,0"/>
<Button x:Name="button1" Content="Encrypt" HorizontalAlignment="Left" Height="17" Margin="335,123,0,0" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="toEncrypt" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="172" Margin="335,145,0,0"/>
<TextBox x:Name="toDecrypt" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="172" Margin="335,173,0,0"/>
<Button x:Name="button2" Content="Decrypt" HorizontalAlignment="Left" Height="23" Margin="335,201,0,0" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
Anyone see anything I could be missing?
Set the x:Name attribute of the Grid in XAML, which will create a field in your MainWindow class:
<Window ...>
<Grid x:Name="rootGrid">
...
</Grid>
</Window>
Then, instead of creating a new Grid instance, set the Background of the one declared in XAML:
rootGrid.Background = New ImageBrush(New BitmapImage(
New Uri("pack://application:,,,/Resources/1680-logoless.jpg")))
I've set up binding of a double value to an AngularBlockBar control. There are two gauges in the layout, but the AngularBlockBar which is over layed on top of the AngularGauge doesn't show its binding value updating on screen, although valid binding values are being updated to it.
To debug this, I've checked first that the binding value that is bound to the AngularBlockBar is valid and this shows as true by setting a break point.
The binding value shows as valid.
I'm thinking its because of the way the two gauges are over layed over each other, which causes the AngularBlockBar's binding to not show.
Does anyone know what the issue could be here, that prevents the AngularblockBar's binding from being shown on screen?
The layout below is the main user control of the application where the two gauges are over layed. The first gauge updates correctly with a white fill value as shown in the screen shot below. But the AngularBlockBar which has a blue fill value isn't updated
<UserControl
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"
xmlns:gauges="http://gu.se/Gauges"
xmlns:local="clr-namespace:MyoTestv4"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="MyoTestv4.AdductionAbductionFlexionView"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Viewbox>
<Grid HorizontalAlignment="Left" Height="280" Margin="10,10,0,0" VerticalAlignment="Top" Width="280">
<TextBox HorizontalAlignment="Left" x:Name="statusTbx" Background="#141c28" Foreground="White" BorderBrush="#141c28" Height="30" Margin="0,3,0,0" TextWrapping="Wrap" Text="{Binding CurrentStatus}" VerticalAlignment="Top" Width="75"/>
<TextBox HorizontalAlignment="Left" x:Name="poseStatusTbx" Background="#141c28" Foreground="White" BorderBrush="#141c28" Height="39" Margin="80,4,0,0" TextWrapping="Wrap" Text="{Binding PoseStatus}" VerticalAlignment="Top" Width="75"/>
<TextBox HorizontalAlignment="Left" x:Name="degreeOfAbductionTbx" Background="#141c28" Foreground="White" Height="22" Margin="0,246,0,0" TextWrapping="Wrap" Text="{Binding DegreeStatus}" VerticalAlignment="Top" Width="47"/>
<Label Content="Arc start:" Foreground="#00bdde" HorizontalAlignment="Left" Margin="0,186,0,0" VerticalAlignment="Top" Width="57"/>
<Image HorizontalAlignment="Left" Source="pack://application:,,,/Images/abduction_side_trans.png" x:Name="exerciseImage" Height="100" Margin="0,33,0,0" VerticalAlignment="Top" Width="100"/>
<TextBox HorizontalAlignment="Left" x:Name="painfulArcEndTbx" Background="#141c28" Foreground="White" Height="22" Margin="62,217,0,0" TextWrapping="Wrap" Text="{Binding EndDegreeStatus}" VerticalAlignment="Top" Width="48"/>
<Label Content="Arc end:" Foreground="#00bdde" HorizontalAlignment="Left" Margin="0,217,0,0" VerticalAlignment="Top" Width="57"/>
<TextBox HorizontalAlignment="Left" x:Name="painfulArcStartTbx" Background="#141c28" Foreground="White" Height="23" Margin="62,189,0,0" TextWrapping="Wrap" Text="{Binding StartDegreeStatus}" VerticalAlignment="Top" Width="48"/>
<Grid>
<gauges:AngularGauge
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
IsDirectionReversed="False"
MajorTickFrequency="8"
FontSize="10"
MajorTicks="10"
MaxAngle="0"
Maximum="180"
MinAngle="-180"
Minimum="0"
Style="{StaticResource FatAngular}"
MinorTickFrequency="4"
Value="{Binding DegreeStatus}" Margin="149,2,-89,2" RenderTransformOrigin="0.5,0.5" Height="Auto" Width="Auto"
>
<gauges:AngularGauge.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="-89.642"/>
<TranslateTransform/>
</TransformGroup>
</gauges:AngularGauge.RenderTransform>
</gauges:AngularGauge>
<gauges:AngularBlockBar
MaxAngle="180"
Maximum="1"
MinAngle="0"
Minimum="0"
Opacity="0.2"
ReservedSpace="50"
Margin="149,2,-96,0"
TickLength="55"
Fill="DodgerBlue"
Value="{Binding PainfulArcStatus}"
RenderTransformOrigin="0.5,0.5" >
<gauges:AngularBlockBar.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="90.711"/>
<TranslateTransform/>
</TransformGroup>
</gauges:AngularBlockBar.RenderTransform>
</gauges:AngularBlockBar>
</Grid>
<TextBlock HorizontalAlignment="Left" x:Name="repCntTblk" Margin="80,43,0,0" TextWrapping="Wrap" Text="" FontSize="38" Foreground="#00bdde" VerticalAlignment="Top" Width="34" Height="41"/>
<Button Content="Submit" x:Name="submitBtn" Command="{Binding Path=DataSubmitCommand}" IsDefault="True" IsCancel="True" HorizontalAlignment="Left" Background="#00bdde" Foreground="White" Margin="0,148,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.76,5.093"/>
</Grid>
</Viewbox>
</UserControl>
The first gauge binding being shown, but the over layed value for the block bar isn't shown.
Don't you need to define Grid.Column and/or Grid.Row on your guage and blockbar other wise they will just lay ontop of eachother?
example with Grid.ColumnDefinitions, you probably want RowDefinitions
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100px"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ComboBox ItemsSource="{Binding NavigationItems}" SelectedItem="{Binding SelectedNavigationItem, Mode=TwoWay}" Grid.Row="0" Grid.Column="0">
</ComboBox>
<ContentControl Content="{Binding SelectedNavigationItem}" Grid.Row="0" Grid.Column="1"/>
</Grid>
When the Visibility of ContentPresenter is set to Collapsed and change its Visibility at runtime, breaking the binding of the element kept inside its content.
XAML
<Grid>
<Button x:Name="button"
Width="100"
Height="20"
Margin="173,23,230,268"
Click="button_Click"
Content="Button" />
<ContentPresenter x:Name="contentPresenter"
Margin="0,62,12,0"
Visibility="Collapsed">
<ContentPresenter.Content>
<StackPanel>
<TextBox x:Name="test2"
Width="200"
Height="20" />
<TextBox Width="200"
Height="20"
Text="{Binding ElementName=test2,
Path=Text,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</ContentPresenter.Content>
</ContentPresenter>
</Grid>
Code
private void button_Click(object sender, RoutedEventArgs e)
{
if (contentPresenter.IsVisible == false)
{
contentPresenter.Visibility = System.Windows.Visibility.Visible;
}
else if (contentPresenter.IsVisible == true)
{
contentPresenter.Visibility = System.Windows.Visibility.Collapsed;
}
}
Any one help.
if you put your content into a usercontrol your binding will work. if you check your output at runtime you would see that there is a binding error. meaning there is no element with "test2"
<Grid>
<Button x:Name="button"
Width="100"
Height="20"
Margin="173,23,230,268"
Click="button_Click"
Content="Button" />
<ContentPresenter x:Name="contentPresenter"
Margin="0,62,12,0"
Visibility="Collapsed">
<ContentPresenter.Content>
<WpfApplication1:UserControl1/>
</ContentPresenter.Content>
</ContentPresenter>
</Grid>
usercontrol
<UserControl x:Class="WpfApplication1.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>
<StackPanel>
<TextBox x:Name="test2"
Width="200"
Height="20" />
<TextBox Width="200"
Height="20"
Text="{Binding ElementName=test2,
Path=Text,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Grid>
</UserControl>
I have a WPF UserControl with a textbox. Here's how the textbox and it's parent control are defined:
<DockPanel Margin="10,20,10,10" FocusManager.FocusedElement="{Binding ElementName=uxJobNumber}" >
<TextBox x:Name="uxJobNumber" Text="{Binding JobNumber, Mode=TwoWay, ValidatesOnDataErrors=True}" TextWrapping="Wrap" FontSize="48" BorderBrush="Black" BorderThickness="1" Margin="10"/>
</DockPanel>
With the FocusManager.FocusedElement set, I can see a cursor bar present within the textbox. However, the cursor bar is not blinking, and does not allow the user to immediately start typing.
Without the FocusManager.FocusedElement set, when the application starts there is no cursor bar within the text box at all.
Here's the complete XAML
<UserControl x:Class=""
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"
xmlns:converters="clr-namespace:.Modules.Converters"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
mc:Ignorable="d">
<UserControl.Resources>
<converters:VisibilityConverter x:Key="Visibility" />
</UserControl.Resources>
<Canvas Width="1024" Height="768" >
<Border Style="{DynamicResource GroupBox}" Canvas.Left="36.261" Canvas.Top="32.131" Width="426.936">
<StackPanel>
<Border>
<TextBlock Text="STEP 1"/>
</Border>
<TextBlock Text="Enter the five (5) digit Job Number and click Verify." />
<Path/>
<DockPanel Margin="10,20,10,10" FocusManager.FocusedElement="{Binding ElementName=uxJobNumber}" >
<Button Content="Verify" Width="125" Height="65" HorizontalAlignment="Right" Command="{Binding SearchJobCommand}" Style="{DynamicResource RedButton}" Margin="0" DockPanel.Dock="Right" IsDefault="True"/>
<TextBox Text="{Binding JobNumber, Mode=TwoWay, ValidatesOnDataErrors=True}" TextWrapping="Wrap" FontSize="48" BorderBrush="Black" BorderThickness="1" x:Name="uxJobNumber" Margin="10" KeyboardNavigation.TabIndex="0" />
</DockPanel>
</StackPanel>
</Border>
<TextBlock Text="{Binding Error}" Visibility="{Binding HasError, Converter={StaticResource Visibility}}" Canvas.Left="48" Canvas.Top="288" FontSize="16" Width="403" Foreground="Red" />
</Canvas>
We finally resorted to using the Focus() method in the code behind when the form is done loading.
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
uxJobNumber.Focus();
}
Using your code I get this to work; blinking and all.