focussing cursor in textbox using xaml in wpf - wpf

private void TextBox1_TextChanged(object sender, TextChangedEventArgs e)
{
txt1_focus.Focus();
}
How can i achieve the above code using using xaml file in wpf.

use the FocusManager.FocusedElementlike below:
<Window x:Class="UI.Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FocusManager.FocusedElement="{Binding ElementName=txtSearch}">
</Window>

http://cloudstore.blogspot.com/2008/06/setting-initial-focus-in-wpf.html
This site explains how to set the initial focus on a certain control.
<Window ...
FocusManager.FocusedElement="{Binding ElementName=TextBox1}">

Related

Control Focus in dialog box with Prism 7.x and 8.x?

I need to set a TextBox being focused when the dialog box was opened. It was working with Prism 6.x; but is no longer working since 7.x and 8.x. The framework difference is that Prism 6.x uses InteractionRequest for dialog box; while Prism 7/8 uses the dialog service. Does Prism 7 and 8 also introduce some new approaches related to dialog box? Following is the code snippet of XAML setting FocusManager:
<UserControl x:Class="FeatureModule.Views.MyDialogView"
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:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
FocusManager.FocusedElement="{Binding ElementName=edit}"
mc:Ignorable="d" d:DesignHeight="140" d:DesignWidth="400">
It does not function any more. Does it related to the dialog service?
The dialog service starting from Prism 7.2.x is completely different from the legacy interaction requests in prior versions, so it is hard to tell what exactly was the breaking change. Setting the focus manager property on the root element of the UserControl instead of on the UserControl could solve this the issue, but it might not work under other circumstances, read below why.
<UserControl x:Class="FeatureModule.Views.MyDialogView" ...>
<Grid FocusManager.FocusedElement="{Binding ElementName=edit}">
<!-- ...your markup -->
</Grid>
</UserControl>
I think the issue is more general. WPF has two different focus concepts, logical and keyboard focus. What you want to set on the TextBox is keyboard focus, but the FocusManager only sets logical focus.
Logical focus pertains to the FocusManager.FocusedElement within a specific focus scope.
The important part is the relationship between logical and keyboard focus.
An element with logical focus does not necessarily have keyboard focus, but an element with keyboard focus will have logical focus.
This means, even if you set the logical focus on a different element it might not work.
Keyboard focus pertains to the element that is currently receiving keyboard input. There can be only one element with keyboard focus.
Unfortunately, the Keyboard.FocusedElement property has only a getter and is not a dependency property, so you cannot bind to it, but you can call the Keyboard.Focus method instead. In order to avoid code-behind, you can either create a small behavior or simply an attached property like this:
public static class KeyboardFocus
{
public static readonly DependencyProperty ElementProperty = DependencyProperty.RegisterAttached(
"Element", typeof(FrameworkElement), typeof(KeyboardFocus), new PropertyMetadata(null, OnElementChanged));
public static FrameworkElement GetElement(DependencyObject dependencyObject)
{
return (FrameworkElement)dependencyObject.GetValue(ElementProperty);
}
public static void SetElement(DependencyObject dependencyObject, FrameworkElement value)
{
dependencyObject.SetValue(ElementProperty, value);
}
private static void OnElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is FrameworkElement frameworkElement)
frameworkElement.Loaded += OnLoaded;
}
private static void OnLoaded(object sender, RoutedEventArgs e)
{
var frameworkElement = (FrameworkElement)sender;
frameworkElement.Loaded -= OnLoaded;
Keyboard.Focus(frameworkElement);
}
}
This attached property can be used on any FrameworkElement, not only TextBox. You can customize it to fit your needs. In XAML apply it to the root element of your UserControl, not the UserControl itself, otherwise the binding element might not be found e.g.:
<UserControl x:Class="FeatureModule.Views.MyDialogView"
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:prism="http://prismlibrary.com/"
xmlns:local="clr-namespace:FeatureModule.Views"
prism:ViewModelLocator.AutoWireViewModel="True"
FocusManager.FocusedElement="{Binding ElementName=edit}"
mc:Ignorable="d" d:DesignHeight="140" d:DesignWidth="400">
<Grid local:KeyboardFocus.Element="{Binding ElementName=edit}">
<!-- ...your markup. -->
</Grid>
</UserControl>
Alternatively, you can set the property on the TextBox itself.
<TextBox local:KeyboardFocus.Element="{Binding RelativeSource={RelativeSource Self}}"/>

