C
C#4mo ago
RANI

how to make it so when u hover on button it turn blue and when you not it turn back to white

serched on google didnt found it, just start blue all the timer
25 Replies
RANI
RANI4mo ago
private void button1_enter(object sender, EventArgs e) {
} //code /code !code
Pobiega
Pobiega4mo ago
Winforms? WPF? Avalonia? Blazor?
RANI
RANI4mo ago
wpf
Pobiega
Pobiega4mo ago
@leowest I summon thee
leowest
leowest4mo ago
hi there
RANI
RANI4mo ago
hi
leowest
leowest4mo ago
u have to write a trigger I think I have an example moment
RANI
RANI4mo ago
bet, would help like trigger class?
leowest
leowest4mo ago
<Button Width="200" Height="40" Content="My Cool Button">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="LightBlue" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" Value="LightSkyBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
<Button Width="200" Height="40" Content="My Cool Button">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="LightBlue" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" Value="LightSkyBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
The ControlTemplate is what changes the IsMouseOver and IsPressed events on the button
RANI
RANI4mo ago
all this to color it blue? damn got some work
leowest
leowest4mo ago
when u hover it yes ofc in this example its not blue but yeah
leowest
leowest4mo ago
RANI
RANI4mo ago
trigger class?
leowest
leowest4mo ago
?
RANI
RANI4mo ago
nvm
leowest
leowest4mo ago
u write this in xaml not in c# code that whole mostruocity is the button if its too confusing let me know I can try to explain the proccess of doing this
RANI
RANI4mo ago
that only way? no way doing it using private void button1_enter(object sender, EventArgs e) { } havnt learn xaml yet, just started wpf
leowest
leowest4mo ago
well WPF is not WinForms u dont drag, drop, move the controls using the designer you write the controls via XAML its the most important part of it to keep your design consistent $rulesofwpf
MODiX
MODiX4mo ago
Rules of WPF:

❌ Avoid the WPF Designer to eliminate a category of confusing bugs
❌ Don't rely on Margin as the primary tool for layouts
❌ Avoid writing UserControls or subclassing to extend a default control -- use Behaviors instead (Microsoft.Xaml.Behaviors.Wpf)

✅ Write XAML by hand and autoformat with "Ctrl K,D" or XAML Styler
✅ Rely upon XAML Hot Reload to design your app's UI at runtime
✅ Use layout controls (Grid, DockPanel, etc) to support proper resizing
✅ Use data binding to eliminate glue code and state synchronization issues
✅ Use collection controls and DataTemplate to dynamically create lists of controls
✅ Learn MVVM to create maintainable apps
✅ Use the Dispatcher to update controls from non-UI threads
✅ WPF's default controls can be easily modernized via $wpfuilibs
✅ Include relevant XAML, code-behind, and ViewModel code for questions when possible
Rules of WPF:

❌ Avoid the WPF Designer to eliminate a category of confusing bugs
❌ Don't rely on Margin as the primary tool for layouts
❌ Avoid writing UserControls or subclassing to extend a default control -- use Behaviors instead (Microsoft.Xaml.Behaviors.Wpf)

✅ Write XAML by hand and autoformat with "Ctrl K,D" or XAML Styler
✅ Rely upon XAML Hot Reload to design your app's UI at runtime
✅ Use layout controls (Grid, DockPanel, etc) to support proper resizing
✅ Use data binding to eliminate glue code and state synchronization issues
✅ Use collection controls and DataTemplate to dynamically create lists of controls
✅ Learn MVVM to create maintainable apps
✅ Use the Dispatcher to update controls from non-UI threads
✅ WPF's default controls can be easily modernized via $wpfuilibs
✅ Include relevant XAML, code-behind, and ViewModel code for questions when possible
leowest
leowest4mo ago
when using the designer and moving things around it will generate a lot of garbage code margins for example that can place your controls out of bounds of visibility @RANI I suggest you take a trip on this site https://wpf-tutorial.com/ its great if u are just starting with WPF, it will show u all the various controls how to write XAML how most of the controls work and their purpose
MODiX
MODiX4mo ago
That command had an error
UnknownCommand: Unknown command.
Remove your reaction to delete this message
Buddy
Buddy4mo ago
Maybe you didn't find it because you dont provide it with enough context to go for You can't simply write how to make button go blue, you need to specify the framework you are using and what language you want it in. Also don't type long sentences into google, summarize what you want very short.
leowest
leowest4mo ago
I think the confusion comes from the fact he wanted to do it from code only, and XAML is a bit different
RANI
RANI4mo ago
ye i try to do it code only
leowest
leowest4mo ago
u would have to write dependencies that would be a lot more than just messing with the xaml to change templates certain things u can certainly do from code when its publicly available some require a major workaround to reach it for example <button x:Name="MyButton" /> now u can access MyButton from code and do say MyButton.Foreground and change the text color but there is a limit of how much u can access without having to resort to reflection and whatnot if u were to write from c# u would have to write a Style, extract the ControlTemplate, recreate the triggers, and apply to the button and that is not trivial