如何解决WP7用户控件-更改方向时更改控件的宽度
| 在我的一个应用程序中,我有一个名为FeedControl的用户控件(是的,它是另一个RSS阅读器!),它非常简单-只需一个复选框即可选择项目,一个文本框显示提要名称,另一个文本框显示未读的提要项数。在启动时将提要加载到一个可观察的集合中时,我然后为每个提要创建一个控件实例,并将它们全部添加到列表框中-确实非常简单。 XAML代码如下:<UserControl x:Class=\"MyReader.FeedControl\"
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\"
mc:Ignorable=\"d\"
FontFamily=\"{StaticResource PhoneFontFamilyNormal}\"
FontSize=\"{StaticResource PhoneFontSizeNormal}\"
Foreground=\"{StaticResource PhoneForegroundBrush}\"
Loaded=\"UserControl_Loaded\" >
<Grid x:Name=\"LayoutRoot\" Height=\"50\" Background=\"Black\" HorizontalAlignment=\"Left\" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width=\"70\"/>
<ColumnDefinition Width=\"*\"/>
<ColumnDefinition Width=\"70\"/>
</Grid.ColumnDefinitions>
<CheckBox Name=\"Select\" Grid.Column=\"0\" IsChecked=\"{Binding IsSelected}\" Margin=\"-5,-16\" Checked=\"Select_Checked\" Background=\"White\"/>
<TextBlock Name=\"FeedName\" Grid.Column=\"1\" Text=\"{Binding FeedName}\" Opacity=\"1\" Margin=\"-20\" VerticalAlignment=\"Center\" FontSize=\"24\"
MouseLeftButtonUp=\"FeedName_MouseLeftButtonUp\" Height=\"32\" Width=\"360\"/>
<TextBlock Name=\"UnreadItems\" Grid.Column=\"2\" Text=\"{Binding UnreadItems}\" Opacity=\"1\" Padding=\"2\" VerticalAlignment=\"Center\" HorizontalAlignment=\"Right\"
FontSize=\"24\" MouseLeftButtonUp=\"FeedName_MouseLeftButtonUp\" />
</Grid>
当页面方向更改时,Feedcontrol项的列表框处于打开状态,我希望将Feed名称文本块更改为正确的大小(横向560px,纵向360px),因此我假设将网格列的宽度设置为\“ * \”或\“ Auto \”并没有为文本块设置宽度,它将自动调整大小,但仅将其大小调整为文本的宽度,而不是整个宽度。
因此,然后我尝试在XAML中将文本块设置为默认的纵向尺寸(360),并在页面方向更改事件中,在当前位于ListBox中的FeedControl的每个实例中调用以下方法:
public void ChangeOrientation(bool isLandscape)
{
if (isLandscape)
{
this.LayoutRoot.Width = App.LandscapeWidth; //700
this.FeedName.Width = App.LandscapeItemWidth; //560
}
else
{
this.LayoutRoot.Width = App.PortraitWidth; //480
this.FeedName.Width = App.PortraitItemWidth; //360
}
this.InvalidateArrange();
this.InvalidateMeasure();
}
但是,这也不起作用(无论我如何尝试),所以我对如何最好地做到这一点感到困惑。我知道必须有一种非常简单的方法,但是到目前为止,我还没有找到它。
有人能指出我正确的方向吗?
麦克风
附言:对于帖子的长度感到抱歉...这么简单的问题这么久!!
解决方法
您应该将拥有控制权的ListBoxItem的
HorizontalAlignment
和ѭ3set设置为HorizontalAlignment
.Stretch
。另外,设置控件的设计宽度/设计高度。
这是我在一个列表框中拥有的一个控件的示例,该控件在横向模式下延伸。
<UserControl x:Class=\"SabWatch.Resources.Controls.ProgressBox\"
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\"
mc:Ignorable=\"d\"
FontFamily=\"{StaticResource PhoneFontFamilyNormal}\"
FontSize=\"{StaticResource PhoneFontSizeNormal}\"
Foreground=\"{StaticResource PhoneForegroundBrush}\"
d:DesignHeight=\"480\" d:DesignWidth=\"480\">
<Grid x:Name=\"LayoutRoot\" Style=\"{StaticResource AppBackgroundStyle}\">
<Grid.RowDefinitions>
<RowDefinition Height=\"Auto\"/>
<RowDefinition Height=\"Auto\"/>
<RowDefinition Height=\"Auto\"/>
<RowDefinition Height=\"Auto\"/>
</Grid.RowDefinitions>
</Grid>
</UserControl>
这是在代码中设置对齐属性的示例。您也可以在XAML
中进行此操作(并可能使用样式并为ListBox
赋予ItemContainerStyle
)
var lbi = new ListBoxItem();
var box = new ProgressBox();
...
lbi.Tag = queueObject;
lbi.Content = box;
lbi.HorizontalAlignment = HorizontalAlignment.Stretch;
lbi.HorizontalContentAlignment = HorizontalAlignment.Stretch;
,在您放置此控件的位置,没有提到您。我认为您正在将此控件放置在“电话”页面中;就像放置其他控件一样。如果这样更好,您可以在该页面(容器页面)中管理与方向相关的内容,而不是在控件中进行调整。我认为这将解决您的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。