我有一个UserControl,用于WinRT应用程序中的Popup。它在设计器中看起来很棒,但是当我运行它时,布局会发生变化,UserControl的子控件会以不需要的方式调整自己的大小:A)它比我在设计时设置的垂直小,我明确设置为托管它的弹出窗口。 B)如果主要文本消息很长,它会将整个弹出窗口扩展为与文本字符串一样长,而不是将其包装以满足TextBlock的固定宽度。如果短信很短,弹出窗口似乎会转移到死点的左侧。
注意,UserControl中有三个子网格。其中只有一个在任何时候出现,所以据我所知,它们之间并不存在冲突。首次运行弹出窗口时,我会显示 gridQuestion 网格并隐藏 gridRateAndReview 和 gridSendFeedback 网格。当在 gridQuestion 网格上单击其中一个按钮时,我隐藏该网格并根据单击的按钮显示其他子网格。当我隐藏元素时,我将其设置为&# 39; s opacity 属性为 0 ,它的 IsHitTestVisible 属性为 false ,它&#39 ; 可见性属性 Collapsed 。当我展示一个元素时,我会反过来。
1)为什么子控件会在运行时自行调整大小?
2)为什么不包含主要文本消息,以及如何阻止它调整包含它的TextBlock的大小?
注意,我已经为每个容器子控件尝试了Stretch和Center的各种组合。似乎没什么用。这是用户控件的XAML:
<UserControl x:Name="userControl"
x:Class="RecNote.UserControls.RateMyApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:RecNote.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
DataContext="{Binding Main, Mode=OneWay, Source={StaticResource Locator}}" HorizontalAlignment="Center" VerticalAlignment="Center">
<Border CornerRadius="6" BorderThickness="2">
<Grid x:Name="gridOuter">
<Grid.RowDefinitions>
<RowDefinition Height="31*"/>
<RowDefinition Height="117*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="lblTitle" Grid.Row="0" HorizontalAlignment="Center" TextWrapping="Wrap" Text="Feedback Please" VerticalAlignment="Center" FontSize="24" Height="29" Width="195" FontFamily="Bookman Old Style" Foreground="#FFF5A3A3"/>
<!-- The contents first shown to the user and ask them if they're happy with the product. -->
<Grid x:Name="gridQuestion" Grid.Row="1" d:IsHidden="True">
<Grid.RowDefinitions>
<RowDefinition Height="137*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="84*"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Text="{Binding RatingMessage, ElementName=userControl}" FontSize="24" Grid.Row="0" Margin="55,25,54,25"/>
<TextBlock TextWrapping="Wrap" FontSize="24" Grid.Row="1" Text="Are you enjoying RecNote?" HorizontalAlignment="Center" VerticalAlignment="Center" Height="29" Margin="55,5,54,1" Width="287"/>
<StackPanel x:Name="stackButtons" Grid.Row="2" Orientation="Horizontal" >
<Button x:Name="btnYes" Content="YES" VerticalAlignment="Center" Width="150" Margin="35,0,30,0" Background="#FF464F41" Click="btnYes_Click" />
<Button x:Name="btnNo" Content="NO" VerticalAlignment="Center" Width="150" Background="#FF875F4D" Click="btnNo_Click"/>
</StackPanel>
</Grid>
<!-- The contents shown to the user if they say YES that prompts them to rate/review the app. -->
<Grid x:Name="gridRateAndReview" Grid.Row="1" d:IsHidden="True" >
<Grid.RowDefinitions>
<RowDefinition Height="183*"/>
<RowDefinition Height="73*"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Margin="25" FontSize="22" Text="{Binding GladYouAreEnjoyingAppMessage, ElementName=userControl}"/>
<Button x:Name="btnRateAndReview" Content="Rate & Review" VerticalAlignment="Center" Width="150" Background="#FF464F41" Grid.Row="1" HorizontalAlignment="Center" Click="btnRateAndReview_Click" />
</Grid>
<!-- The contents shown to the user if they say NO that prompts them to rate/review the app. -->
<Grid x:Name="gridSendFeedback" Grid.Row="1" d:IsHidden="True" >
<Grid.RowDefinitions>
<RowDefinition Height="183*"/>
<RowDefinition Height="73*"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Margin="25" FontSize="22" Text="{Binding SendFeedbackMessage, ElementName=userControl}"/>
<Button x:Name="btnSendFeedback" Content="Send Feedback" VerticalAlignment="Center" Width="150" Background="#FF875F4D" Grid.Row="1" HorizontalAlignment="Center" Click="btnSendFeedback_Click" />
</Grid>
</Grid>
</Border>
</UserControl>
这是在弹出窗口中显示用户控件的代码:
public static Popup ShowPopup(UserControl userControl, int width, int height)
{
if (userControl == null)
throw new ArgumentNullException("The user control is unassigned.");
if (width <= 0)
throw new ArgumentException("The width is zero or negative.");
if (height <= 0)
throw new ArgumentException("The height is zero or negative.");
// Create the popup.
Popup popup = new Popup();
// --------------- POPUP STYLING -----------------------
// Set the width and height.
popup.Width = width;
popup.Height = height;
// Center the popup on the screen.
// popup.HorizontalAlignment = HorizontalAlignment.Center;
// popup.VerticalAlignment = VerticalAlignment.Center;
// Center the popup.
popup.HorizontalOffset = (Window.Current.Bounds.Width - popup.Width) / 2;
popup.VerticalOffset = (Window.Current.Bounds.Height - popup.Height) / 2;
popup.MinWidth = width;
popup.MaxWidth = width;
popup.MinHeight = height;
popup.MaxHeight = height;
popup.IsLightDismissEnabled = true;
// Make the user control a child of the popup.
popup.Child = userControl;
// Show it.
popup.IsOpen = true;
return popup;
}
答案 0 :(得分:1)
他们重新调整大小是因为你允许它们使用*
声明提供任何可用的空间(当然减去你有固定大小的元素。d:
集合大小只是设计者。)至于缺少包装,没有父元素提供限制来调用它(除了你有一个硬集Width
的实例,我会假设它包装),这也是基于你的{{ 1}}用法。
至于何时隐藏它们,您无需单独明确设置*
,Opacity
和HitTestVisibility
。只需切换Visibility
,就可以了。
希望这有帮助。
答案 1 :(得分:0)
您可以检查是否在Window.Current.Bounds.Width或Window.Current.Bounds.Height中正确检索了该值。尝试TextWrapping =“Wholewords”