WP7 - 自定义控件中的数据绑定值,如何设置默认字符串以便其在设计器中可见

发布于 2024-11-18 19:39:15 字数 431 浏览 0 评论 0原文

我对 WP7 相当陌生,对 Expression Blend 也完全陌生。

我有一个绑定到自定义对象列表的 ListBox,

List<Person>

列表中的每个项目都包含一个自定义控件 MyControl,它绑定到 Person。

MyControl 包含一个绑定到 Person 的 Username 属性的 TextBox。

所有这些都运行良好。我的问题是:如何设置 TextBlock 的默认值,以便它在 Designer 或 ExpressionBlend 中可见?由于它是数据绑定的,所以在运行之前它没有文本......所以我实际上无法使用这些美妙的工具做任何花哨的样式,除非我重复删除绑定代码以将其替换为字符串,进行更改,替换绑定代码,重复。看来啰嗦了!

谢谢,

史蒂文

I'm fairly new to WP7 and totally new to Expression Blend.

I have a ListBox bound to a List of custom objects,

List<Person>

Each item in the list contains a custom control, MyControl which is bound to Person.

MyControl contains a TextBox which is bound to the Username property of Person.

All of this works fine. My question is: how do I set a default value for the TextBlock so that it becomes visible in the Designer or ExpressionBlend? With it being data bound, it has no text till it runs ... so I can't actually do any fancy styling using these wonderful tools unless I repeatedly delete the binding code to replace it with a string, make the changes, replace the binding code, repeat. Seems long winded!

Thanks,

Steven

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷爱 2024-11-25 19:39:15

您想要的是“设计时数据”。

有多种方法可以做到这一点。幸运的是,还有大量在线资源 对此进行解释。

What you want is "Design time data".

There are a number of ways of doing this. Fortunately there are also lots of resources online which explain it.

零度℉ 2024-11-25 19:39:15

@Steven您是否考虑过在 Blend 中创建示例数据来执行您需要的操作,然后进行一些绑定以将数据实际附加到绑定到您的列表的控件?您可能想查看混合示例数据 它引导您完成一个简单的示例来实现这一点。然后你也许能够适应你自己的目的。

@Steven Have you looked at creating sample data in Blend to do what you require and then some binding to actually attached the data to the control bound to your list? You might like to check out Blend Sample Data as it guides you through a simple example of doing just that. You might then be able to adapt to to your own ends.

提赋 2024-11-25 19:39:15

这取决于您是否使用任何 MVVM 模型。

我的建议是,如果您不使用 MVVM,则使用 Blend Sample 数据,速度很快。

如果您是 MVVM Light,我发现创建两个文件非常有用:
DataService.cs - 包含真实的连接和数据
DesignDataService.cs - 包含示例数据

从调用角度来看,这两个库是相同的,因此您可以在 ViewModelLocator 中交换它们:

        if (ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
        }
        else
        {
            //SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
            SimpleIoc.Default.Register<IDataService, DataService>();
        }

在 Design 类中,我决定为每个模型创建一个 XML 文件,以便轻松更改样本数据并测试所有可能的场景。

然后,我使用反序列化函数来读取它:

                csNodeList _Copyrights = new csNodeList();
                resource = System.Windows.Application.GetResourceStream(new Uri(@"Design/sampledata.xml", UriKind.Relative));
                streamReader = new StreamReader(resource.Stream);
                serializer = new XmlSerializer(typeof(csNodeList));
                _Copyrights = (csNodeList)serializer.Deserialize(streamReader);

请注意,文件sampledata.xml 必须存储在Design 文件夹中,并且必须定义为内容而不是资源。
建议提高性能和加载时间。

中号

It depends if you are using any MVVM model or not.

My suggestion, if you are not using a MVVM, is to use Blend Sample data, is fast and quick.

If you are MVVM Light I've found very usefull to create two files:
DataService.cs - contains the real connection and data
DesignDataService.cs - contains the sample data

The two libraries are identical, from an call perspective so that in the ViewModelLocator you can swap them:

        if (ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
        }
        else
        {
            //SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
            SimpleIoc.Default.Register<IDataService, DataService>();
        }

In the Design class I've decided to create an XML file for each Model so that it's easy to change the sample data and test all possible scenarios.

I then use the Deserialize function to read it:

                csNodeList _Copyrights = new csNodeList();
                resource = System.Windows.Application.GetResourceStream(new Uri(@"Design/sampledata.xml", UriKind.Relative));
                streamReader = new StreamReader(resource.Stream);
                serializer = new XmlSerializer(typeof(csNodeList));
                _Copyrights = (csNodeList)serializer.Deserialize(streamReader);

Please note that the file sampledata.xml has to be stored in folder Design and must be defined as Content not as Resource.
It is suggested to improve performance and load time.

M

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文