用户控件不显示其内容
Usercontrol isn't displaying its content
当我尝试使用用户控件时,我偶然发现了这个奇怪的问题,出于某种原因,当我将用户控件添加到 Window 时,它只显示我制作的边框,同时不显示其中的任何内容(无论我将其放置在边框还是网格内)
这是它在设计器中的样子:
这是它在 Window(在设计器和运行时)中的样子:
如图所示,它既不显示按钮也不显示标签。
用户控制:
<Grid Background="{x:Null}">
<Border BorderBrush="#FAF9F9" CornerRadius="20" Background="#FAF9F9">
<Border.Effect>
<DropShadowEffect Opacity="0.3" ShadowDepth="2" Direction="270"/>
</Border.Effect>
<Grid Background="{x:Null}">
<Label Content="Label" HorizontalAlignment="Left" VerticalContentAlignment="Center" Margin="10,280,0,0" VerticalAlignment="Top" Height="47" Width="428" FontSize="25"/>
<Button Content="+" HorizontalAlignment="Left" Margin="443,280,0,0" VerticalAlignment="Top" Width="47" Height="47" HorizontalContentAlignment="Center" Foreground="White" FontSize="40" FontWeight="Bold" RenderTransformOrigin="0.5,0.5" BorderBrush="{x:Null}">
<Button.Effect>
<DropShadowEffect Opacity="0.3" ShadowDepth="2" Direction="270"/>
</Button.Effect>
<Button.Template >
<ControlTemplate TargetType="Button" >
<Grid >
<Path Stretch="Uniform" UseLayoutRounding="False" Fill="#FFEA1E27">
<Path.Data>
<EllipseGeometry RadiusX="1" RadiusY="1"/>
</Path.Data>
</Path>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Border>
</Grid>
看起来您的控件的实际大小小于设计大小。因此,标签和按钮在控件的可见部分之外。
尝试将 Label
和 Button
相对于右下角和左下角而不是左上角定位。
<Label Content="Label" HorizontalAlignment="Left" Margin="10" VerticalContentAlignment="Center" VerticalAlignment="Bottom" Height="47" Width="428" FontSize="25"/>
<Button Content="+" HorizontalAlignment="Right" Margin="10" VerticalAlignment="Bottom" Width="47" Height="47" HorizontalContentAlignment="Center" Foreground="White" FontSize="40" FontWeight="Bold" RenderTransformOrigin="0.5,0.5" BorderBrush="{x:Null}">
当我尝试使用用户控件时,我偶然发现了这个奇怪的问题,出于某种原因,当我将用户控件添加到 Window 时,它只显示我制作的边框,同时不显示其中的任何内容(无论我将其放置在边框还是网格内)
这是它在设计器中的样子:
这是它在 Window(在设计器和运行时)中的样子:
如图所示,它既不显示按钮也不显示标签。
用户控制:
<Grid Background="{x:Null}">
<Border BorderBrush="#FAF9F9" CornerRadius="20" Background="#FAF9F9">
<Border.Effect>
<DropShadowEffect Opacity="0.3" ShadowDepth="2" Direction="270"/>
</Border.Effect>
<Grid Background="{x:Null}">
<Label Content="Label" HorizontalAlignment="Left" VerticalContentAlignment="Center" Margin="10,280,0,0" VerticalAlignment="Top" Height="47" Width="428" FontSize="25"/>
<Button Content="+" HorizontalAlignment="Left" Margin="443,280,0,0" VerticalAlignment="Top" Width="47" Height="47" HorizontalContentAlignment="Center" Foreground="White" FontSize="40" FontWeight="Bold" RenderTransformOrigin="0.5,0.5" BorderBrush="{x:Null}">
<Button.Effect>
<DropShadowEffect Opacity="0.3" ShadowDepth="2" Direction="270"/>
</Button.Effect>
<Button.Template >
<ControlTemplate TargetType="Button" >
<Grid >
<Path Stretch="Uniform" UseLayoutRounding="False" Fill="#FFEA1E27">
<Path.Data>
<EllipseGeometry RadiusX="1" RadiusY="1"/>
</Path.Data>
</Path>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Border>
</Grid>
看起来您的控件的实际大小小于设计大小。因此,标签和按钮在控件的可见部分之外。
尝试将 Label
和 Button
相对于右下角和左下角而不是左上角定位。
<Label Content="Label" HorizontalAlignment="Left" Margin="10" VerticalContentAlignment="Center" VerticalAlignment="Bottom" Height="47" Width="428" FontSize="25"/>
<Button Content="+" HorizontalAlignment="Right" Margin="10" VerticalAlignment="Bottom" Width="47" Height="47" HorizontalContentAlignment="Center" Foreground="White" FontSize="40" FontWeight="Bold" RenderTransformOrigin="0.5,0.5" BorderBrush="{x:Null}">