C
C#ā€¢2y ago
Codsworth

ā” 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
Codsworth
CodsworthOPā€¢2y 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>
Codsworth
CodsworthOPā€¢2y ago
Codsworth
CodsworthOPā€¢2y 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 ?
Codsworth
CodsworthOPā€¢2y ago
Shinyshark
Shinysharkā€¢2y ago
Is this WPF @Codsworth ?
Codsworth
CodsworthOPā€¢2y 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ā€¢2y 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.
Codsworth
CodsworthOPā€¢2y 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ā€¢2y 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ā€¢2y 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
Codsworth
CodsworthOPā€¢2y 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ā€¢2y 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.
Codsworth
CodsworthOPā€¢2y ago
Codsworth
CodsworthOPā€¢2y ago
why now create a style with <Window.Styles> ?
Shinyshark
Shinysharkā€¢2y ago
In WPF (and probably other languages too) you can use shorthands to set certain values. So you could do <Button Style={StaticResource MyStyle} />
Codsworth
CodsworthOPā€¢2y 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ā€¢2y 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.
Codsworth
CodsworthOPā€¢2y ago
and why you have button and then button style inside
Shinyshark
Shinysharkā€¢2y 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.
Codsworth
CodsworthOPā€¢2y 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ā€¢2y 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.
Codsworth
CodsworthOPā€¢2y ago
ok thanks so now only problem is hover ...
Codsworth
CodsworthOPā€¢2y ago
Codsworth
CodsworthOPā€¢2y ago
you mean in separate file right ?
Shinyshark
Shinysharkā€¢2y ago
You can, you do not have to. By the way, if you want to have total control over your button
Codsworth
CodsworthOPā€¢2y ago
so I can register a style in resouirse ?
Shinyshark
Shinysharkā€¢2y ago
it might be easier to make a UserControl.
Codsworth
CodsworthOPā€¢2y 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ā€¢2y ago
That's alright.
Codsworth
CodsworthOPā€¢2y ago
to not enter hardcoded values
Shinyshark
Shinysharkā€¢2y 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}/>
Codsworth
CodsworthOPā€¢2y ago
<Style Selector="Button" x:Key="myStyleButton"> you mean like that
Shinyshark
Shinysharkā€¢2y ago
Yeah.
Codsworth
CodsworthOPā€¢2y ago
and when selector is button it will choose all buttons
Shinyshark
Shinysharkā€¢2y 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.
Codsworth
CodsworthOPā€¢2y ago
and if <Style Selector="Button.XBTN" x:Key="myStyleButton"> what that XBTN says ?
Shinyshark
Shinysharkā€¢2y ago
I don't know what XBTN is. But usually, your style targets a specific element.
Codsworth
CodsworthOPā€¢2y ago
I made it it is kinda key also
Shinyshark
Shinysharkā€¢2y ago
Such as a Button or a TextBlock
Codsworth
CodsworthOPā€¢2y ago
aha . ok thanks for help
Shinyshark
Shinysharkā€¢2y 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.
Codsworth
CodsworthOPā€¢2y ago
I have very hard time to recreating that button lol
Codsworth
CodsworthOPā€¢2y ago
Shinyshark
Shinysharkā€¢2y 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.
Codsworth
CodsworthOPā€¢2y ago
yes right šŸ„‚
Accord
Accordā€¢2y 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.
Want results from more Discord servers?
Add your server