WPF中的按钮可见性

时间:2017-10-22 09:57:45

标签: wpf xaml

我开始写一个我昨天想过的游戏,但我是xaml的新手。 我有以下代码 -

<Window x:Class="TapFrenzy.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:TapFrenzy"
        mc:Ignorable="d"
        Title="MainWindow" Height="486" Width="600">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="FontSize" Value="100"/>
            <Setter Property="FontWeight" Value="Black"/>
            <Setter Property="Foreground" Value="White"/>
        </Style>

    </Window.Resources>

    <Grid x:Name="Map">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="90"/>
            <ColumnDefinition Width="90"/>
            <ColumnDefinition Width="90"/>
            <ColumnDefinition Width="90"/>
            <ColumnDefinition Width="90"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="90"/>
            <RowDefinition Height="90"/>
            <RowDefinition Height="90"/>
            <RowDefinition Height="90"/>
            <RowDefinition Height="90"/>
        </Grid.RowDefinitions>

        <Button Click="ButtonClick" Background="White" x:Name="Button0_0" Grid.Column="0" Grid.Row="0"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button0_1" Grid.Column="0" Grid.Row="1"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button0_2" Grid.Column="0" Grid.Row="2"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button0_3" Grid.Column="0" Grid.Row="3"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button0_4" Grid.Column="0" Grid.Row="4"/>

        <Button Click="ButtonClick" Background="White" x:Name="Button1_0" Grid.Column="1" Grid.Row="0"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button1_1" Grid.Column="1" Grid.Row="1"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button1_2" Grid.Column="1" Grid.Row="2"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button1_3" Grid.Column="1" Grid.Row="3"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button1_4" Grid.Column="1" Grid.Row="4"/>

        <Button Click="ButtonClick" Background="White" x:Name="Button2_0" Grid.Column="2" Grid.Row="0"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button2_1" Grid.Column="2" Grid.Row="1"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button2_2" Grid.Column="2" Grid.Row="2"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button2_3" Grid.Column="2" Grid.Row="3"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button2_4" Grid.Column="2" Grid.Row="4"/>

        <Button Click="ButtonClick" Background="White" x:Name="Button3_0" Grid.Column="3" Grid.Row="0"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button3_1" Grid.Column="3" Grid.Row="1"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button3_2" Grid.Column="3" Grid.Row="2"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button3_3" Grid.Column="3" Grid.Row="3"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button3_4" Grid.Column="3" Grid.Row="4"/>

        <Button Click="ButtonClick" Background="White" x:Name="Button4_0" Grid.Column="4" Grid.Row="0"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button4_1" Grid.Column="4" Grid.Row="1"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button4_2" Grid.Column="4" Grid.Row="2"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button4_3" Grid.Column="4" Grid.Row="3"/>
        <Button Click="ButtonClick" Background="White" x:Name="Button4_4" Grid.Column="4" Grid.Row="4"/>

        <Canvas Margin="91,0,-142,0" Grid.Column="4" Grid.RowSpan="5">
            <Rectangle
                Width="140"
                Height="455"
                Fill="Gray"/>

            <Button Content="New Game" Click="ClickNewGame" x:Name="_NewGame" Height="50" Width="120"  Canvas.Left="410" Canvas.Top="-12" IsDefault="True" FontSize="14" FontStyle="Italic" FontFamily="DejaVu Sans"/>

            <Button Content="Pause Game" Click="ClickPauseGame" x:Name="_PauseGame" Height="50" Width="120"  Canvas.Left="410" Canvas.Top="81" IsDefault="True" FontSize="14" FontStyle="Italic" FontFamily="DejaVu Sans"/>

            <Label Content="Timer" x:Name="Timer" Height="50" Width="120"  Canvas.Left="410" Canvas.Top="262" FontSize="14" FontStyle="Italic" FontFamily="DejaVu Sans"/>

            <Label Content="Score" x:Name="Score" Height="50" Width="120"  Canvas.Left="410" Canvas.Top="175" FontSize="14" FontStyle="Italic" FontFamily="DejaVu Sans"/>

        </Canvas>


    </Grid>
</Window>

