圆形文本框wpf项目中的奇怪行为

时间:2015-07-11 01:12:23

标签: c# .net wpf

我在Visual Studio 2013中的WPF项目中有此页面

但是当我执行它时,它会显示:

如果我最大化它,它会显示:

enter image description here

这是XAML页面代码:

<Page x:Class="ControlDomotico.Client.MenuPrincipal"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d"
Title="MenuPrincipal" d:DesignHeight="633.654" d:DesignWidth="877.835">

<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="d:\Documents\Visual Studio 2013\Projects\ControlDomotico\ControlDomotico.Client\images\menu.jpg">
            <ImageBrush.Transform>
                <TransformGroup>
                    <ScaleTransform ScaleY="1.05"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform X="-11"/>
                </TransformGroup>
            </ImageBrush.Transform>
        </ImageBrush>
    </Grid.Background>
    <Label Content="Bienvenido a tu casa" FontSize="30px" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="10.453,3.723" Height="53" Width="309" Margin="10,10,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="#00000000" Foreground="#FF0F0101" FontWeight="Bold"/>

    <Button Name="claveBtn" Width="100" Content="Cambiar clave"
    Height="100" Margin="439,40,339,493.6" >
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Ellipse Stroke="Black"
                     StrokeThickness="2">
                        <Ellipse.Fill>
                            <RadialGradientBrush>
                                <GradientStop Offset="0"
                                      Color="Gold" />
                                <GradientStop Offset="1"
                                      Color="Gold" />
                                <GradientStop Offset="1"
                                      Color="Gold" />
                                <RadialGradientBrush.Transform>
                                    <TransformGroup>
                                        <ScaleTransform ScaleY="0.65" />
                                    </TransformGroup>
                                </RadialGradientBrush.Transform>
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <ContentPresenter HorizontalAlignment="Center"
                              VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <Button Name="alarmaBtn" Width="100" Content="Configurar alarma"
        Height="100" Margin="588,95,190,438.6">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Ellipse Stroke="Black"
                        StrokeThickness="2">
                        <Ellipse.Fill>
                            <RadialGradientBrush>
                                <RadialGradientBrush.Transform>
                                    <TransformGroup>
                                        <ScaleTransform ScaleY="0.65" />
                                    </TransformGroup>
                                </RadialGradientBrush.Transform>
                                <GradientStop Offset="0"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <ContentPresenter HorizontalAlignment="Center"
                        VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <Button Name="horaBtn" Width="100" Content="Cambiar hora"
        Height="100" Margin="732,198,46,335.6" Click="horaBtn_Click">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Ellipse Stroke="Black"
                        StrokeThickness="2">
                        <Ellipse.Fill>
                            <RadialGradientBrush>
                                <RadialGradientBrush.Transform>
                                    <TransformGroup>
                                        <ScaleTransform ScaleY="0.65" />
                                    </TransformGroup>
                                </RadialGradientBrush.Transform>
                                <GradientStop Offset="0"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <ContentPresenter HorizontalAlignment="Center"
                        VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <Button Name="puertaBtn" Width="100" Content="Puerta"
        Height="100" Margin="607,308,171,225.6">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Ellipse Stroke="Black"
                        StrokeThickness="2">
                        <Ellipse.Fill>
                            <RadialGradientBrush>
                                <RadialGradientBrush.Transform>
                                    <TransformGroup>
                                        <ScaleTransform ScaleY="0.65" />
                                    </TransformGroup>
                                </RadialGradientBrush.Transform>
                                <GradientStop Offset="0"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <ContentPresenter HorizontalAlignment="Center"
                        VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <Button Name="iluminacionBtn" Width="100" Content="Iluminación"
        Height="100" Margin="588,463,190,70.6">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Ellipse Stroke="Black"
                        StrokeThickness="2">
                        <Ellipse.Fill>
                            <RadialGradientBrush>
                                <RadialGradientBrush.Transform>
                                    <TransformGroup>
                                        <ScaleTransform ScaleY="0.65" />
                                    </TransformGroup>
                                </RadialGradientBrush.Transform>
                                <GradientStop Offset="0"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <ContentPresenter HorizontalAlignment="Center"
                        VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <Button Name="baneraBtn" Width="100" Content="Bañera"
        Height="100" Margin="439,508,339,25.6">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Ellipse Stroke="Black"
                        StrokeThickness="2">
                        <Ellipse.Fill>
                            <RadialGradientBrush>
                                <RadialGradientBrush.Transform>
                                    <TransformGroup>
                                        <ScaleTransform ScaleY="0.65" />
                                    </TransformGroup>
                                </RadialGradientBrush.Transform>
                                <GradientStop Offset="0"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                                <GradientStop Offset="1"
                                    Color="Gold" />
                            </RadialGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <ContentPresenter HorizontalAlignment="Center"
                        VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>

