Windows Phone 7 中的网格

2023-12-12

我有一个网格视图代码,下面有分为3列。 但我的代码有一个问题。 什么时候多重数据被检索到。 第 3 列中的所有数据都是重叠的。我如何修改下面的代码,例如它会在下面依次显示。

           //Define grid column, size

            Grid schedule = new Grid();

            foreach (var time in timeSplit)
            {
                timeList = time;
                //Column 1 to hold the time of the schedule
                ColumnDefinition scheduleTimeColumn = new ColumnDefinition();
                GridLength timeGrid = new GridLength(110);
                scheduleTimeColumn.Width = timeGrid;
                schedule.ColumnDefinitions.Add(scheduleTimeColumn);

                //Text block that show the time of the schedule
                TextBlock timeTxtBlock = new TextBlock();
                timeTxtBlock.Text = time;
                //Set the alarm label text block properties - margin, fontsize
                timeTxtBlock.FontSize = 28;
                timeTxtBlock.Margin = new Thickness(0, 20, 0, 0);
                //Set the column that will hold the time of the schedule
                Grid.SetColumn(timeTxtBlock, 0);

                schedule.Children.Add(timeTxtBlock);
            }

            foreach (var title in titleSplit)
            {
                titleList = title;

                //Column 2 to hold the title of the schedule
                ColumnDefinition scheduleTitleColumn = new ColumnDefinition();
                GridLength titleGrid = new GridLength(500);
                scheduleTitleColumn.Width = titleGrid;
                schedule.ColumnDefinitions.Add(scheduleTitleColumn);

                //Text block that show the title of the schedule
                TextBlock titleTxtBlock = new TextBlock();

                if (title.Length > 10)
                {
                    string strTitle = title.Substring(0, 10) + "....";
                    titleTxtBlock.Text = strTitle;
                }
                else
                {
                    titleTxtBlock.Text = title;
                }

                //Set the alarm label text block properties - margin, fontsize
                titleTxtBlock.FontSize = 28;
                titleTxtBlock.Margin = new Thickness(60, 20, 0, 0);
                //Set the column that will hold the title of the schedule
                Grid.SetColumn(titleTxtBlock, 1);

                schedule.Children.Add(titleTxtBlock);
                //scheduleListBox.Items.Add(schedule);
            }

            foreach (var category in categorySplit)
            {
                categoryList = category;

                //Column 3 to hold the image category of the schedule
                ColumnDefinition categoryImageColumn = new ColumnDefinition();
                GridLength catImgnGrid = new GridLength(70);
                categoryImageColumn.Width = catImgnGrid;
                schedule.ColumnDefinitions.Add(categoryImageColumn);

                TextBlock categoryTxtBlock = new TextBlock();
                categoryTxtBlock.Text = category;

                //set the category image and its properties - margin, width, height, name, background, font size
                Image categoryImage = new Image();
                categoryImage.Margin = new Thickness(-50, 15, 0, 0);
                categoryImage.Width = 50;
                categoryImage.Height = 50;
                if (category == "Priority")
                {
                    categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/exclamination_mark.png", UriKind.Relative));
                }
                else
                    if (category == "Favourite")
                    {
                        categoryImage.Source = new BitmapImage(new Uri("/AlarmClock;component/Images/star_full.png", UriKind.Relative));
                    }


                Grid.SetColumn(categoryImage, 2);
                schedule.Children.Add(categoryImage);
            }

            scheduleListBox.Items.Add(schedule);
        }

Quickhorn 的回答基本上是正确的。问题是您正在为列表中的每一项创建一个大网格而不是一行。这是代码的简化示例,它使用模型对象和数据绑定。

通过这种方式,您可以轻松地对 xaml 中的所有内容进行样式设置,并且使事情变得更加简单。

背后代码:MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage
{
    private ObservableCollection<Schedule> _schedules;

    public MainPage()
    {
        InitializeComponent();

        string[] timeSplit = new string[] { "One1", "Two2", "Three3" };
        string[] titleSplit = new string[] { "Title1", "Title2", "Title3" };
        string[] categorySplit = new string[] { "Priority", "Favourite", "Another" };

        _schedules = new ObservableCollection<Schedule>();

        for ( int i = 0; i < timeSplit.Length; i++ )
        {
            _schedules.Add( CreateSchedule( timeSplit[i], titleSplit[i], categorySplit[i] ) );
        }

        scheduleListBox.ItemsSource = _schedules;
    }

    private Schedule CreateSchedule( string time, string title, string category )
    {
        Schedule schedule = new Schedule
        {
            Time = time,
            Title = title,
            Category = category
        };

        if ( category == "Priority" )
        {
            schedule.ImageSource = "/AlarmClock;component/Images/exclamination_mark.png";
        }
        else if ( category == "Favourite" )
        {
            schedule.ImageSource = "/AlarmClock;component/Images/star_full.png";
        }

        return schedule;
    }
}

