WPF - behaviour of button in template - wpf

I want to create button that always has the same behaviour (close window). Can I add this behaviour to a template?

Templates in WPF are used for layout and data. I am assuming you are looking to attach your code-behind actions to the button template. This won't be done through a template. Instead, you probably want to look at building a custom control. This way you can put your code-behind and your button together.
Here is an article on WPF templates and how they work:
http://wpf.2000things.com/tag/templates/
Here is a forum post on rolling your own button custom control:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a81ed36b-c9d1-4619-96e2-4025b919819c/
Finally, here is a good article from MSDN on all of these issues:
http://msdn.microsoft.com/en-us/magazine/cc163421.aspx

Related

Custom Control vs Custom Behavior in WPF

What is the difference between Custom Control and Custom Behavior?
Where Custom Control should be used and where Custom Behavior should be. in what ways they can be best used?
Behaviour extends control functionalities
Custom controls customize the visual of Control
A behaviour can be used to extend the functionality of a control to do something that it cannot do on its own without having to modify or re-implement the entire control.
Consider for example the built-in TreeView control in WPF. It has a SelectedItem property that is read-only which means that you cannot two-way bind it to a property of your view model. If you don't want to implement your own custom TreeView control from scratch just because of this - which you probably don't :) - you could solve this by implementing a custom behaviour that sets the value of your source property whenever the value of the target property changes and vice versa. Please refer to the following blog post for more information about this and an example: https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/
The following article should also provide a good introduction to attached behvaiours: https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF
So a behaviour is basically a piece of code that can be attached to some element in the XAML markup of a view through attached properties and add additional functionality to this element.
A control is a UI component that encapsulates some functionality and has a template such, as for example a Button or a ListBox.
Behaviors encapsulate pieces of functionality into a reusable component.
Custom controls are more work than custom behaviors(White box vs black box)
Some things you have to do a custom control, eg Access Protected members.

How to make custom WPF ContentControl that supports drag-and-drop at design time?

I want to create custom WPF control that has a single "child" control inside. Subclassing ContentControl or UserControl works, but has one flaw: these controls don't work in designer mode.
By "don't work" I mean this scenario: suppose I have a Canvas with my custom control in it. I want to put, say, a Button inside my control. I drag it from the toolbox, and it appears inside my control. However, XAML view shows that the new button actually belongs to Canvas, not to my control.
I can place it inside my control by manually editing XAML, but I want the designer to work too.
Interestingly, when I subclass Canvas, Grid or Panel, designer works as expected. However, these controls have many children, which is not what I need.
How can I make a single-child control that works in designer?
how about inheriting from Border? that way you could spare yourself the hassle with Designer Extensibility
I had the same problem with a content control I am writing and found an easy solution on this
StackOverflow thread.
Just implement the HitTestCore method:
protected override System.Windows.Media.HitTestResult HitTestCore(System.Windows.Media.PointHitTestParameters hitTestParameters)
{
return new PointHitTestResult(this, hitTestParameters.HitPoint);
}
I also had a similar question here.
But after digging and digging it seams that the real answer is "NO", there isn't any official way to support dragging controls straight into a custom Content-Control at Design-Time, even implementing 'HitTestCore' as Stephan's answer suggests, does not enable drag&drop at design-time for ContentControl.
For such purposes you should consider inheriting from either Grid or Panel (to allow multiple child controls), or Border (to allow single child).

VisualstateManager in Style

I am new to both Silverlight and Blend 4.
I am trying to make a Image Gallery, where u click on the image and it shows details of the same.
I used VisualStateManager to get a mouseOver and mouseOut effect to the thumbnails. and here is wat i want. I want to add the VisualState's to all my thumbnails through Style. (I had seen this in some forum, but i cudnt figure it out how he did it.)
Here is wat i want:
I have set of thumbnails to, which need to scale up on MouseOver and come back to normal on MouseOUT. I created a VisualStateManager States.
But i want to use the state as a
common state for all the thumbnails
and apply it to the thumbnails through
Style.
Is this possible? If so how?
If not? then is how can i achieve it.
Would be really thankful to any one who can help me :) just that this is a bit urgent. :(
MSDN documentation says that you should be able to add the VisualStageManager XAML tag to any control which inherits from UIElement and accepts child controls.
It seems to me then that the best way to do this is to create a UserControl for your thumbnail and set the VisualStateManager there. You can then reuse that UserControl.
More reading material:
MSDN UserControl documentation
Adding behaviour to stock controls

Silverlight: Creating a modal control

I have a window with some contents. I'd like to click a button and another control (a grid/border) slides up. But i'd like the contents of the window that is under this slided up control to be modal. I cannot click or use keyboard to activate anything.
Thank you.
For a modal window I would use the ChildWindow class. Microsoft provides the templates used for all of their major controls and objects so one can take what they did and change it. The ChildWindow template and styles page has a pretty good explanation of the layout so one can figure out what to change. You should just be able to instantiate a new ChildWindow, set its template to your custom template, and rock out!
Sounds to me you could do with using the ChildWindow control instead, which handles most of this for you. Make a copy of its template and tweak it up to get your slide-in effect.
You can create a control filling the complete canvas and make it transparent.

Best way to modify Button behavior and Content, but maintain styles?

I'm trying to create a control in Silverlight that inherits from Button so that I can perform a specific action everytime it is clicked. I'm doing this because I'd like to reuse this custom button in several locations with the same functionality.
I'd like to create the control in such a way so that I have a can set the custom Button's Content to a specific default icon image, but still have the rest of the button's style coming from either the default button style, or being automatically set by the toolkit Themes.
I'd also like to have the Content be described and editable in XAML rather than code if possible.
It seems like this would be a pretty common problem for Silverlight developers - is there a good way to tackle it?
If you use a normal button and edit an "Empty Template", then you can style the button to have any content you wish and expose properties that you can set in the XAML for Icons etc.
By using the standard button control, you will have all the behaviors that you require.
I believe this is what you're looking for, if not can you expand on your question.
--EDIT--
Ok, I get what you are trying to do now. So what you might want to consider is creating a custom button class that inherits from Button. Then you can override the OnClick method to handle your logic. When it comes to the XAML, you can create a template style for a TargetType of your custom button class, that would be styled to your requirements.
HTH, if you need some examples place a comment and I'll mock up some examples

Resources