ā How to create a Template for a button ?
Hi I can' create a button that I can use.. I need a global template description for button that I will be able to use in any apps
46 Replies
this gives me that , but I can't control the hover over background in same time it becomes white color
while I need to set custom color, how to do that ?
Is this WPF @Codsworth ?
Avalonia @Shinyshark
I just don't understand if I'm doing that correctly at all , Im new in avalonia and in WPF in general also in XAML
I need to create a button template, of course to not recreate needed button many times
is
<Window.Styles> </Window.Styles>
correct approache ?I have no experience with Avalonia but in WPF, you can use Data Triggers to specify background colours.
You can specify what should happen if a certain event is fired.
avalonia is pretty much a same , it is WPF and XAML based also
so rules that work in WPF also applicable for avalonoia
https://stackoverflow.com/a/17259993/12175949 If you look at this answer, toward the bottom
Stack Overflow
How do you change Background for a Button MouseOver in WPF?
I have a button on my page with this XAML:
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom"
Width="50" Height="50" HorizontalContentAlignment="Left"
You could play around with that a bit.
You could probably also do what you want in the code behind of the template view.
But it would probably be cleaner to make use of data triggers so that all your view logic is happening close to each other.
I don't know if Avalonia has triggers though. I googled it and the first thing I saw was a question asking people how they could replicate WPF Data Triggers in Avalonia
ok thanks I will read that ... sure it will have that triggers but in avalonia all has different names
also rider sometimes isn't refresh a viewer and it is so bad .. you don't know your settings worked or not
in a code you sent , what does that mean ?
<Button.Style>
why he sets a style property inside a button ?He is creating a style for a button so that he can apply that style to a button.
Where did you see <Button.Style> ?
Oh in the question.
why now create a style with
<Window.Styles>
?In WPF (and probably other languages too) you can use shorthands to set certain values.
So you could do <Button Style={StaticResource MyStyle} />
no I'm asking something else he refers to button directly instead of using
<Window.Styles>
how it works why button directlyBut you can also define them in a more verbose way by saying:
It's probably easiest if you try it with a TextBlock. You can set the text directly or via that more verbose syntax.
and why you have
button
and then button style
insideHe makes a button and then he sets the style inside of that button.
You don't have to do that. You can also make a style outside of the button and then specify that style on the button.
yes I'm doing that like that
when I'm saying
window.styles
does that mean that I'm refering to all buttons in a window ?Yeah, that's alright. If you create a style and give that style no name, it will apply to all elements that this style targets.
So long as they have no style set yet.
I'm not familiar with Avalonia at all so I don't know how it works exactly but you can create styles and register them in your resource dictionary so that they can be used elsewhere.
ok thanks so now only problem is hover ...
you mean in separate file right ?
You can, you do not have to.
By the way, if you want to have total control over your button
so I can register a style in resouirse ?
it might be easier to make a UserControl.
I thought resources is for colors
I set my colors like that
I named my color templates like that
That's alright.
to not enter hardcoded values
You've registered a resource with a key.
Your style is also a resource.
If you give your style a key, you can use it on the shorthand style attribute.
<Button Style={StaticResource MyButtonStyle}/>
<Style Selector="Button" x:Key="myStyleButton">
you mean like thatYeah.
and when selector is button it will choose all buttons
If you leave out the key, all buttons without a style will use that style.
When you specify a key, you need to give each button that should use that style the style itself.
and if
<Style Selector="Button.XBTN" x:Key="myStyleButton">
what that XBTN says ?I don't know what XBTN is.
But usually, your style targets a specific element.
I made it it is kinda key also
Such as a Button or a TextBlock
aha . ok thanks for help
Xaml elements are lookless. You can change the way they look entirely without damaging their functionality.
within reason of course. If your button is invisible it's not very useful
But styles let you determine everything about its looks.
I have very hard time to recreating that button lol
Start out small by creating a button.
Then make a style and change one small thing about it and play around with it until that style applies.
yes right
š„
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.