WP7用户控件-更改方向时更改控件的宽度

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-