wpf-使整个盒子可点击

时间:2017-12-14 00:24:34

标签: c# wpf xaml

我在WPF中有一个矩形文本框,文本框中已经有一个可点击的按钮。我需要使整个矩形文本框可以点击,这样用户就可以点击文本框中的任意位置,而不仅仅是按钮进行交互。

是否可以这样做?

这是我的XAML:

<Grid Background="{StaticResource TinyGrayBrush}" Height="260" Width="530" Margin="10,10,10,10" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="120*"/>
        <ColumnDefinition Width="140*"/>
    </Grid.ColumnDefinitions>

    <Grid Grid.Column="0">
        <Image x:Name="imgCard" Source="{StaticResource Exception-NoImageAvailable}" Stretch="Fill" />
        <Image x:Name="hotpickEn" Source="{StaticResource HotPickEn}" Height="20" Width="70" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0" Visibility="Hidden" />
        <Image x:Name="hotpickCh" Source="{StaticResource HotPickCH}" Height="20" Width="70" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0" Visibility="Hidden" />
    </Grid>
    <Grid x:Name="gdEnglish" Visibility="Visible" Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="12*"/>
            <RowDefinition Height="62*"/>
            <RowDefinition Height="90*"/>
            <RowDefinition Height="5*"/>
            <RowDefinition Height="55*"/>
            <RowDefinition Height="10*"/>
        </Grid.RowDefinitions>
        <Grid.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="25,5,25,5"/>
            </Style>
        </Grid.Resources>
        <TextBlock Grid.Row="1" x:Name="txtTitle" Text="SPARKLING SUNDAY BRUNCH BUFFET" FontSize="20" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource SemiDarkGrayBrush}" Margin="5,0,5,6.5" />
        <TextBlock Grid.Row="2" x:Name="txtDesc" Text="Pop your Sunday Morning with luxury sparkling bubble" FontSize="16" TextWrapping="Wrap" Foreground="{StaticResource GrayBrush}" Margin="5,3.5,5,6.5" />
        <TextBlock Grid.Row="3" x:Name="txtPoint" Text="138,900 GP" FontSize="21" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource GrayBrush}" Margin="25,3.5,25,6"  />
        <Button Grid.Row="4" x:Name="btnFindOutMore" Content="FIND OUT MORE OR REDEEM" Style="{StaticResource SmallWhiteRedButton}" Margin="12,0,12,0" FontSize="20" />
    </Grid>
    <Grid x:Name="gdChinese" Visibility="Hidden" Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="12*"/>
            <RowDefinition Height="62*"/>
            <RowDefinition Height="90*"/>
            <RowDefinition Height="5*"/>
            <RowDefinition Height="55*"/>
            <RowDefinition Height="10*"/>
        </Grid.RowDefinitions>
        <Grid.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="25,5,25,5"/>
            </Style>
        </Grid.Resources>
        <TextBlock Grid.Row="1" x:Name="txtCHTitle" Text="SPARKLING SUNDAY BRUNCH BUFFET" FontSize="20" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource SemiDarkGrayBrush}" HorizontalAlignment="Left" Margin="5,4.667,0,5.333" />
        <TextBlock Grid.Row="2" x:Name="txtCHDesc" Text="Pop your Sunday Morning with luxury sparkling bubble" FontSize="16" TextWrapping="Wrap" Foreground="{StaticResource GrayBrush}" HorizontalAlignment="Left" Margin="5,4.667,0,5.667" />
        <TextBlock Grid.Row="3" x:Name="txtCHPoint" Text="138,900 GP" FontSize="21" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource GrayBrush}" HorizontalAlignment="Left" Margin="25,3.5,25,6" Width="105"  />
        <Button x:Name="btnCHFindOutMore" Grid.Row="4" Content="FIND OUT MORE OR REDEEM" Style="{StaticResource SmallWhiteRedButton}" Margin="15,0,10,0"/>
    </Grid>

1 个答案:

答案 0 :(得分:1)

正如您使用Code-Behind:

你基本上也订阅了Grid的事件

// Original Data.
const data = [{time: 26, price: 19.99}, {time: 26, price: 17.99}, {time: 26, price: 22.45}, {time: 27, price: 25.21}]

// Object Form.
const unique = data.reduce((accumulator, point) => ({...accumulator, [point.time]: point.price}), {})

// Array Form.
const array = Object.keys(unique).map((time) => ({time, price: unique[time]}))

// Result.
console.log('Object Form', unique) // Object Form.
console.log('Array Form', array) // Array Form.

添加一个新的私有变量以指示当前状态

gdEnglish.TouchDown += gdEnglish_TouchDown;
gdEnglish.TouchUp += gdEnglish_TouchUp;
gdEnglish.TouchLeave += gdEnglish_TouchLeave;
gdEnglish.MouseDown += gdEnglish_MouseDown;
gdEnglish.MouseUp += gdEnglish_MouseUp;
gdEnglish.MouseLeave += gdEnglish_MouseLeave;

并创建不同的事件处理程序

private bool gdEnglish_clickflag = false;

我仍然建议您使用WPF研究MVVM主题

它会使这样的事情变得更加干净和简单