运行代码时,wpf-Xaml行没有显示

时间:2018-01-02 05:05:19

标签: c# wpf xaml

我已经开始学习wpf了。我试图使用线元素,但每当我尝试运行此代码行时根本不可见。与矩形相同的情况,我也使用了矩形元素,但它也没有出现。问题是什么?我不明白。我能够在设计器视图中看到它,但在运行时却看不到。

<Window x:Class="Mi_Express.Product_Details"
        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:Mi_Express"
        mc:Ignorable="d"
        Title="Product_Details" Height="1920" Width="1080" WindowState="Maximized" ResizeMode="NoResize" WindowStyle="None" 
        Foreground="White" FontSize="30" FontWeight="Bold"  Topmost="True"  Background="White">
    <Grid>

        <Line HorizontalAlignment="Stretch" X1="10" Y1="10" X2="870" Y2="10" Stroke="Black" StrokeThickness="4" Margin="120,180,79,1712"/>
        <Label Content="Shopping Cart:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="157,125,0,0" FontSize="30" FontWeight="DemiBold"/>


        <Rectangle HorizontalAlignment="center" Height="73" Stroke="Black" StrokeThickness="3" VerticalAlignment="Top" Width="1017" Margin="28,1663,35,0"
             />
        <Image HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Width="139"  Margin="157,270,0,0"/>

        <Button   Content="Go Back" ClickMode="Press" Background="#FFE87E04" BorderBrush="#e87e04" FontSize="18" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Top" Width="174" Margin="157,1786,0,0" Height="61"/>
        <Button   Content="Continue" ClickMode="Press" Background="#e87e04" BorderBrush="#e87e04" FontSize="18" Foreground="white" HorizontalAlignment="Right" VerticalAlignment="Top" Width="160" Margin="0,1786,159,0" Height="61"/>


    </Grid>
</Window>

2 个答案:

答案 0 :(得分:0)

这里的问题是你的网格的结构..不要将边距应用到这里的任何UI元素..而是删除它并使用grid.column定位你的UI元素..我已经修改了你的代码一个litte

<Window x:Class="WpfApp1.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:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Line Grid.Column="0" HorizontalAlignment="Stretch" X1="10" Y1="10" X2="870" Visibility="Visible" Y2="10" Stroke="Red" StrokeThickness="4" Margin="12,18,79,171"/>
    <Label Grid.Column="1" Content="Shopping Cart:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="157,125,0,0" FontSize="30" FontWeight="DemiBold"/>


    <Rectangle Grid.Column="2"
               Visibility="Visible"
               Fill="Blue"
              HorizontalAlignment="center" Height="73" Stroke="Black" StrokeThickness="3" VerticalAlignment="Top" Width="1017" Margin="28,166,35,0"
         />
    <Image HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Width="139"  Margin="157,270,0,0"/>

    <Button   Content="Go Back" ClickMode="Press" Background="#FFE87E04" BorderBrush="#e87e04" FontSize="18" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Top" Width="174" Margin="157,1786,0,0" Height="61"/>
    <Button   Content="Continue" ClickMode="Press" Background="#e87e04" BorderBrush="#e87e04" FontSize="18" Foreground="white" HorizontalAlignment="Right" VerticalAlignment="Top" Width="160" Margin="0,1786,159,0" Height="61"/>

</Grid>

有关如何在wpf中使用网格的更多信息,请点击以下链接:

1)http://www.c-sharpcorner.com/UploadFile/1e050f/grid-layout-in-wpf/

2)http://www.wpf-tutorial.com/panels/grid-rows-and-columns/

3)https://wpftutorial.net/GridLayout.html

答案 1 :(得分:0)

问题在于边缘。底部边缘太高而且线条偏离窗口

我猜你错误的高度和宽度你的宽度=“1920”高度=“1080”。即使如此,网格的实际高度也不是1080,您可以使用此行检查

<Label Content="{Binding ElementName=grid,Path=ActualHeight}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="157,175,0,0" FontSize="30" FontWeight="DemiBold"/>