C
C#•3y ago
geno

ā” 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
geno
genoOP•3y ago
<Window.Styles>

<Style Selector="Button">
<Setter Property="Width" Value="120"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="CornerRadius" Value="2"/>
<Setter Property="Background" Value="{StaticResource DarkVividColor}"/>
<Setter Property="Foreground" Value="{StaticResource BrightVividColor}"/>
</Style>
</Window.Styles>
<Window.Styles>

<Style Selector="Button">
<Setter Property="Width" Value="120"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="CornerRadius" Value="2"/>
<Setter Property="Background" Value="{StaticResource DarkVividColor}"/>
<Setter Property="Foreground" Value="{StaticResource BrightVividColor}"/>
</Style>
</Window.Styles>
geno
genoOP•3y ago
geno
genoOP•3y ago
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 ?
geno
genoOP•3y ago
Shinyshark
Shinyshark•3y ago
Is this WPF @Codsworth ?
geno
genoOP•3y ago
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 ?
Shinyshark
Shinyshark•3y ago
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.
geno
genoOP•3y ago
avalonia is pretty much a same , it is WPF and XAML based also so rules that work in WPF also applicable for avalonoia
Shinyshark
Shinyshark•3y ago
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"
Shinyshark
Shinyshark•3y ago
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
geno
genoOP•3y ago
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 ?
Shinyshark
Shinyshark•3y ago
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.
geno
genoOP•3y ago
geno
genoOP•3y ago
why now create a style with <Window.Styles> ?
Shinyshark
Shinyshark•3y ago
In WPF (and probably other languages too) you can use shorthands to set certain values. So you could do <Button Style={StaticResource MyStyle} />
geno
genoOP•3y ago
no I'm asking something else he refers to button directly instead of using <Window.Styles> how it works why button directly
Shinyshark
Shinyshark•3y ago
But you can also define them in a more verbose way by saying:
<Button>
<Button.Style>
Your style things here
</Button.Style>
</Button>
<Button>
<Button.Style>
Your style things here
</Button.Style>
</Button>
It's probably easiest if you try it with a TextBlock. You can set the text directly or via that more verbose syntax.
geno
genoOP•3y ago
and why you have button and then button style inside
Shinyshark
Shinyshark•3y ago
He 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.
geno
genoOP•3y ago
yes I'm doing that like that
<Window.Styles>
<Style Selector="Button">
<Setter Property="Width" Value="120"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="CornerRadius" Value="2"/>
<Setter Property="Background" Value="{StaticResource DarkVividColor}"/>
<Setter Property="Foreground" Value="{StaticResource BrightVividColor}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>

</Window.Styles>
<Window.Styles>
<Style Selector="Button">
<Setter Property="Width" Value="120"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="CornerRadius" Value="2"/>
<Setter Property="Background" Value="{StaticResource DarkVividColor}"/>
<Setter Property="Foreground" Value="{StaticResource BrightVividColor}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>

</Window.Styles>
when I'm saying window.styles does that mean that I'm refering to all buttons in a window ?
Shinyshark
Shinyshark•3y ago
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.
geno
genoOP•3y ago
ok thanks so now only problem is hover ...
geno
genoOP•3y ago
geno
genoOP•3y ago
you mean in separate file right ?
Shinyshark
Shinyshark•3y ago
You can, you do not have to. By the way, if you want to have total control over your button
geno
genoOP•3y ago
so I can register a style in resouirse ?
Shinyshark
Shinyshark•3y ago
it might be easier to make a UserControl.
geno
genoOP•3y ago
I thought resources is for colors
<Window.Resources>
<SolidColorBrush x:Key="DarkVividColor">#434343</SolidColorBrush>
<SolidColorBrush x:Key="BrightVividColor">#DDDDDD</SolidColorBrush>
</Window.Resources>
<Window.Resources>
<SolidColorBrush x:Key="DarkVividColor">#434343</SolidColorBrush>
<SolidColorBrush x:Key="BrightVividColor">#DDDDDD</SolidColorBrush>
</Window.Resources>
I set my colors like that I named my color templates like that
Shinyshark
Shinyshark•3y ago
That's alright.
geno
genoOP•3y ago
to not enter hardcoded values
Shinyshark
Shinyshark•3y ago
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}/>
geno
genoOP•3y ago
<Style Selector="Button" x:Key="myStyleButton"> you mean like that
Shinyshark
Shinyshark•3y ago
Yeah.
geno
genoOP•3y ago
and when selector is button it will choose all buttons
Shinyshark
Shinyshark•3y ago
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.
geno
genoOP•3y ago
and if <Style Selector="Button.XBTN" x:Key="myStyleButton"> what that XBTN says ?
Shinyshark
Shinyshark•3y ago
I don't know what XBTN is. But usually, your style targets a specific element.
geno
genoOP•3y ago
I made it it is kinda key also
Shinyshark
Shinyshark•3y ago
Such as a Button or a TextBlock
geno
genoOP•3y ago
aha . ok thanks for help
Shinyshark
Shinyshark•3y ago
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.
geno
genoOP•3y ago
I have very hard time to recreating that button lol
geno
genoOP•3y ago
Shinyshark
Shinyshark•3y ago
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.
geno
genoOP•3y ago
yes right šŸ„‚
Accord
Accord•3y ago
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.

Did you find this page helpful?