So, what is an attached event? Well, basically, it's an event that can be attached to another element without the other element having to be aware that this capability is present. If you think about it as being like an attached property for events then it starts to make sense. So, why do we have this? Well, it allows you to extend WPF through composition rather than inheritance.
I understand perfect "Grid.Row" is an "Attached Property" that it's not a property of "TextBox" itself for instance. I understand analogy when this concept extended to "Events". However - what's confusing is the SYNTAX: you can define an event handler to a RoutedEvent the same way/syntax as you would with AttachedEvent.
But if you see the sample I gave in MainWindow.xaml, I can subscribe to "AttachedEvent" and "RoutedEvent" from xaml in same syntax - the syntax which does not require "Instance" of "GrandChild" (i.e. "MainWindow" does NOT implement event GrandChildRoutedEvClick itself, which is declared in "GrandChild")
From MainWindow.xaml, "RoutedEvent" handler setup like this:
From MainWindow.xaml, "AttachedEvent" handler setup like this:
Despite in "MiddleChild.xaml", I setup handler to "RoutedEvent" the "Propery way"... what's real diff between Routed/AttachedEvent besides phylosophical or syntactical ...?
[local:GrandChild x:Name="Junior" VerticalAlignment="Stretch" GrandChildRoutedEvClick="GrandChild_GrandChildRoutedEvClick"/]
What I am saying is, you can define a DockStateChanged event with syntax of a "RoutedEvent" - not "AttachedEvent", and in some parent control couple layers up, setup event handler using syntax as you would consuming an "AttachedEvent"
As I said. An attached event is a specialist form of routed event. I don't know how I can say it any clearer - other than an attached event IS A routed event that is used for a specific purpose that routed events aren't designed for.
I was brought up to respect my elders. I don't respect many people nowadays.
I am new to WPF applications. I come from a VB6 Windows Forms background. I used to create user controls that I used in UI's to create graphic content controls like buttons with normal, down,and hover images. While I know the ultimate way to do this is create a separate project for my custom controls I am just working withing a single project to get things worked out first.
I have created a Controls folder where I have added several custom controls each inherting from a base control like Button, RadioButton, Slider, Checkbox etc. I can then add properties to each custom control for DefaultImage, DownImage, HoverImage and in the case of a Slider GripImage and GutterImage. The properties have a public set/get property so I can set the images for a specific control and then the control behavior can set the proper image.
There is an Images folder where I put all the images needed for the application.
I have created a Images.xaml ResourceDictionary that defines key names like ButtonDown to the specific image names so the image naming in the template is easier and I can swap out images easily.
A Generic.xaml gets created in the Themes folder where the basic Control Template goes. Not sure if this is best practice for storing each Control Template or not but it is what gets created by default.
My main area of difficulty is creating the template. I know what I want to do but can't get the syntax to work correctly.
In the case of the custom button I want to create triggers for IsPressed and IsMouseOver and set the controls Background ImageBrush to the proper image. When those triggers are false I want to set the background image to the DefaultImage
I'm not sure about the syntax for refering to the image property of the custom Control and how best to access it in the xamal.
I've looked at a lot of examples but none seem to do things the way I am organizing it. I don't want to hard code the images to the control. My ultimate goal is to be able to drag the custom control into a grid using the designer and set the Default, Down, and Hover image properties and then place the control how I want it in the designer. Then if I want to change the entire look of the app I can just swap out images in the Images folder and the GUI will change.
Last Visit: 31-Dec-99 18:00 Last Update: 20-Oct-14 19:08