Is there a reason why I can't set the focus on my PasswordBox Control?
C#:
public Login()
{
InitializeComponent();
_password.Focus();
}
XAML:
<PasswordBox x:Name="_password" Width="200" Height="30" FontSize="14"
KeyDown="_password_KeyDown"/>
You're doing it too early when you're setting it in the constructor. Try the Load event handler instead.
public Login()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Login_Loaded);
}
void Login_Loaded(object sender, RoutedEventArgs e)
{
_password.Focus();
}
You can also do it in XAML:
<Window ....
FocusManager.FocusedElement="{Binding ElementName=_password}"
... />
WPF offers a nice elegant way (xaml only)
FocusManager.FocusedElement="{Binding ElementName=pass}">
Example:
<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"
FocusManager.FocusedElement="{Binding ElementName=pass}">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="261,165,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="name" HorizontalAlignment="Left" Height="23" Margin="70,193,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="pass" HorizontalAlignment="Left" Height="23" Margin="70,165,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
If you want to do it in XAML:
<StackPanel FocusManager.FocusedElement="{Binding ElementName=_password}">
<PasswordBox Name="_password" />
</StackPanel>
Please note (MSDN):
There can be only one element on the whole desktop that has keyboard
focus. In WPF, the element that has keyboard focus will have
IsKeyboardFocused set to true.
Related
how to transfer what I selected to ListBoxEdit to ComboBoxEdit WPF C #
How can i do this?
code below
<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:local="clr-namespace:ReportPrint"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="ReportPrint.ComboList"
mc:Ignorable="d" Width="200" Height="23" Name="k">
<Grid Margin="0">
<dxe:ComboBoxEdit x:Name="combo" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Height="23" PopupOpening="ComboBoxEdit_PopupOpening" AutoComplete="False" GotFocus="combo_GotFocus" ItemsSource="{Binding ElementName=checkedListBoxEditStyleSettings}" SelectedItem="{Binding ElementName=checkedListBoxEditStyleSettings, Mode=OneWay}" EditValue="{Binding ElementName=checkedListBoxEditStyleSettings, Mode=OneWay}" >
<dxe:ComboBoxEdit.StyleSettings>
<dxe:TokenComboBoxStyleSettings x:Name="tokenComboBoxStyleSettings"/>
</dxe:ComboBoxEdit.StyleSettings>
</dxe:ComboBoxEdit>
<dxe:ListBoxEdit x:Name="List" HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Visibility="Hidden" PreviewMouseWheel="List_PreviewMouseWheel" Margin="0,23,-40,-102" >
<dxe:ListBoxEdit.StyleSettings>
<dxe:CheckedListBoxEditStyleSettings x:Name="checkedListBoxEditStyleSettings"/>
</dxe:ListBoxEdit.StyleSettings>
</dxe:ListBoxEdit>
</Grid>
</UserControl>
did it myself, but I will share it with you
combo.Items.Add(List.SelectedItem);//combo this ComboBoxEdit
combo.DisplayMember = "Value";
combo.SelectedItems.Add(List.SelectedItem);//List this ListBoxEdit
I am new to WPF, and I am trying to make an RPG using 2D Sprite gif files for animations for walking forward, backward, etc.
I am having issues making the original picture move in any direction using the arrow keys. Here is the snippet of code:
<UserControl x:Class="TextofTheWild2._0.Screen1"
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:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
xmlns:local="clr-namespace:TextofTheWild2._0"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
FocusManager.FocusedElement = "{Binding ElementName=MyCanvas}">
<Canvas x:Name="MyCanvas" HorizontalAlignment="Right" Height="450" VerticalAlignment="Top" Width="800" Background="#FFFCD8A8" KeyDown="Canvas_OnKeyDown" Focusable="True" FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}">
<Grid HorizontalAlignment="Left" Height="190" VerticalAlignment="Top" Width="410" Background="#FF00A800" Canvas.Left="390"/>
<TextBlock Height="40" Canvas.Left="149" TextWrapping="Wrap" Text="Press E to activate" Canvas.Top="40" Width="52" FontFamily="Microsoft Sans Serif" Visibility="Collapsed"/>
<Grid HorizontalAlignment="Left" Height="190" VerticalAlignment="Top" Width="27" Background="#FF00A800"/>
<Grid HorizontalAlignment="Left" Height="26" VerticalAlignment="Top" Width="85" Background="Black" Canvas.Left="27" Canvas.Top="35"/>
<Grid HorizontalAlignment="Left" Height="35" VerticalAlignment="Top" Width="112" Background="#FF00A800"/>
<Grid HorizontalAlignment="Left" Height="181" VerticalAlignment="Top" Width="27" Background="#FF00A800" Canvas.Top="269"/>
<Grid HorizontalAlignment="Left" Height="181" VerticalAlignment="Top" Width="22" Background="#FF00A800" Canvas.Left="778" Canvas.Top="269"/>
<Grid HorizontalAlignment="Left" Height="80" VerticalAlignment="Top" Width="800" Background="#FF00A800" Canvas.Top="370"/>
<Image Name="Green_Link" Height="93" VerticalAlignment="Top" Canvas.Left="480" Canvas.Top="259" Width="85" gif:AnimationBehavior.SourceUri="Assets/Green Link idle.gif" Source="Assets/Green Link idle.gif" Focusable="True"/>
<Image Name="Blue_Link" Height="93" VerticalAlignment="Top" Canvas.Left="390" Canvas.Top="259" Width="85" gif:AnimationBehavior.SourceUri="Assets/Blue Link idle.gif" Source="Assets/Blue Link idle.gif"/>
<Image Name="Red_Link" Height="93" VerticalAlignment="Top" Canvas.Left="305" Canvas.Top="259" Width="85" gif:AnimationBehavior.SourceUri="Assets/Red Link Idle.gif" Source="Assets/Red Link Idle.gif"/>
<Image Name="Purple_Link" Height="93" VerticalAlignment="Top" Canvas.Left="220" Canvas.Top="259" Width="85" gif:AnimationBehavior.SourceUri="Assets/Purple Link idle.gif" Source="Assets/Purple Link idle.gif"/>
</Canvas>
</UserControl>
For XAML, and the code behind it:
public partial class Screen1 : UserControl
{
public Screen1()
{
InitializeComponent();
}
private void Canvas_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
Canvas.SetTop(Green_Link, Canvas.GetTop(Green_Link) + 10);
}
else if (e.Key == Key.Up)
{
Canvas.SetTop(Green_Link, Canvas.GetTop(Green_Link) - 10);
}
else if (e.Key == Key.Left)
{
Canvas.SetLeft(Green_Link, Canvas.GetLeft(Green_Link) - 10);
}
else if (e.Key == Key.Right)
{
Canvas.SetLeft(Green_Link, Canvas.GetLeft(Green_Link) + 10);
}
}
}
Before I start working on transitional animations, I would like to at least get the images to move in the canvas. Thanks!
Looks like you've put your C# code in the wrong place. The start of your XAML file says this:
<x:Name="WINDow" x:Class="TextofTheWild2._0.GameWindow" ...
This means that your xaml file is partially defining a class called GameWindow. This'll be made up of two files, the xaml and the codebehind (which has extension .xaml.cs). When you handle an event in xaml like you're doing (ie. KeyDown="Canvas_OnKeyDown"), it gets routed to a function in the codebehind for that view.
If you put your Canvas_KeyDown method in GameWindow.xaml.cs (and rename the method to Canvas_OnKeyDown - you were missing an On), WPF should pick up and it should work correctly.
Consider the below WPF -
The issue is the text that appears in the label is being cut off if it is longer than the width of the label.
XAML - label name is 'factTxt'
<Window x:Name="mainScreen" x:Class="FactsOnAll.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:FactsOnAll"
mc:Ignorable="d"
Title="Random Facts" Height="450" Width="800" Icon="Properties/checklist.png">
<Grid>
<Button x:Name="getFactBtn" Content="Get Random Fact" HorizontalAlignment="Left" Margin="304,331,0,0" VerticalAlignment="Top" Width="177" Click="getFactBtn_Click" FontSize="20" Background="#FF8ECF87" Foreground="#FF444444"/>
<Label x:Name="factTxt" Content="" Margin="10,98,10,0" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="20"/>
<Label x:Name="titleTxt" Content="Random Facts" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="772" Height="46" FontSize="24"/>
</Grid>
</Window>
Expect result
Allow the text to go to a new line. I thought increasing the height of the label would fix this but no luck.
The Label has no wrapping but a TextBlock.
You can just create custom content like, that will wrap automatically:
<Label Width="100">
<TextBlock Text="Get Random Fact" TextWrapping="Wrap"/>
</Label>
How to:
Disable resizing for this usercontrol. In other words, when the user grabs the corners or the sides of this usercontrol with a mouse, I dont want the user to be able to change the size of the usercontrol?
Or if there is no way to stop resizing then how do I only allow the right side of the usercontrol dragged?
<UserControl x:Class="MyEditor.MyDialog"
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="152" d:DesignWidth="590" HorizontalContentAlignment="Right" MinWidth="{Binding ElementName=VariableType}" MinHeight="{Binding RelativeSource={RelativeSource Self}}">
<Grid Width="591" Height="147" MinWidth="{Binding ElementName=VariableTypeTextBox}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="137*" />
<ColumnDefinition Width="454*" MinWidth="250" />
</Grid.ColumnDefinitions>
<Button Content="Cancel" Height="23" Margin="0,94,7,0" Name="CancelButton" VerticalAlignment="Top" Click="CancelButton_Click" Grid.Column="1" HorizontalAlignment="Right" Width="75" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" />
<Button Content="Create" Height="23" Margin="0,94,108,0" Name="CreateButton" VerticalAlignment="Top" Click="CreateButton_Click" Grid.Column="1" HorizontalAlignment="Right" Width="75" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" />
<Label Content="Variable Name " Height="28" Margin="0,12,29,0" Name="VariableName" VerticalAlignment="Top" HorizontalAlignment="Right" Width="96" Target="{Binding}" HorizontalContentAlignment="Right" />
<TextBox Height="29" Margin="0,11,7,0" Name="VarNameTextBox" VerticalAlignment="Top" KeyDown="OnKeyDownHandler" MouseLeave="MouseLeaveHandler" LostFocus="LostFocusHandler" Grid.Column="1" HorizontalAlignment="Stretch" />
<Label Content="Variable Type" Height="28" Margin="0,0,29,73" Name="VariableType" VerticalAlignment="Bottom" HorizontalContentAlignment="Right" HorizontalAlignment="Right" Width="96" />
<TextBox Height="23" Margin="0,51,7,0" Name="VariableTypeTextBox" VerticalAlignment="Top" IsReadOnly="True" Background="Silver" Foreground="Black" Grid.Column="1" HorizontalAlignment="Stretch" Width="AUTO" />
</Grid>
You've pasted the XAML for a UserControl, but your question is asking about a Window. So, you will need to place your UserControl inside a Window that is set up to not allow resizing.
A WPF Window has a ResizeMode property, which can be one of the following:
NoResize
CanMinimize
CanResize (default)
CanResizeWithGrip
You will want NoResize.
Example:
<Window x:Class="MyEditor.Views.EditorWindow"
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:views="clr-namespace:MyEditor"
mc:Ignorable="d"
ResizeMode="NoResize"
Title="Editor Window">
<views:MyDialog />
</Window>
Please see the documentation for more details.
Simply set the MinWidth/MaxWidth and MinHeight/MaxHeight properties to your required value.
For Disabling : ResizeMode="CanMinimize"
<Window x:Class="XXXXXX.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:XXXXXXX"
mc:Ignorable="d"
WindowState="Maximized"
ResizeMode="CanMinimize"
Title="Window">
Here is a simple solution:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
((Window)Parent).ResizeMode = ResizeMode.NoResize;
}
Or an attached property on the UserControl if you use the Prism Library:
<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="prism:Dialog.WindowStartupLocation"
Value="CenterScreen" />
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="ShowInTaskbar" Value="False"/>
Setter Property="SizeToContent" Value="WidthAndHeight"/>
</Style>
</prism:Dialog.WindowStyle>
Correct way to add and remove user controls on a panel.
I have some doubts about this:
It is right (better way) to do it this way?
Leave some waste in memory to run the application after a while?
Currently I have a main window as follows:
<!-- MainWindow.xaml -->
<Window x:Class="Empresas.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Empresas" Height="480" Width="640"
MinHeight="480" MinWidth="640">
<DockPanel HorizontalAlignment="Stretch" Width="Auto" Margin="0">
<!-- Menu -->
<Menu x:Name="MainWindowClientesMenu" Width="Auto" Height="25"
DockPanel.Dock="Top">
<MenuItem Header="_Archivo">
<MenuItem Header="Agregar _Nueva empresa" x:Name="MainWindowClientesAgregarEmpresa" Click="MainWindowClientesAgregarEmpresa_Click" />
<Separator/>
<MenuItem Header="Salir"/>
</MenuItem>
</Menu>
<!-- Fin Menu -->
<!-- Barra de Estado -->
<StatusBar x:Name="MainWindowClientesStatusBar" Width="Auto" Height="25"
DockPanel.Dock="Bottom" Background="#ddd" HorizontalAlignment="Stretch">
<StatusBarItem x:Name="MainWindowClientesCurrentAction" HorizontalContentAlignment="Right"
Margin="0 0 5 0"></StatusBarItem>
</StatusBar>
<StackPanel x:Name="MainWindowClientesContenido"></StackPanel>
<!-- Fin Lista isquierda de Empresas/Clientes -->
</DockPanel>
</Window>
My user control is as follows
<!-- NuevaEmpresa.xaml -->
<UserControl x:Class="Empresas.View.NuevaEmpresa"
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" Height="300" Width="350">
<Grid>
<Label Content="Razon Social" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="118,14,0,0" Name="NuevaEmpresaRazonSocialTxtBox" VerticalAlignment="Top" Width="220" />
<Label Content="Nit" Height="28" HorizontalAlignment="Left" Margin="12,52,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="118,54,0,0" Name="NuevaEmpresaNitTxtBox" VerticalAlignment="Top" Width="220" />
<Button Content="Crear Empresa" Height="23" HorizontalAlignment="Left" Margin="223,114,0,0" Name="NuevaEmpresaCrearButton" VerticalAlignment="Top" Width="115" Click="NuevaEmpresaCrearButton_Click" />
<Button Content="Cancelar" Height="23" HorizontalAlignment="Left" Margin="93,114,0,0" Name="NuevaEmpresaCancelarButton" VerticalAlignment="Top" Width="115" Click="NuevaEmpresaCancelarButton_Click" />
</Grid>
</UserControl>
and how they add and remove the control is as follows:
// MainWindow.xaml.cs
private void MainWindowClientesAgregarEmpresa_Click(object sender, RoutedEventArgs e)
{
MainWindowClientesContenido.Children.Add(new View.NuevaEmpresa(MainWindowClientesContenido));
}
// NuevaEmpresa.xaml.cs
public partial class NuevaEmpresa : UserControl
{
// ...
private Panel _parent;
public NuevaEmpresa(Panel parent)
{
InitializeComponent();
_parent = parent;
// ...
}
// ...
private void NuevaEmpresaCancelarButton_Click(object sender, RoutedEventArgs e)
{
_parent.Children.Clear();
}
}
I don't see why this could be wrong. You're setting its child and then clearing it. It seems pretty sane to me. Also don't see why memory could be leaked. Once the parents clear its children then it must be disposed.