How to hide windows or open it behind main aplication

I need to open new window behind main screen. This code doesn't work for me.
Lets assume I have 2 classes
MainWindow.xaml
<Grid>
<Button Click="ButtonBase_OnClick"></Button>
</Grid>
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Test t=new Test();
t.Show();
}
Test class that I need to open behind Main window
<Window x:Class="WpfApplication1.Test"
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="Test" Height="350" Width="525" ShowInTaskbar="False" Visibility="Hidden" >
<Grid>
<TextBox/>
</Grid>
I try to use ShowInTaskbar="False" Visibility="Hidden" but it still doesn't work. The problem I can see "Test" windows 1 sec and after it will be unviable(because Visibility="Hidden") .I need to open this windows many times, it will blinks many times. The better solution to open it behind application.
Is anyone knows how to do?
Try to set the Topmost property of the MainWindow to true:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Test t = new Test();
t.Show();
Topmost = true;
}
This should cause the Test window to be displayed "behind" the main window.
The final solution for me
if (Application.Current.MainWindow != null)
{
Application.Current.MainWindow.Topmost=true;
}

How to Use RadWindow as MainWindow of WPF application

I want to use RadWindow as the main window of my WPF application. So that outer window also take the theme of overall application. I used this approach, but after that application is not being shown on Taskbar anymore. After reading different threads I came to know that because RadWindow is not child of Window so that's why its not possible.
So What is now I am trying to do, is to hide the outer Window completely somehow and to use RadWindow as a child, but parent of all other controls. Following is XAML
<Window x:Class="MyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
WindowStyle="None"
Title="MainWindow" >
<telerik:RadWindow WindowStartupLocation="CenterScreen" Width="Auto" Height="Auto" x:Name="MyRadWindow">
<Grid>
<!-- All Controls here -->
</Grid>
</telerik:RadWindow>
</Window>
But I am unable to completely hide the outer window. It still shows the borders. Then as a second step I have to handle the minimize, maximize and window close events from this RadWidow.
If anyone have tried this approach please help me out or suggest what would be the better way of doing this?
The main purpose of doing all this is to Sync the GUI of Outerwindow with the current TelerikTheme.
There's a simpler solution using the RadWindowInteropHelper like this:
using Telerik.Windows.Controls.Navigation;
public partial class MyRadWindow : RadWindow
{
public MyRadWindow()
{
InitializeComponent();
RadWindowInteropHelper.SetShowInTaskbar(this, true);
}
}
I think you should try to set main class as telerik window instead of nesting inside normal window:
<telerik:RadWindow x:Class="MyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
WindowStyle="None" WindowStartupLocation="CenterScreen" ShowInTaskbar="True"
Title="MainWindow" >
<Grid>
<!-- All Controls here -->
</Grid>
</telerik:RadWindow>
Don't forget to change base class of MyTest.MainWindow to RadWindow
EDIT: Sorry didn't notice provided link.
You can try hide main window with overriding it style by setting following attributes:
WindowStyle="None" Background="Transparent" AllowsTransparency ="True"
First open the MainWindow.xaml file and replace the Window declaration with RadWindow declaration:
<telerik:RadWindow x:Class="RadWindowAsMainWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Loaded="RadWindow_Loaded_1"
Header="MainWindow" Height="350" Width="525">
...
</telerik:RadWindow>
and in code-behind:
`
public partial class MainWindow : RadWindow
{
...
}
2. Then override OnStartup method of the Application class to show the RadWindow:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
new MainWindow().Show();
base.OnStartup(e);
}
}
`
Then in RadWindowLoad event write this:
`
private void RadWindow_Loaded_1(object sender, RoutedEventArgs e)
{
var window = this.ParentOfType();
window.ShowInTaskbar = true;
}
`

