【问题标题】:Button not fully clickable按钮无法完全点击
【发布时间】:2017-01-25 08:47:33
【问题描述】:

我刚刚编写了一个没有 Windows 边框的自己的窗口。所以我的退出按钮只是右上角的一个按钮。这个按钮不是完全可点击的,这意味着我可以只点击按钮的上 5 个像素。之后,按钮不会对悬停甚至单击做出反应。 虽然我试图将按钮放在其他地方,但它在窗口中间工作,但在窗口的上侧不起作用。

看起来不错。

在 Snoop 中,按钮上没有任何内容。

这是我的代码,我找不到原因。

<Window x:Class="LatikeManager.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:LatikeManager"
    mc:Ignorable="d"
    Title="Project LATIKE"  
    Icon="Resources/pcc.ico" 
    Height="800" 
    Width="1000" 
    Background="#FF222525" 
    MinHeight="400" 
    MinWidth="500" 
    WindowStyle="None" 
    AllowsTransparency="True" 
    SizeToContent="Manual"
    ResizeMode="CanResizeWithGrip">
<Border BorderThickness="1">
    <!--complete grid-->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <!--title & window settings-->
        <Grid Background="#FF131212">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <!--Title-->
            <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                <Image Width="25" Height="25" Margin="2 2 2 2" VerticalAlignment="Center"/>
                <TextBlock VerticalAlignment="Center" Margin="10 0 0 0" Foreground="LightGray" FontWeight="Bold">Project LATIKE - Latike Manager</TextBlock>
            </StackPanel>
            <!--Window settings-->
        <StackPanel Orientation="Horizontal" Grid.Column="1">
            <Button>x</Button>
        </StackPanel>
    </Grid>
        <!--menuebar to load different modes-->
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <StackPanel Name="spMenu" Grid.Column="0" Orientation="Horizontal" Background="#FF131212">
                <!--Roommanager-->
                <ToggleButton Name="tbtRoomManager">
                    <ToggleButton.ContentTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                <!--<Image VerticalAlignment="Center" HorizontalAlignment="Left" Width="25" Height="25" Stretch="Uniform"/>-->
                                <TextBlock VerticalAlignment="Center"  Margin="5 2 0 0"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </ToggleButton.ContentTemplate>
                </ToggleButton>
                <!--Roomdesigner-->
                <!--<ToggleButton Name="tbtRoomDesigner">
                <ToggleButton.ContentTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                            <Image VerticalAlignment="Center" HorizontalAlignment="Left" Width="25" Height="25" Stretch="Uniform"/>
                            <TextBlock VerticalAlignment="Center"  Margin="5 2 0 0">Raumdesigner</TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ToggleButton.ContentTemplate>
            </ToggleButton>-->
                <!--Settings-->
                <ToggleButton Name="tbtSettings">
                    <ToggleButton.ContentTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                <!--<Image VerticalAlignment="Center" HorizontalAlignment="Left" Width="25" Height="25" Stretch="Uniform"/>-->
                                <TextBlock VerticalAlignment="Center"  Margin="5 0 0 0"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </ToggleButton.ContentTemplate>
                </ToggleButton>
            </StackPanel>

    </Grid>
        <!--content loader: load and shows the modes-->
        <Grid Name="gdManagerContent" Grid.Row="2" Grid.ColumnSpan="2"/>
        <!--State- and eventbar-->
        <Grid Grid.Row="3" Background="#FF131212" Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <!--actual eventbar-->
            <StackPanel Grid.Column="0" Orientation="Horizontal">
                <TextBlock Margin="10 0 0 0" VerticalAlignment="Center" Foreground="White">Ereignisse:</TextBlock>
                <Image Width="25" Height="25" Margin="2 0 2 0" Stretch="Uniform"></Image>
                <TextBlock Foreground="White" MinWidth="100" VerticalAlignment="Center" Margin=" 15 0 0 0"></TextBlock>
            </StackPanel>
            <Grid Width="1" HorizontalAlignment="Right" Background="Gray" />
            <!--linkbar-->
            <StackPanel Margin="5 0 0 0" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Fortschritt :" Width="70" Foreground="Gray" VerticalAlignment="Center"/>
                    <TextBlock Width="70" Foreground="Gray" HorizontalAlignment="Right" VerticalAlignment="Center"></TextBlock>
                    <TextBlock Width="25" Foreground="Gray" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                    <TextBlock Text="s" Foreground="Gray" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                    <Grid Margin="7 0 3 0">
                        <ProgressBar  Width="100" Height="10"></ProgressBar>
                        <StackPanel Width="100" Orientation="Horizontal">
                            <TextBlock Margin="20 0 0 0" Width="25" Foreground="Black" VerticalAlignment="Center"></TextBlock>
                            <TextBlock Width="30" Text="MB/s" Foreground="Black" VerticalAlignment="Center" Margin="2 0 0 0"></TextBlock>
                        </StackPanel>

                    </Grid>
                </StackPanel>
                <Grid Width="1" HorizontalAlignment="Right" Background="Gray" />
                <TextBlock Margin="5 0 0 0" TextAlignment="Right" Width="120" Foreground="Gray" Grid.Column="1" VerticalAlignment="Center"></TextBlock>
                <Image Width="25" Height="25" Stretch="Uniform" Margin=" 10 2 5 5"/>
            </StackPanel>
        </Grid>
        <!--Console-->
        <!--<Grid Grid.Row="4">
                <RichTextBox Name="rtbConsole" BorderBrush="Orange" BorderThickness="0 1 0 0" MinHeight="100" MaxHeight="100" Background="#FF131212" Foreground="White" VerticalScrollBarVisibility="Auto">
                    <FlowDocument>
                        <Paragraph>
                            <Run Text="{Binding ConsoleText}"/>
                        </Paragraph>
                    </FlowDocument>
                </RichTextBox>
        </Grid>-->
    </Grid>
