Victor
Victor

Reputation: 45

WPF button click event

I am making my first WPF window. I placed a grid onto it and divided the grid into rows and columns that resize automatically if window is resized. There are two buttons that fill two of grid's cells. The buttons contents is set to "OK" and "EXIT" respectively. What I have some difficulty to understand is why would these buttons only work if I click on the text but won't react if I click on the area that is around text and still is inside respective button. Is there a way to make it possibe to click anywhere on the button and it will press down (even if I click somewhere long way from the text)? Any help would be appreciated, thanks!

<Window x:Class="INL.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Please enter your details" Height="350" Width="350" MinWidth="350" MinHeight="350" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" WindowStyle="ToolWindow" Background="White">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="4*"/>
        <ColumnDefinition Width="1*"/>                    
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="3*"/>
    </Grid.RowDefinitions>
    <Label Grid.Column="0" Grid.Row="0"  Grid.ColumnSpan="2" Content="Name:"> </Label>
    <Label Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"  Content="Last name:"></Label>
    <Label Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="2" Content="ID:"></Label>
    <Label Grid.Column="0" Grid.Row="6" Grid.ColumnSpan="2" Content="Result:"></Label>
    <TextBox x:Name="TextBoxNamn" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"></TextBox>
    <TextBox x:Name="TextBoxEfternamn" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2"></TextBox>
    <TextBox x:Name="TextBoxPersonnummer" Grid.Column="0" Grid.Row="5" Grid.ColumnSpan="2"></TextBox>
    <TextBox x:Name="TextBoxResultat" Grid.Column="0" Grid.Row="7" Grid.RowSpan="2" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" IsReadOnly="True" IsReadOnlyCaretVisible="True" />
    <Button x:Name="ButtonOK" Grid.Column="1" Grid.Row="7" Content="OK" IsDefault="True" Background="{x:Null}" Click="ButtonOK_Click" ClickMode="Press"></Button>
    <Button x:Name="ButtonExit" Grid.Column="1" Grid.Row="8" IsCancel="True" Content="EXIT" Background="{x:Null}" Click="ButtonExit_Click" ClickMode="Press"></Button>


</Grid>

Upvotes: 2

Views: 2569

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81313

Instead of setting Background to {x:Null}, set it to Transparent.

Setting background to {x:Null} makes surrounding area non-clickable. Null background does not respond to mouse events.

Refer to this for more details - {x:Null} Vs. Transparent.

Upvotes: 5

Related Questions