public class Schedule
{
    public string Time { get; set; }
    public string Title { get; set; }
    public string Category { get; set; }
    public string ImageSource { get; set; }
}

主页.xaml

<ListBox
    x:Name="scheduleListBox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock
                    Text="{Binding Time}"/>
                <TextBlock
                    Text="{Binding Title}"
                    Grid.Column="1"/>
                <StackPanel
                    Orientation="Horizontal"
                    Grid.Column="2">
                    <TextBlock
                        Text="{Binding Category}"/>
                    <Image
                        Source="{Binding ImageSource}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Windows Phone 7 中的网格 的相关文章

  • 如何检查图像对象与资源中的图像对象是否相同?

    所以我试图创建一个简单的程序 只需在单击图片框中更改图片即可 我目前只使用两张图片 所以我的图片框单击事件函数的代码 看起来像这样 private void pictureBox1 Click object sender EventArgs
  • 无法使用已与其底层 RCW 分离的 COM 对象。在 oledb 中

    我收到此错误 但我不知道我做错了什么 下面的代码在backrgroundworker中 将异常详细信息复制到剪贴板 System Runtime InteropServices InvalidComObjectException 未处理 通
  • 获取按下的按钮的返回值

    我有一个在特定事件中弹出的表单 它从数组中提取按钮并将标签值设置为特定值 因此 如果您要按下或单击此按钮 该函数应返回标签值 我怎样才能做到这一点 我如何知道点击了哪个按钮 此时代码返回 DialogResult 但我想从函数返回 Tag
  • 如何使用GDB修改内存内容?

    我知道我们可以使用几个命令来访问和读取内存 例如 print p x 但是如何更改任何特定位置的内存内容 在 GDB 中调试时 最简单的是设置程序变量 参见GDB 分配 http sourceware org gdb current onl
  • pthread_cond_timedwait() 和 pthread_cond_broadcast() 解释

    因此 我在堆栈溢出和其他资源上进行了大量搜索 但我无法理解有关上述函数的一些内容 具体来说 1 当pthread cond timedwait 因为定时器值用完而返回时 它如何自动重新获取互斥锁 互斥锁可能被锁定在其他地方 例如 在生产者
  • 未解决的包含:“cocos2d.h” - Cocos2dx

    当我在 Eclipse 中导入 cocos2dx android 项目时 我的头文件上收到此警告 Unresolved inclusion cocos2d h 为什么是这样 它实际上困扰着我 该项目可以正确编译并运行 但我希望这种情况消失
  • 如何避免情绪低落?

    我有一个实现状态模式每个状态处理从事件队列获取的事件 根据State因此类有一个纯虚方法void handleEvent const Event 事件继承基础Event类 但每个事件都包含其可以是不同类型的数据 例如 int string
  • 如何在列表框项目之间画一条线

    我希望能够用水平线分隔列表框中的每个项目 这只是我用于绘制项目的一些代码 private void symptomsList DrawItem object sender System Windows Forms DrawItemEvent
  • 将布尔参数传递给 SQL Server 存储过程

    我早些时候问过这个问题 我以为我找到了问题所在 但我没有 我在将布尔参数传递给存储过程时遇到问题 这是我的 C 代码 public bool upload false protected void showDate object sende
  • 指针问题(仅在发布版本中)

    不确定如何描述这一点 但我在这里 由于某种原因 当尝试创建我的游戏的发布版本进行测试时 它的敌人创建方面不起作用 Enemies e level1 3 e level1 0 Enemies sdlLib 500 2 3 128 250 32
  • WPF TabControl,用C#代码更改TabItem的背景颜色

    嗨 我认为这是一个初学者的问题 我搜索了所有相关问题 但所有这些都由 xaml 回答 但是 我需要的是后台代码 我有一个 TabControl 我需要设置其项目的背景颜色 我需要在选择 取消选择和悬停时为项目设置不同的颜色 非常感谢你的帮助
  • 使用 System.Text.Json 即时格式化 JSON 流

    我有一个未缩进的 Json 字符串 例如 hash 123 id 456 我想缩进字符串并将其序列化为 JSON 文件 天真地 我可以使用缩进字符串Newtonsoft如下 using Newtonsoft Json Linq JToken
  • 在 ASP.NET Core 3.1 中使用包含“System.Web.HttpContext”的旧项目

    我们有一些用 Net Framework编写的遗留项目 应该由由ASP NET Core3 1编写的API项目使用 问题是这些遗留项目正在使用 System Web HttpContext 您知道它不再存在于 net core 中 现在我们
  • C# 中的递归自定义配置

    我正在尝试创建一个遵循以下递归结构的自定义配置部分
  • for循环中计数器变量的范围是多少?

    我在 Visual Studio 2008 中收到以下错误 Error 1 A local variable named i cannot be declared in this scope because it would give a
  • 实体框架 4 DB 优先依赖注入?

    我更喜欢创建自己的数据库 设置索引 唯一约束等 使用 edmx 实体框架设计器 从数据库生成域模型是轻而易举的事 现在我有兴趣使用依赖注入来设置一些存储库 我查看了 StackOverflow 上的一些文章和帖子 似乎重点关注代码优先方法
  • 在 Dynamics CRM 插件中访问电子邮件发件人地址

    我正在编写一个 Dynamics CRM 2011 插件 该插件挂钩到电子邮件实体的更新后事件 阶段 40 pipeline http msdn microsoft com en us library gg327941 aspx 并且在此阶
  • 32 位到 64 位内联汇编移植

    我有一段 C 代码 在 GNU Linux 环境下用 g 编译 它加载一个函数指针 它如何执行并不重要 使用一些内联汇编将一些参数推送到堆栈上 然后调用该函数 代码如下 unsigned long stack 1 23 33 43 save
  • mysql-connector-c++ - “get_driver_instance”不是“sql::mysql”的成员

    我是 C 的初学者 我认为学习的唯一方法就是接触一些代码 我正在尝试构建一个连接到 mysql 数据库的程序 我在 Linux 上使用 g 没有想法 我运行 make 这是我的错误 hello cpp 38 error get driver
  • 使用按位运算符相乘

    我想知道如何使用按位运算符将一系列二进制位相乘 但是 我有兴趣这样做来查找二进制值的十进制小数值 这是我正在尝试做的一个例子 假设 1010010 我想使用每个单独的位 以便将其计算为 1 2 1 0 2 2 1 2 3 0 2 4 虽然我

