Hi guys,
I have a problem defining an EventHandler in XAML
Please take a look at the code and you will understand what I am trying to do and how I am trying to assign a method to the attached handler.
This is code of attached property representing the handler ( in this question its called attached handler):
public static EventHandler<EventArgs> GetResetValue(DependencyObject obj)
{
return (EventHandler<EventArgs>)obj.GetValue(ResetValueProperty);
}
public static void SetResetValue(DependencyObject obj, EventHandler<EventArgs> value)
{
obj.SetValue(ResetValueProperty, value);
}
// Using a DependencyProperty as the backing store for ResetValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ResetValueProperty =
DependencyProperty.RegisterAttached("ResetValue", typeof(EventHandler<EventArgs>), typeof(Extension), new UIPropertyMetadata(null, OnResetValue));
private static void OnResetValue(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
DependencyObject obj = dependencyObject;
for (; obj != null && !(obj is TextBox); obj = VisualTreeHelper.GetParent(obj)) ;
TextBoxtbx = obj as TextBox;
if (tbx != null)
{
tbx.TextChanged += (EventHandler<EventArgs>)dependencyPropertyChangedEventArgs.NewValue;
}
}
XAML looks like this:
<TextBox><Button><Label ext:Extension.ResetValue=".. here i want to assign the handler, as examle DoSomething" ></Button></TextBox>
The method which i wish to assign to attachedhandler is inside Window.cs looks like this as example:
public void DoSomething(object sender, EventArgs e)
{
....
}
As you can seeI am trying to assign method to attached handler ( look above, attached handler is once again attachedproperty of type EventHandler).
OnResetValue method I travel up the VisualTree till i find the TextBox and I add the handler to TextBox.TextChanged event.
When TextChanged then the attached handler method will be called and assigned method will be called. Its like a chain.
How do I do this? I would like to do this with events.