Controlling the appearance of custom button's predefined content - c#

I would like to create a custom button template that has a predefined content: a vector image (Path) and text (TextBlock). Then I would like to define a style that, for example, would control the Path.Fill or TextBlock.Foreground based on different visual states.
What is the best approach to achieve this? Do I need to create a custom control that inherits from ButtonBase, and then expose dependency properties for Path and Text, or is there a better way?
Just to be clear, the idea is that later I can create multiple instances of this button, and just assign to it a Path and a Text.

Define a Style for the Button, set the ControlTemplate in the style to use the Path and TextBlock. Define a couple of Attached Properties to modify Path and TextBlock visual state, if you have a need for something beyond what a button template could provide you.

Related

WPF (C#) Usercontrol remove elements

I am making a template using Usercontrol in WPF(C#).
However, when applying this user control, is it possible to subtract a specific part? For example, removing a button?
To substract specific parts from UserControl, Visibility (Collapse, Hidden) option can be used.
Make sure to add dependency property in UserControl for Visibility to show & hide specific part.
It sounds like you are just trying to hide an existing button, which you should do by setting Visibility to Visibility.Collapsed or Visibility.Hidden. This should be done through a binding to the ViewModel of your user control.
If you need a pure XAML solution: No it is not possible as such. However, the reverse is possible: you can add content to a user control, and that effectively provides the same functionality.
What you could do is make a base user control that doesn't contain the button, and instead has a content presenter. A second user control could wrap the base user control and define a button as its content. Then when you don't want to use the user control with the button you can simply create an instance of the base user control.

Creating User Controls without predefined properties C# WPF

I am aiming at creating different UserControls for my college project, in which I am attempting to use ContentControl to wrap my UserControl. I've placed other Controls like Image, WebBrowser, MediaElement and the like, now I have reached a stage where I need to set the properties for my UserControls. Thus, I thought of making use of PropertyGrid Control, but now the problem I am facing is in the PropertyGrid control, as I get all the default properties of the Controls, which in my case I don't want.
For Eg: if I use Image Control then i need properties like Source and Stretch to be displayed only in the PropertyGrid. Can anyone help me in achieving this?
I tried to override some default properties like "Name" and assign it as [Browsable(false)] to hide it from being displayed. I don't want to do this for all the other properties which are being displayed and which are not under my requirements as well.
I am using Xceed.Wpf.Toolkit for my PropertyGrid.
It is explained in the documentation on how to do that:
http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid
When you change the selected object see SelectedObjectType and set PropertyDefinitions in code to match the properties you want to see for that type of object.

Create Simple Custom XAML / C# button

First of all, I'm new to XAML / C# (am iOS/Android programmer) so please bear with me instead of immediately voting down. My app has some GridViews that contain buttons. A lot of these buttons are similar and I would like to refactor this. For simplicity let's say my buttons are just Rectangles with a given color. This color might come from the Item class that defines the particular item in the GridView, or it might be hardcoded. I want the rectangle to change color on hover and pressed states. I want these colors to be parameters as well.
What is the best way to achieve this?
I tried to make a Button subclass but somehow I couldn't access the dependency properties in the VisualStateManager
I tried to write stuff in the code-behind but then I wasn't sure how to delegate the click command to the ViewModel class.
Could someone give me a small working example?
Thanks
You can do this with style templates.
In the Visual Studio designer, right-click on your button and then select Edit Template and then select Edit a Copy....
You will then be prompted to name your new style and also for which file to store it in. For now, just give it a unique name such as MyButtonStyle, and select the current file.
Visual Studio will then add a copy of the style to the current xaml document, and will update your button to use the new style.
<Button x:Name="Download" Style="{StaticResource MyButtonStyle}"></Button>
After this, you can update the new style including changing the colors for the different visual states such as hover or clicked.
You can then proceed to use the new style in other buttons in the same document. To use the style in multiple xaml documents, you have to pull it out into a common resource file.
So you want to adjust your button using custom properties. This is a good time to use a custom control. You can create whatever dependency properties you want and adjust your layout in your code. http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx

Custom checkbox in silverlight

How to create custom checkbox that looks like the one shown below. The checkboxes are dynamically drawn and can have custom colors.
Well, you can just style it. Take the style from here and change it. The key component in the style is VisualStateManager, make sure you understand what it does.
Also you can create a custom control, you can read about it, for example, here.
What I would do is, create a custom control called ImageCheckBox which inherits from the default CheckBox class, add in three dependency properties of type ImageSource, called CheckedImage, IndeterminateImage and UncheckedImage. Just toggle their Visibility or Opacity based on the control's CheckStates, i.e. Checked, Unchecked and Indeterminate.

Load Image to a button by Code behind(C#) in WP7

Being a beg. me facing a problem. Please help me out.
I have created a "Style" in xaml and named it "CustomButton" for creating a button( which consist of Two Images and one Textblock) and want to load one of the image and text to TextBlock only at runtime i.e by code behind so that ill be having diffrent image and differnt text for each button. Actully, me need create an array of Buttons of the same style but diff. Image.
Style mystyle = (Style)Application.Current.Resources["CustomButton"];
Setter templateSetter = (Setter)mystyle.Setters[0];
btnNext.Style = mystyle;
i created "style" in App.xaml and in code behind i call name style.
hope this help !
Thongaduka !
Depending how much XAML you want, you need to either create a custom UserControl that inherits from Button, with a DependencyProperty for the background.
Or you can specify a ImageBrush for the Background property, and use that, along with the Content property, in your custom style. The ImageBrush approach is going to require some 3-4 lines of XAML per button.
And I wouldn't recommend creating any UI controls from C#, as you can do everything you want to using databindings. If you're attempting to render custom buttons inside a listbox, with a custom background, simple databinding work should do just fine, rather than creating any custom controls or styles.
Feel free to clarify (with code!) what you're attempting to do now.

Categories