随机推荐

  • ES6类中的构造函数和原型中的构造函数之间的区别?

    ES6 类和函数原型都有一个contructor 但我想知道它们是一样的吗 让我给出更多解释 因此 我创建了一个 Cat 函数 例如 const Cat function name this name name The Cat has th
  • WPF 数据变化动画

    我正在开发一个列出一些对象的 WPF 网格 如果对象的数据发生变化 我想启动动画 下面列出了 XAML 代码的摘录
  • 在生产环境中使用 PGO(配置文件引导优化)的风险

    我有一个系统 Linux 和 C 执行密集的信号 图像处理操作 我想使用 PGO 来提高我们应用程序的性能 使用 PGO 时我应该注意哪些风险 潜在问题 单元测试 E2E测试是否足以验证PGO没有破坏任何东西 微软有一个系统 它根据使用统计
  • 永久添加到 MAC 上的 DYLD_LIBRARY_PATH 会导致 X11 错误

    我正在使用 Python 2 7 并尝试导入 graph tool 并且我的 libboost thread mt dylib 似乎位于 opt local lib 而不是 usr local lib 中 如果我启动 X11 终端并输入 e
  • 在运行时修改 Spring Security 配置

    我正在使用最新的 Spring Boot Spring Boot Starter Security 来构建简单的代理应用程序 目标是使用单一路由 方法启动应用程序 RequestMapping value api register meth
  • 在文本框控件内添加标签

    我想创建一个继承自 TextBox 的控件 其中有一个标签 其中 粘贴 到文本框的右侧 并且该文本不可由用户编辑 而是由属性设置 如何才能做到这一点 我意识到可能有很多原因导致这种用户体验是一个坏主意 但我必须这样做 改编自汉斯 帕桑特的w
  • 使用“月-年”日期值创建带有时间线的 D3.js 散点图

    我有一个列出日期的数据集 如下所示 var dataset 1 2006 20 3 2009 90 11 2004 50 5 2012 33 4 2008 95 4 2004 12 7 2000 44 5 2006 67 6 2007 21
  • POSIX 相当于 boost::thread::hardware_concurrency [重复]

    这个问题在这里已经有答案了 可能的重复 以编程方式查找机器上的核心数 用于确定系统在不超额订阅的情况下可以运行的最大线程数的 POSIX 或 x86 x86 64 特定系统调用是什么 谢谢 它使用 C 兼容的结构 那么为什么不直接使用实际的
  • Magento 模块在本地主机上运行良好,但在实时服务器上运行不佳

    我这里有一个奇怪的案例 I m making a simple magento module right now Some kind of script injection module similar to google analytic
  • 使用 UML 进行 C 编程

    我正在开发一个基于标准规范的嵌入式软件 该软件很大 在开始编码之前我需要设计我的软件 UML是一种面向对象的语言 可能是一个解决方案 但我不知道如何将它用于C软件 有没有任何文档可以帮助使用 UML 进行 C 编程 或者还有其他的c软件设计
  • 使用 cookie 保留整个网站的样式表偏好

    我有一个简单的 JavaScript 函数 它允许我在网站的每个页面中的不同样式表之间进行交换 目前 我的网站上没有实施任何 cookie 因此每当我进入新页面时 都会加载默认样式表 如果用户想要使用备用样式表 他们将被迫再次交换 我想通过
  • ios 中键盘显示方向错误

    我有一个viewcontroller在支持横向和纵向方向的应用程序中 单击按钮后 会出现一个弹出窗口 我应该在其中输入名称 在纵向模式下一切正常 但是 如果我关闭键盘 向左或向右旋转设备 然后打开弹出窗口 键盘仍会以纵向模式打开 I ve
  • 在c#中读取原始图像文件

    如何在未安装编解码器的情况下解码 打开 CR2 或 NEF 和 ARW 等原始图像文件 例如 lightroom 打开原始文件 我的代码如下所示 if fe CR2 fe NEF fe ARW BitmapDecoder bmpDec Bi
  • Xamarin MonoAndroid 设备管理

    我正在使用 Visual Studio 2017 来开发 Android 单视图应用程序 该应用程序应该是一个信息亭应用程序 我的目标是 API 21 在 Visual Studio AVD 设备 提供的模拟器上 我遇到的问题是我似乎无法制
  • 在 Joda 中将 UTC 转换为 LocalDateTime?

    DateTime dt new DateTime 2014 09 15T21 20 14 System out println dt System out println dt plusMillis 581042272 toDateTime
  • Android - 将下一个和上一个按钮添加到软键盘,该按钮在我的活动中单击编辑文本时出现

    我需要将 下一个 和 上一个 按钮添加到我的软键盘上 该键盘在单击活动页面中的编辑文本字段时出现 请为我提供有关实现此概念的教程的指导 提前致谢 您可以通过以下方式在 xml 中执行此操作 android imeOptions action
  • 如何在 WCF 客户端中提供用户名和客户端证书(为什么此示例有效)?

    考虑一个 WCF 服务 其目的是让传输层需要客户端证书 客户端证书在 IIS 中设置为 必需 同样 消息层也会有用户名认证 现在我已经看到这个问题了 禁止 WCF 客户端证书和用户名凭据 我可以在某种程度上理解那里发生的事情 并意识到 WC
  • Serilog HTTP接收器+Logstash:将Serilog消息数组拆分为单独的日志事件

    我们正在使用Serilog HTTP 接收器将消息发送到 Logstash 但是HTTP消息体是这样的 events Timestamp 2016 11 03T00 09 11 4899425 01 00 Level Debug Messa
  • 文件对象的初始化

    我想问一下java中初始化的格式 我目前所知道的是 int a 1 double b 1 0 String c java etc 现在 在主类中我想初始化一个File 我不知道该怎么做 首先 文件是一种对象类型 与原始类型 int 和 do
  • Windows Phone 7 中的网格

    我有一个网格视图代码 下面有分为3列 但我的代码有一个问题 什么时候多重数据被检索到 第 3 列中的所有数据都是重叠的 我如何修改下面的代码 例如它会在下面依次显示 Define grid column size Grid schedule