开发者

WP7 - Data bound value in custom control, how to set default string so it's visible in designer

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 won开发者_如何转开发derful 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


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.


@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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