I have Popup window where dynamically attaching the UserControl. Here I need to set the focus for first control. If I use:
FocusManager.FocusedElement="{Binding ElementName=txtcode}"
on UserControl the cursor not coming. Could anyone help me to set focus on first element in the UserControl.
Thanks in advance.
I have just needed to do this, but not in a user control, so might not help. what I needed to do is to add the FocusManager bit in the Window tag:
<Window x:Class="xxxxx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" FocusManager.FocusedElement="{Binding ElementName=txtcode}">
And then make sure i correctly named the control:
<TextBox Name="txtcode" Text="" />
Although I'm new to wpf, and haven't worked with user controls yet.
Related
I have an interesting situation where I set the focus on a searchbox using: FocusManager.FocusedElement="{Binding ElementName=SearchBox}" which I found here: Set the focus on a textbox in xaml wpf
The good thing is that the search box is actually focused (I have a trigger that performs an action on focus), but I can't type into the textbox until I click on the textbox.
My textbox is very basic:
<TextBox x:Name="SearchBox"
Grid.Row="0"
style="{StaticResource SearchBox}" />
Any idea how I can allow the user to type right away as they open the control?
See the following link:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.focusmanager.focusedelement?redirectedfrom=MSDN&view=netframework-4.7.2#System_Windows_Input_FocusManager_FocusedElement
Especially the remarks:
The FocusedElement is the element which has logical focus for a specific focus scope. This object may or may not have keyboard focus. Keyboard focus refers to the element that receives keyboard input. For more information on focus, keyboard focus, and logical focus, see the Input Overview.
and:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.focusmanager?redirectedfrom=MSDN&view=netframework-4.7.2
An element with logical focus does not necessarily have keyboard
focus, but an element with keyboard focus will have logical focus. It
is possible to define a focus scope within a focus scope. In this
case, both the parent focus scope and the child focus scope can have a
FocusManager.FocusedElement
In the following post you'll find an example how to handle this:
https://stackoverflow.com/a/20299923/9295125
you can also use this with <window> tag of the xaml page
<Window x:Class="WpfApplication30.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
FocusManager.FocusedElement="{Binding ElementName=SearchBox}">
<StackPanel>
<TextBox x:Name="textBox1"/>
<TextBox x:Name="SearchBox"
style="{StaticResource SearchBox}" />
</StackPanel>
</Window>
Newbie question. I am using a simple dialog to display some of my usercontrols:
<Window x:Class="Nova5.UI.Views.WindowDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowDialog"
WindowStyle="SingleBorderWindow"
WindowStartupLocation="CenterScreen" WindowState="{Binding WindowState, Mode=TwoWay}" >
<ContentPresenter x:Name="DialogPresenter" Content="{Binding .}"/>
</Window>
For some of my usercontrols that are hosted by the WindowDialog, I would like to have the initial dialog use SizeToContent instead of WindowState. The effect I'm trying to get is to have some user controls maximize to the entire screen, while others are to be smaller in the center of the screen.
Can this be done with the single WindowDialog or should I simply use two different WindowDialog's? (e.g., WindowDialogMaximized, and WindowDialogToContent)
Is there a better way?
Thank you for your consideration of my question.
I would create another binding for SizeToContent and bind the two properties like this:
For controls that should be sized to content, set SizeToContent to Width/Height/WidthAndHeight and WindowState to Normal.
For controls that should be maximized, set SizeToContent to Manual and WindowState to Maximized.
I have a tab control that is dynamically populated with tabs that contain user controls of various sizes. When a tab is opened I want the window to automatically resize to a size that makes sense given the active user control. Is there a clean way to do this?
I'm using a standard mvvm pattern.
Use the SizeToContent property on the Window class.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
SizeToContent='Width'>
<Grid>
<Button Width='200' Content='The Button' />
</Grid>
</Window>
I made a usercontrol in WPF with an image in it. I declared a MouseDown event for this image:
<Image x:Name="imgState" Height="300" Width="300" MouseDown="imgState_MouseDown" OpacityMask="#00000000" />
I placed this usercontrol on my application form, but the event isn't fireing. I'm pretty new to WPF and I read about RoutedEvents but I don't really understand it. I would be happy if someone could help and explain this to me!
Update
Changing to PreviewMouseDown didn't fire the event too. I tried setting the background to transparent and even tried with a blank 300x300 image. The grid workaround doesn't fire the event too. Here is how my code behind looks like:
private void imgState_MouseDown(object sender, MouseButtonEventArgs e)
{
//Some code here
}
Update 2
Here is my whole XAML file:
<UserControl x:Class="TicTacToe.controls.SingleField"
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>
<Image x:Name="imgState" MouseDown="imgState_MouseDown" Height="300" Width="300" Stretch="None" OpacityMask="#00000000"/>
</Grid>
</UserControl>
I removed the source again because I set one from code behind at runtime and adding a transparent/clear image didn't helped.
You probably want PreviewMouseUp instead of MouseDown event
<Image x:Name="imgState" Height="300" Width="300"
PreviewMouseUp="ImgState_OnPreviewMouseUp"
PreviewMouseDown="ImgState_OnPreviewMouseDown"/>
Either of the two, you can capture the event from there.
If the answer above does not help:
Not a very nice solution but does work so many times:
Wrap your image with a grid on which you will have your event...
<Grid MouseDown="imgState_MouseDown">
<Image/>
</Grid>
Okay I solved the problem myself.
The problem was the setting OpacityMask="#00000000" that prevented the image from appearing so there were, as #lll said, nothing to hit. I don't know when the setting was set, but I think it happened automatically while expanding the Representation tab.
Thanks for helping me!
I have a WPF WebBrowser control in a Window.
I want to remove the default window controls so I set AllowsTransparency="True" and WindowStyle="None".
<Window x:Class="InstallerToolkit.InteractiveDemosWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="870" Width="1110" WindowStyle="None" AllowsTransparency="True" WindowStartupLocation="CenterScreen">
<Grid>
<WebBrowser Name="WebBrowser" HorizontalAlignment="Left" Height="795" Margin="10,35,0,0" VerticalAlignment="Top" Width="1082" />
</Grid>
This results in me not being able to see my webbrowser contents. If I remove the AllowsTransparency="True" then I can see by webpage but I now have the default controls which I dont want.
How do I get around this?
If the functionality you are looking for is truly AllowTransparency, you can update to WPF 4.5 where they have added a fix to Airspace issues, read this for more details.
If you are unable to upgrade your version of WPF then you will need to disable resize as others have mentioned.
Adding ResizeMode = NoResize should get you rid of the controls. I should mention that AllowTransparency = True can reduce performance! Depending on the kind of software, try avoinding it!