简体   繁体   中英

How can I disable WPF button highlight when mouse over or when clicked in runtime?

I've this code in C# to create a button as a child of a StackPanel:

` Button myButton = new Button(); //All button stuff (Background, text...).

myStackPanel.Children.add(myButton); `

But, as every button, it highlights every time the mouse is over or when I click it. Is there any way to change that in an easy code (I'm still new to C#) can remove that highlight.

I don't know how to do this. I haven't seen anything explaining this and the only codes I could find were in XAML, and I didn't understand them so couldn't translate them to C#.

I took a look at a few of the answers for this and didn't see any I liked much.

WPF controls are lookless, meaning they have fixed behaviour but not specific look to them. You can re template a wpf control to pretty much anything you can describe in xaml. Many wpf controls have quite complicated templates.

Here's one way to template a button as described.

I've put this style in my window's resources. Usually such styles are in resource dictionaries which are merged in app.xaml.

    <Window.Resources>
    <Style x:Key="NoMouseOverButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="BorderBrush" Value="LightGray"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="Border"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"   >
                        <ContentPresenter Margin="2"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            RecognizesAccessKey="True" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    </Window.Resources> 
    <Grid>
        <Button Style="{StaticResource NoMouseOverButtonStyle}"
                Content="This is my Button"
                Click="Button_Click"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                />
    </Grid>
</Window>

The button references the style as a resource. That style sets some defaults so the button has a border you can see but over ride.

The contentpresenter is critical because this is where whatever you make content of your button will appear.

If I set an actual value on a button then that will over ride the style. Hence

        <Button Style="{StaticResource NoMouseOverButtonStyle}"
                Content="This is my Button"
                Click="Button_Click"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                BorderBrush="Red"
                />

Gives me a red border on my button.

A lightgray border is rather simpler than a button has by default.

You could reproduce that. Maybe that'd be an interesting learning exercise.

Lookup the button template on msdn.

Google: "wpf button template msdn"

Take a look at that. Brace yourself - it is complicated.

See the button border brush is hard coded in the template?

Change the style above so it does the same.

Clue:

  <Setter.Value>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM