I'm simply trying to reset the value of a NumericUpDown element in xaml to 0 when IsEnabled becomes false, but checking for IsEnabled changes is not a routed event. This is what I currently have which doesn't work because this event is not a Routed Event
My XAML code:
<CheckBox Name="AirportTemplate">Airport</CheckBox>
<NumericUpDown Name="AirportToGen"
Width="300"
Minimum="0"
IsEnabled="{Binding ElementName=AirportTemplate, Path=IsChecked}"
IsEnabledChanged="ResetValueAirport"/>
My Code Behind C#
private void ResetValueAirport(object sender, EventArgs e)
{
if (!AirportToGen.IsEnabled)
{
AirportToGen.Value = 0;
}
}
I'm guessing you are using the NumericUpDown class form the extended WPF toolkit, in which case the IsEnabledChanged event requires a handler with the signature private void handler(object sender, DependencyPropertyChangedEventArgs e). So in your code behind use this instead:
private void ResetValueAirport(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
{
if (!AirportToGen.IsEnabled)
{
AirportToGen.Value = 0;
}
}
Related
I created a simple Blend behaviour to be attached to TextBox elements. It's purpose is to scroll the textbox to its end when it gets the focus, and to scroll it back to the beginning when it loses the focus.
public class TextBoxScrollToEndBehaviour : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.GotFocus += AssociatedObject_GotFocus;
AssociatedObject.LostFocus += AssociatedObject_LostFocus;
}
private void AssociatedObject_LostFocus(object sender, System.Windows.RoutedEventArgs e)
{
var textBox = sender as TextBox;
textBox.ScrollToHorizontalOffset(0);
}
private void AssociatedObject_GotFocus(object sender, System.Windows.RoutedEventArgs e)
{
var textBox = sender as TextBox;
textBox.ScrollToHorizontalOffset(double.PositiveInfinity);
}
}
Xaml:
<TextBox Text="{Binding MyBinding, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Behaviors>
<behaviours:TextBoxScrollToEndBehaviour />
</i:Interaction.Behaviors>
</TextBox>
It works great when I focus the TextBox and then I click on some other control to loose the focus. Problem is that if I switch the focus between two TextBox that share the same behaviour, the scroll is not set back to 0 on the first TextBox, even the LostFocus event is correctly triggered on it.
What am I missing here? Thanks!
.NET Framework 4.7.2
I found out that by replacing this line in the LostFocus event:
textBox.ScrollToHorizontalOffset(0);
with
textBox.ScrollToLine(0);
the behaviour works perfectly in any condition.
Is there anyway to add right click events to all textbox controls in silverlight without needing to manually adding it to each control in the whole project?
doing like:
<TextBox x:Name="txtName" MouseRightButtonUp="txtName_MouseRightButtonUp"
MouseRightButtonDown="txtName_MouseRightButtonDown" /></TextBox>
then fixing the events in the .cs for about 50+ (hopefully it's just 50+) textboxes can take a while.
If not then what might be the easiest way to do this?
You can extend your textbox
class SimpleTextBox
{
public SimpleTextBox()
{
DefaultStyleKey = typeof (SimpleCombo);
MouseRightButtonDown += OnMouseRightButtonDown;
}
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs
mouseButtonEventArgs)
{
//TODO something
}
}
==========
And use this control.
Or as alternative solution - you can create behavior:
CS:
...
using System.Windows.Interactivity;
public class TextBoxBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseRightButtonDown += AssociatedObject_MouseRightButtonDown;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.MouseRightButtonDown -= AssociatedObject_MouseRightButtonDown;
}
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
e.Handled = true;
// DO SOMETHING
}
}
XAML:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<TextBox ...>
<i:Interaction.Behaviors>
<local:TextBoxBehavior />
</i:Interaction.Behaviors>
</TextBox>
And attach this handler to your TextBox general style.
My answer to this question is also the answer to your question.
In short it's probably easiest to derive a type from TextBox, put your MouseRightButtonDown event handler in there and replace all existing instances of textBox with your type.
I am trying out custom routed events, but I get a TargetInvocationException when compiling with an Attached Event Handler.
I have the following code inside custom control EventRaiserControl:
public static readonly RoutedEvent KickedEvent = EventManager.RegisterRoutedEvent("KickedEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(EventRaiserControl));
public event RoutedEventHandler Kicked
{
add
{ this.AddHandler(KickedEvent, value); }
remove
{ this.RemoveHandler(KickedEvent, value); }
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs(KickedEvent));
}
I then have the following XAML in my main window:
<StackPanel local:EventRaiserControl.Kicked="StackPanel_Kicked">
<local:EventRaiserControl Kicked="EventRaiserControl_Kicked"/>
</StackPanel>
With the following handlers in the MainWindow code behind:
private void StackPanel_Kicked(object sender, RoutedEventArgs e)
{
Console.WriteLine("Caught Kicked Event at Panel level.");
}
private void EventRaiserControl_Kicked(object sender, RoutedEventArgs e)
{
Console.WriteLine("Caught Kicked Event at Control level.");
}
My code works fine with this handler:
<local:EventRaiserControl Kicked="EventRaiserControl_Kicked"/>
But fails with TargetInvocationException the moment I add the attached handler:
<StackPanel local:EventRaiserControl.Kicked="StackPanel_Kicked">
Can somebody help? What am I missing / misusing?
Many thanks
At first I was surprised why it is, but I saw the reason after coding as your code. Just change
EventManager.RegisterRoutedEvent("KickedEvent"....
to
EventManager.RegisterRoutedEvent("Kicked"....
What is the equivalent Following code in wpf
code in winapp :
private void textBox1_Enter(object sender, EventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
There is no Enter event on a WPF textbox - you could use the GotFocus event to the same effect though.
private void textbox1_GotFocus(object sender, System.Windows.RoutedEventArgs e)
{
MessageBox.Show("www.stackoverflow.com");
}
this is accessed in the XAML as follows:
<TextBox GotFocus="textbox1_GotFocus"/>
I am wondering if anybody knows why the datepicker will pass standard keys to any parent control's key down routed event, but not the return key?
here's the xaml i wrote:
<WrapPanel Name="_wpParameters"
Grid.Row="0" Grid.Column="0"
Orientation="Horizontal"
Grid.IsSharedSizeScope="True"
Keyboard.KeyDown="_wpParameters_KeyDown" >
<!-- this is where the dynamic parameter controls will be added -->
</WrapPanel>
Here is the code i was using to check for the return key:
private void _wpParameters_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
RaiseEvent(new RoutedEventArgs(LoadLiveResultsEvent, this));
}
}
I was using key down on accident (meant to use key up) but I found it interesting that the standard numeric and / characters were firing the logic, but not the return key. Any Idea's why the return key is not included as a key down key?
The KeyDown event is a lower-level
text input event that might not behave
as expected on certain controls. This
is because some controls have control
compositing or class handling that
provides a higher-level version of
text input handling and related
events.
As viewed on MSDN...my assumption is that the control is consuming the event and perhaps committing the text to the bindable source and other cleanup and then marking the event as handled.
In addition had to mention my solution.
I had a parent view, that handle the keyDown event from all child viewmodels.
I declared a behavior for special controls like DatePicker,MaskedTextBox,etc... that catch previewKeyDown tunneling event and raise KeyDown bubbling event:
public class EnterPressedBehavior : Behavior<UIElement>
{
public ICommand EnterPressedCommand { get; private set; }
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewKeyDown += EnterPressed;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PreviewKeyDown -= EnterPressed;
}
private void EnterPressed(object sender, KeyEventArgs keyEventArgs)
{
if (Keyboard.PrimaryDevice != null && Keyboard.PrimaryDevice.ActiveSource != null)
{
var eventArgs = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, keyEventArgs.Key) { RoutedEvent = UIElement.KeyDownEvent };
AssociatedObject.RaiseEvent(eventArgs);
}
}
}
this behavior assigned to datePicker:
<DatePicker x:Name="BirthDateDatePicker" Grid.Column="1"
Grid.Row="6" Margin="3" HorizontalAlignment="Stretch"
IsEnabled="{Binding PersonFieldsEditDenied}"
Validation.ErrorTemplate="{StaticResource DefaultValidationTemplate}"
AutomationProperties.AutomationId="BirthDateDatePicker">
<i:Interaction.Behaviors>
<viewModels:EnterPressedBehavior />
</i:Interaction.Behaviors>
</DatePicker>
which is listened by parent view:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title=""
KeyDown="OnKeyDownHandler">
Code behind:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// your code
}
}