` - 但是,我的新游戏和暂停游戏按钮以及2个标签都不会出现。在我开始调整大小和适应所有东西之前,他们就在那里,现在他们无处可寻。我尝试改变他们的z-index,但它没有用。 任何想法都将受到高度赞赏。 此致

1 个答案:

答案 0 :(得分:0)

显然你的画面不需要画布。

无论如何,我纠正了以下问题:

一个。不可能(以简单的方式)用黑色正方形填充所有画布边界(不是用于此目的的canavas)。

我的解决方案是将它放在面板之外但在同一个地方

B中。按钮及其边缘的尺寸(Canvas.Left等)不在可见窗口内。

<Rectangle Grid.Column="4" Grid.RowSpan="5" Fill="Gray" />
<Canvas Grid.Column="4" Grid.RowSpan="5">

    <Button Content="New Game" Click="ClickNewGame" x:Name="_NewGame" Height="50" Width="70"  Canvas.Left="10" Canvas.Top="10" IsDefault="True" FontSize="14" FontStyle="Italic" FontFamily="DejaVu Sans"/>

    <Button Content="Pause Game" Click="ClickPauseGame" x:Name="_PauseGame" Height="50" Width="70"  Canvas.Left="10" Canvas.Top="81" IsDefault="True" FontSize="14" FontStyle="Italic" FontFamily="DejaVu Sans"/>

    <Label Content="Timer" x:Name="Timer" Height="50" Width="70"  Canvas.Left="10" Canvas.Top="262" FontSize="14" FontStyle="Italic" FontFamily="DejaVu Sans"/>

    <Label Content="Score" x:Name="Score" Height="50" Width="70"  Canvas.Left="10" Canvas.Top="175" FontSize="14" FontStyle="Italic" FontFamily="DejaVu Sans"/>

</Canvas>

对于初学者来说这很棒,但同样,这里的canava不是正确的。

我也有关于你设置整个网格的漂亮(但是很难的工作......)的评论......

这里有一些评论的快速示例:

<Grid x:Name="Map">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="486" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <UniformGrid Rows="5" Columns="5">
        <UniformGrid.Resources>
            <Style TargetType="Button">
                <Setter Property="FontSize" Value="100"/>
                <Setter Property="FontWeight" Value="Black"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Background" Value="White"/>
            </Style>
        </UniformGrid.Resources>

        <Button Click="ButtonClick" x:Name="Button0_0" />
        <Button Click="ButtonClick" x:Name="Button0_1" />
        <Button Click="ButtonClick" x:Name="Button0_2" />
        <Button Click="ButtonClick" x:Name="Button0_3" />
        <Button Click="ButtonClick" x:Name="Button0_4" />

        <Button Click="ButtonClick" x:Name="Button1_0" />
        <Button Click="ButtonClick" x:Name="Button1_1" />
        <Button Click="ButtonClick" x:Name="Button1_2" />
        <Button Click="ButtonClick" x:Name="Button1_3" />
        <Button Click="ButtonClick" x:Name="Button1_4" />

        <Button Click="ButtonClick" x:Name="Button2_0" />
        <Button Click="ButtonClick" x:Name="Button2_1" />
        <Button Click="ButtonClick" x:Name="Button2_2" />
        <Button Click="ButtonClick" x:Name="Button2_3" />
        <Button Click="ButtonClick" x:Name="Button2_4" />

        <Button Click="ButtonClick" x:Name="Button3_0" />
        <Button Click="ButtonClick" x:Name="Button3_1" />
        <Button Click="ButtonClick" x:Name="Button3_2" />
        <Button Click="ButtonClick" x:Name="Button3_3" />
        <Button Click="ButtonClick" x:Name="Button3_4" />

        <Button Click="ButtonClick" x:Name="Button4_0" />
        <Button Click="ButtonClick" x:Name="Button4_1" />
        <Button Click="ButtonClick" x:Name="Button4_2" />
        <Button Click="ButtonClick" x:Name="Button4_3" />
        <Button Click="ButtonClick" x:Name="Button4_4" />

    </UniformGrid>

    <Grid Background="Gray" Grid.Column="4" Grid.RowSpan="5" TextElement.FontStyle="Italic" TextElement.FontFamily="DejaVu Sans" TextElement.FontSize="14">
        <Button Content="New Game" Click="ClickNewGame" x:Name="_NewGame" Padding="6" Margin="0,20"  IsDefault="True" VerticalAlignment="Top" HorizontalAlignment="Center"/>
        <Button Content="Pause Game" Click="ClickPauseGame" x:Name="_PauseGame"  Padding="6" Margin="0,85" VerticalAlignment="Top" HorizontalAlignment="Center" />
        <Label Content="Timer" x:Name="Timer"  Margin="0,85" VerticalAlignment="Bottom" HorizontalAlignment="Center"  />
        <Label Content="Score" x:Name="Score"  Margin="0,20" VerticalAlignment="Bottom"  HorizontalAlignment="Center"/>
    </Grid>

</Grid>

我建议您了解WPF面板。

http://www.wpf-tutorial.com/panels/introduction-to-wpf-panels/

https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/panels-overview

所有按钮都可以更容易制作(例如ItemsSource),WPF是专为懒惰设计的:)

但条件是他们对材料和知识进行了大量的研究。