我不明白这种行为,可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

由于某种原因,窗口的大小与您在Buttons上设置的边距冲突。我建议您使用RowDefinitions&amp ;;在XAML编辑器中设计代码。 Column Definitions上的Grid而不是XAML设计器中的Window/Page。但是,您的解决方案的快速解决方案是:

a:将833 x 933尺寸增加到ViewBox或类似的东西。

b:将您的代码包含在<Viewbox> <Grid Background="Red"> <Label Content="Bienvenido a tu casa" FontSize="30px" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="10.453,3.723" Height="53" Width="309" Margin="10,10,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="#00000000" Foreground="#FF0F0101" FontWeight="Bold"/> <Button Name="claveBtn" Width="100" Content="Cambiar clave" Height="100" Margin="664,445.5,106,257.5" > <Button.Template> <ControlTemplate TargetType="Button"> <Grid> <Ellipse Stroke="Black" StrokeThickness="2"> <Ellipse.Fill> <RadialGradientBrush> <GradientStop Offset="0" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> <RadialGradientBrush.Transform> <TransformGroup> <ScaleTransform ScaleY="0.65" /> </TransformGroup> </RadialGradientBrush.Transform> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Button.Template> </Button> <Button Name="alarmaBtn" ClipToBounds="True" Content="Configurar alarma" Width="100" Height="100" Click="alarmaBtn_Click" Margin="581,103.55,189,599.45"> <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Ellipse Stroke="Black" StrokeThickness="2"> <Ellipse.Fill> <RadialGradientBrush> <RadialGradientBrush.Transform> <TransformGroup> <ScaleTransform ScaleY="0.65" /> </TransformGroup> </RadialGradientBrush.Transform> <GradientStop Offset="0" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Button.Template> </Button> <Button Name="horaBtn" Width="100" Content="Cambiar hora" Height="100" Margin="485,597.5,285,105.5" > <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Ellipse Stroke="Black" StrokeThickness="2"> <Ellipse.Fill> <RadialGradientBrush> <RadialGradientBrush.Transform> <TransformGroup> <ScaleTransform ScaleY="0.65" /> </TransformGroup> </RadialGradientBrush.Transform> <GradientStop Offset="0" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Button.Template> </Button> <Button Name="puertaBtn" Width="100" Content="Puerta" Height="100" Margin="689,236.5,81,466.5" > <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Ellipse Stroke="Black" StrokeThickness="2"> <Ellipse.Fill> <RadialGradientBrush> <RadialGradientBrush.Transform> <TransformGroup> <ScaleTransform ScaleY="0.65" /> </TransformGroup> </RadialGradientBrush.Transform> <GradientStop Offset="0" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Button.Template> </Button> <Button Name="iluminacionBtn" Width="100" Content="Iluminación" Height="100" Margin="385,58.5,385,644.5" > <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Ellipse Stroke="Black" StrokeThickness="2"> <Ellipse.Fill> <RadialGradientBrush> <RadialGradientBrush.Transform> <TransformGroup> <ScaleTransform ScaleY="0.65" /> </TransformGroup> </RadialGradientBrush.Transform> <GradientStop Offset="0" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Button.Template> </Button> <Button Name="baneraBtn" Width="100" Content="Bañera" Height="100" Margin="204,629.5,566,73.5" > <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Ellipse Stroke="Black" StrokeThickness="2"> <Ellipse.Fill> <RadialGradientBrush> <RadialGradientBrush.Transform> <TransformGroup> <ScaleTransform ScaleY="0.65" /> </TransformGroup> </RadialGradientBrush.Transform> <GradientStop Offset="0" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> <GradientStop Offset="1" Color="Gold" /> </RadialGradientBrush> </Ellipse.Fill> </Ellipse> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Button.Template> </Button> </Grid> </Viewbox> 中,如下所示:

public class getDbData {

private final String JDBC_DRIVER = "com.mysql.jdbc.Driver";   
private final String DB_URL = "jdbc:mysql://localhost:3306/test";   
private final String USER = "root";   
private final String PASS = "";   
    Connection conn;   
    Statement stmt;   
    ResultSet rs;   

public getDbData() throws SQLException, ClassNotFoundException {
   Class.forName("com.mysql.jdbc.Driver");   
    conn = DriverManager.getConnection(DB_URL, USER, PASS);   
    stmt = conn.createStatement();   
}

public String[] getavailableMeasureMU() throws SQLException {   
    String sql;   
    sql = "Select * from availablemeasures where incentive='"+"mu"+"'";   
    rs = stmt.executeQuery(sql);    
    String x = null;    
    Vector data = new Vector();    
    while (rs.next()) {    

        data.add(rs.getString("measure"));    
    }    
    for (Object data1 : data) {    
        System.out.println(data1);    
    }    

    return null;    

}

public static void main(String[] args){    
    System.out.println("called");     

    getDbData call = null;    
    try {    
        call = new getDbData();    
    } catch (SQLException ex) {     
        Logger.getLogger(getDbData.class.getName()).log(Level.SEVERE, null, ex);     
    } catch (ClassNotFoundException ex) {     
        Logger.getLogger(getDbData.class.getName()).log(Level.SEVERE, null, ex);    
    }    

    try {    
        call.getavailableMeasureMU();     
    } catch (Exception e) {     
        System.out.println(e);     
    }     
}
}

答案 1 :(得分:0)

您应该将面板划分为假想的网格区域,而不是使用边距和固定大小的按钮,如下图所示:

enter image description here

然后例如&#34; Cambiar clave&#34; 位于Grid.Row = 0 Grid.Column = 1&#34; Puerta&#34; it&# 39; s Grid.Row = 3 Grid.Column = 2等等。这可以这样做:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <Grid.Background>
        <ImageBrush ImageSource="d:\Documents\Visual Studio 2013\Projects\ControlDomotico\ControlDomotico.Client\images\menu.jpg">
            <ImageBrush.Transform>
                <TransformGroup>
                    <ScaleTransform ScaleY="1.05"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform X="-11"/>
                </TransformGroup>
            </ImageBrush.Transform>
        </ImageBrush>
    </Grid.Background>
    <Label Content="Bienvenido a tu casa" FontSize="30px" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="10.453,3.723" Height="53" Width="309" Margin="10,10,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="#00000000" Foreground="#FF0F0101" FontWeight="Bold"/>
    <Button Name="claveBtn"  Content="Cambiar clave"
     Grid.Row="0" Grid.Column="1" Template="{StaticResource RoundButton}"/>
    <Button Name="alarmaBtn"  Content="Configurar alarma"
     Grid.Row="1" Grid.Column="2" Template="{StaticResource RoundButton}"/>
    <Button Name="horaBtn" Content="Cambiar hora"
     Grid.Row="2" Grid.Column="3" Template="{StaticResource RoundButton}" />
    <Button Name="puertaBtn" Content="Puerta"
     Grid.Row="3" Grid.Column="2" Template="{StaticResource RoundButton}"/>
    <Button Name="iluminacionBtn"  Content="Iluminación"
     Grid.Row="4" Grid.Column="2" Template="{StaticResource RoundButton}"/>
    <Button Name="baneraBtn" Content="Bañera"
     Grid.Row="5" Grid.Column="1" Template="{StaticResource RoundButton}"/>
</Grid>

您还应该在资源部分定义ControlTemplate(我称之为RoundButton)以避免代码重复:

<Page.Resources>
    <ControlTemplate x:Key="RoundButton" TargetType="Button">
        <Grid>
            <Ellipse Stroke="Black"
                 StrokeThickness="2">
                <Ellipse.Fill>
                    <RadialGradientBrush>
                        <GradientStop Offset="0"
                                  Color="Gold" />
                        <GradientStop Offset="1"
                                  Color="Gold" />
                        <GradientStop Offset="1"
                                  Color="Gold" />
                        <RadialGradientBrush.Transform>
                            <TransformGroup>
                                <ScaleTransform ScaleY="0.65" />
                            </TransformGroup>
                        </RadialGradientBrush.Transform>
                    </RadialGradientBrush>
                </Ellipse.Fill>
            </Ellipse>
            <ContentPresenter HorizontalAlignment="Center"
                          VerticalAlignment="Center"/>
        </Grid>
    </ControlTemplate>
</Page.Resources>