所以我正在创建一个小的wpf应用程序以获得乐趣,我希望它有一个菜单。我创建了菜单,但没有,但是当我运行程序时,菜单根本没有显示。菜单的代码目前是:
<Window x:Class="TBA_Alpha.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TBA LogIn" Height="350" Width="525">
<Grid>
<PasswordBox x:Name="tbPsw" HorizontalAlignment="Left" Margin="214,143,0,0" VerticalAlignment="Top" Width="133" SelectionBrush="Black" Background="White">
<PasswordBox.BorderBrush>
<LinearGradientBrush EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0">
<GradientStop Color="#FFABADB3" Offset="0.05"/>
<GradientStop Color="#FFE2E3EA" Offset="0.07"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</PasswordBox.BorderBrush>
</PasswordBox>
<TextBox x:Name="tbUId" HorizontalAlignment="Left" Height="23" Margin="214,94,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="133" SelectionBrush="Black">
<TextBox.Background>
<SolidColorBrush Color="White"/>
</TextBox.Background>
<TextBox.BorderBrush>
<LinearGradientBrush EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0">
<GradientStop Color="#FFABADB3" Offset="0.05"/>
<GradientStop Color="#FFE2E3EA" Offset="0.07"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</TextBox.BorderBrush>
</TextBox>
<Button Content="LogIn" HorizontalAlignment="Left" Margin="239,200,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBlock HorizontalAlignment="Left" Margin="214,73,0,0" TextWrapping="Wrap" Text="Your UserID:" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" Margin="214,122,0,0" TextWrapping="Wrap" Text="Your Password:" VerticalAlignment="Top"/>
<Menu HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width="43" BorderThickness="1">
<MenuItem Header=" New " RenderTransformOrigin="0.5,0.5" Width="63" Height="36" Background="White" Visibility="Visible" AllowDrop="True" IsHitTestVisible="True">
<MenuItem Header=" User " Click="newUser"/>
</MenuItem>
</Menu>
</Grid>
任何帮助都会在很大程度上受到感谢。
编辑:为wpf添加了整个XAML代码,其中菜单靠近代码的末尾
编辑:删除所有其他内容并仅留下标题后,仍然遇到同样的问题
编辑:在不同的干净项目中尝试了代码并且它有效,谢谢你
答案 0 :(得分:2)
问题是,您的大多数控件都使用Magrin
属性来分隔自己,而不是Grid
的功能。由于它们都没有指定附加属性Grid.Row
,因此它们都在同一行中。由于Menu
未指定Margin
,因此它位于Grid
的顶部,因此与其他控件重叠。
尝试使用实际网格行
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<PasswordBox x:Name="tbPsw" HorizontalAlignment="Left" Grid.Row="0" VerticalAlignment="Top" Width="133" SelectionBrush="Black" Background="White">
<PasswordBox.BorderBrush>
<LinearGradientBrush EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0">
<GradientStop Color="#FFABADB3" Offset="0.05"/>
<GradientStop Color="#FFE2E3EA" Offset="0.07"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</PasswordBox.BorderBrush>
</PasswordBox>
<TextBox x:Name="tbUId" HorizontalAlignment="Left" Height="23" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="133" SelectionBrush="Black">
<TextBox.Background>
<SolidColorBrush Color="White"/>
</TextBox.Background>
<TextBox.BorderBrush>
<LinearGradientBrush EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0">
<GradientStop Color="#FFABADB3" Offset="0.05"/>
<GradientStop Color="#FFE2E3EA" Offset="0.07"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</TextBox.BorderBrush>
</TextBox>
<Button Content="LogIn" HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBlock HorizontalAlignment="Left" Grid.Row="3" TextWrapping="Wrap" Text="Your UserID:" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" Grid.Row="4" TextWrapping="Wrap" Text="Your Password:" VerticalAlignment="Top"/>
<Menu Grid.Row="5" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width="43" BorderThickness="1">
<MenuItem Header=" New " RenderTransformOrigin="0.5,0.5" Width="63" Height="36" Background="White" Visibility="Visible" AllowDrop="True" IsHitTestVisible="True">
<MenuItem Header=" User " Click="newUser"/>
</MenuItem>
</Menu>
</Grid>