VB WPF textblock.text equal a text file

In VB2010 WPF, i have a textblock which, when the window loads, i need the text inside the textblock to be the same as the text in an outside .txt file. How can i do this?
Thanks
Nick
<Window
...
Loaded="Window_Loaded">
...
<TextBlock Name="textBlock" .../>
...
</Window>
and...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
textBlock.Text = System.IO.File.ReadAllText(path);
}

Create a custom click event handler for a WPF usercontrol which contains a button?

have you ever found a problem when assigning a click event handler for your custom WPF usercontrol with a nested button control? I do.
When you put such user control in a main window, let's say Main.xaml, the MouseLeftButtonDown doesn't work, but the PreviewMouseLeftButtonDown works like a charm.
But imagine yourself telling each developer in your team to use this event when using your usercontrol... Some usercontrols in you library has MouseLeftButtonDown, others PreviewMouseLeftButtonDown.... It's a mess don't you agree?
So I've got a solution but I want someone to see if there's some elegant way to create your custom event handler called "Click".
In my usercontrol called CustomButton.xaml.cs, I have so far:
public partial class CustomButton: UserControl
{
public CustomButton()
: base()
{
this.InitializeComponent();
}
public delegate void ClickHandler(object sender, EventArgs e);
public event EventHandler Click;
public void Button_Click(object sender, RoutedEventArgs e) {//execute daddy's button click
(((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e);
e.Handled = false;
}
In my CustomButton.xaml
<UserControl
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"
mc:Ignorable="d"
x:Class="YourCompany.UI.Controls.CustomButton" d:DesignHeight="72.5" d:DesignWidth="200">
<UserControl.Resources>
blablabla
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<Button Style="{DynamicResource CustomButton}"
Width="{Binding ElementName=CustomButton, Path=ActualWidth}"
Cursor="Hand" Foreground="#ffffff" FontSize="28" Margin="8,8,0,12"
HorizontalAlignment="Left"
Content="Custom Button" Click="Button_Click" />
</Grid>
Now in my Main.xaml, the caller, I have:
<Window x:Class="YourCompany.MyProject.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyProject!" Height="600" Width="800"
MinWidth="800" MinHeight="600" WindowState="Maximized" WindowStartupLocation="CenterScreen"
xmlns:bigbola="clr-namespace:YourCompany.UI.Controls;assembly=YourCompany.UI.Controls">
<mycontrols:CustomButton Name="test" MyImage=".\Images\btnOptions.png" Cursor="Hand"
Texto="See options" Click="test_Click"
Margin="168.367,176.702,253.609,0" ToolTip="See all options" Height="76.682"
VerticalAlignment="Top"></mycontrols:CustomButton>
Explanation:
in the usercontrol, when you click the nested button, it executes its parent custom "Click" handler.
Is there a elegant way to accomplish the same effect?
Going off of what mdm20 was saying... Why are you creating a UserControl (a collection of controls grouped into 1) when you could much more easily create a CustomControl (a control that extends the functionality of an existing control, such as a Button)? Assuming a Button is the only control you'd like in CustomButton, I'd highly recommend a CustomControl over what you have (a UserControl).
Example of UserControl vs CustomControl here
Hope this helps!
If your implementing a button, why not just derive from button?
To answer your question though, all you need it this.
if (Click != null) Click(this, EventArgs.Empty);
Couldn't this line:
(((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e);
be replaced by
this.Click(sender, e);
?
Other than that though the answer depends on the exact behaviour that you want. If you want to click event of your user control to only trigger when you click on the inner button then I think you are handling it the right way. On the other hand if you want the click event to trigger whenever you click anywhere within the bounds of the user control then you are probably best styling or inheriting from the standard button control. Remember that in WPF the button's content can be any other element including another button.

Resources