</Border>
</Window>

没有主题也是同样的行为。

【问题讨论】:

  • 您可以尝试Snoop 它以找到与您的按钮重叠的部分。
  • 我刚刚使用了 Snoop,我找不到任何覆盖按钮的东西。
  • 可以分享一下你用的Button的风格吗?默认按钮会这样显示
  • 我仍然无法重现您的问题。尚不清楚如何将 ButtonStyle 应用于 Button。它不在您的示例标记中。
  • @DonKnash 复制项目并慢慢删除代码,直到a)它可以工作并且您现在知道导致问题的原因或b)它仍然没有并且您有一个minimal reproducible example

标签: c# wpf xaml button


【解决方案1】:

好吧,假设这是您的按钮代码:

<StackPanel Orientation="Horizontal" Grid.Column="1">
    <Button>x</Button>
</StackPanel>

您具体在哪里处理按钮的点击?你应该给它一个点击事件或一些命令处理。

<StackPanel Orientation="Horizontal" Grid.Column="1">
    <Button Command="CloseTheApplicationCommand">x</Button>
</StackPanel>

如果上述方法不起作用,请检查 snoop 中确实发生了哪些事件。也许您应该使用“pre...”事件来处理按钮。

【讨论】:

  • 它们是“预览”事件,它们不是 OP 问题的解决方案。
  • 抱歉,由于评论,我刚刚删除了所有事件。在我的应用程序中有事件,我可以触发这些事件,但仅在按钮的 5 像素上。
【解决方案2】:

我刚刚发现了错误。我只是用它在没有 Windows 样式的情况下将 Windows 7 更高版本的功能添加到我的窗口中。

WindowChrome.SetWindowChrome(this, new WindowChrome());

但是因为我只是没有显示边框,所以它是透明的,挡住了按钮。 删除这个解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2011-07-14
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    相关资源
    最近更新 更多