Generating sample XML data for Blend from Entity Framework data model? - expression-blend

In working with MS Expression Blend, you can import sample data from an XML file to use to test your XAML and bindings in design mode. My data model is fully defined in XML using the Entity Framework. Is there any reasonable way to use that model, either directly or indirectly, as the source for the Blend sample data? It seems like this is a fairly obvious use case, but I haven't found any samples of how to do this. Is my only option to write a utility that serializes an instance of my EF classes to XML?
(I don't think I can use a link to the live datasource, because I'm using Silverlight w/ WCF, and my data model is the auto-generated proxy classes. If there's a way to hook Blend up to a WCF service, I have yet to find it -- though I'd be happy to get some pointers.)

I still don't have a great answer for this. I tried serializing some instances of my EF classes to XML using the DataContractSerializer:
DataContractSerializer serializer = new DataContractSerializer(typeof(Room));
using (SlideLincEntities ctx = new SlideLincEntities())
{
Room roomWithMostSessions = ctx.Room
.OrderByDescending(r => r.Sessions.Count)
.FirstOrDefault();
string fileName = ConfigurationSettings.AppSettings["outputFile"];
Console.WriteLine("Writing data to file '{0}'", fileName);
File.Delete(fileName);
using (Stream fileStream = File.Open(fileName, FileMode.OpenOrCreate))
{
serializer.WriteObject(fileStream, roomWithMostSessions);
}
}
Unfortunately, Blend can't read the generated XML: the DataContractSerializer uses XML ref notation, which apparently confuses Blend's simplistic XML deserializer.
I also tried generating sample data by hand (uggh) using XAML's object notation:
<rs:Room d:IsDataSource="True"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:rs="clr-namespace:SlideLinc.Client.Common.RoomService;assembly=SlideLinc.Client.Common"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d"
Name="_default" OwnerId="smithkl42"
>
<rs:Room.Owner>
<rs:RegisteredUser x:Name="ken" UserId="ken" Email="ken#hotmail.com" FirstName="Ken" LastName="Smith" MaxFileUploadSize="20000" UserName="Ken Smith" />
</rs:Room.Owner>
</rs:Room>
This was better, but for some reason, Blend wouldn't bind to any collections within the objects: I don't know why, because of course XAML databinding errors get swallowed silently. (Have I mentioned how much I hate XAML databinding?)
I eventually went with defining small amounts of data straight within the XAML form proper:
<ListBox x:Name="fileListBox" Grid.Row="2" Margin="4" BorderThickness="0" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25" />
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<Image Source="/Images/Word.png" Grid.Column="0" Margin="2" />
<TextBlock Text="{Binding OriginalFileName}" Grid.Column="1" VerticalAlignment="Center" />
<HyperlinkButton Content="Share" Grid.Column="2" Margin="4" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<HyperlinkButton Grid.Column="4" Margin="4" HorizontalAlignment="Right" VerticalAlignment="Center" >
<Image Source="/Images/trashcan.png" Width="25" />
</HyperlinkButton>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<!-- I shouldn't have to do this, but for some reason, I can't get sample data any other way -->
<roomservice:SharedFile OriginalFileName="Some Document.docx" />
<roomservice:SharedFile OriginalFileName="Another document.pptx"/>
</ListBox>
So that's my current approach. Not very elegant. Open to other ideas.

Related

How can I use the Design Tab in Visual Studio when designing XAML UI for a UWP App that uses Data Template in List View?

This is a bit of a tricky question to put succinctly but here goes. Snippets have been included to get the point across.
This problem came about whilst trying to solve another problem, to which a solution was found on SO, but which required to right-click and edit the default style of a TextBox, which I can't do because the TextBox in question is part of a DataTemplate on a databound ListView which of course has no content at design time.
Here are some snippets: I use a data template to control the content/layout of my listview like so:
<ListView x:Name="DataItemList"
ItemsSource="{x:Bind MasterMenuItem.DataObjects, Mode=OneWay}"
SelectedItem="{x:Bind SelectedItem, Mode=TwoWay}"
SelectionMode="Single"
ItemTemplate="{StaticResource DataListItemTemplate}">
</ListView>
and a data template as a page/control resource
<DataTemplate x:Key="DataListItemTemplate" x:DataType="vm:DataObjectListItem">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<Grid Height="40" Margin="10 0 0 0">
<Grid.Resources>
<SolidColorBrush x:Key="brush" Color="Silver"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="*" MinWidth="240" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="{StaticResource brush}" BorderThickness="1 1 1 1">
<FontIcon Name="Icon"
Grid.Column="0"
VerticalAlignment="Center"
FontSize="32"
Glyph="{x:Bind ListItemIcon}" />
</Border>
<Border Grid.Column="1" BorderBrush="{StaticResource brush}" BorderThickness="1 1 1 1">
<TextBlock Name="Data Object Name"
Padding="6 4 6 4"
Text="{x:Bind Name, Mode=OneWay}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontWeight="Bold"
TextWrapping="WrapWholeWords"/>
</Border>
</StackPanel>
</DataTemplate>
When I view my page in the Design tab of visual studio (instead of the XAML tab) I see nothing useful, unlike if, for example, I put a TextBlock in my XAML with Text="Hello" I would be able to see 'Hello' in the Design tab and crucially Right-Click on it to do certain things...
Is there a way to 'populate' the list with some dummy data, or the text box with some dummy text in order to visualize the runtime layout in the Design tab?

Is there a way that I can add the ColumnDefinitions into the <grid>

My code looks like this:
<Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<local:LabelBodyRendererClass HorizontalOptions="StartAndExpand" Text="Show Subcategory" YAlign="Center" XAlign="Center" />
<Switch x:Name="swtSwitch" Grid.Column="1" Toggled="SwtSwitch" VerticalOptions="Center" HorizontalOptions="End" />
</Grid>
Is there a way that I can add the ColumnDefinitions into the element? My IDE is showing it's an option but I don't know how to specify them.
I don't think you can define it as an attribute. And if you could, it would wreck your readability. Because ColumnDefinition is a complex type, you would have to type the string representation of it, as an array, inside an attribute.
If you want to clean your code, create your own, custom grid which has the columns predefined, but then you would have to expose views as properties, to fill your column. So, you would only save a few lines of the column definitions.

Prism release mode causing an error

Redoing this question (sorry to anyone who was in the middle of reading it)
Attempting to play with Prism 6, but I'm getting an issue on all of my shellview components, when I'm working in debug mode everything is fine, however the moment I switch to release mode I'm getting a lot of errors.
Error CS0246 The type or namespace name 'BindableBase' could not be found
(are you missing a using directive or an assembly reference?)
ShellModule C:\Dev\Prism_Prototype\Source\ShellModule\ViewModels\TitleControlViewModel.cs
This is happening in pretty much every file. On hovering over the error I get the option to add a reference to Prism, and add using for Prism.MVVM, but there's already a reference there, and after adding the reference back in all I get is a new error stating
The type or namespace name "BindableBase" could not be found, are you missing
a using directive or assembly reference.
Here's one of the files which is going wrong as an example.
using Prism.Mvvm;
using ShellModule.ViewModels.Interfaces;
namespace ShellModule.ViewModels
{
public class TitleControlViewModel : BindableBase, ITitleControlViewModel
{
private string _content = "This is my content";
public string Content
{
get { return _content; }
set { SetProperty(ref _content, value); }
}
}
}
And TitleControlView.xaml
<UserControl
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"
xmlns:mvvm="http://prismlibrary.com/"
mc:Ignorable="d"
mvvm:ViewModelLocator.AutoWireViewModel="true"
x:Class="ShellModule.Views.TitleControlView"
HorizontalContentAlignment="Stretch">
<UserControl.Resources>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="100*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label x:Name="DekLogo" Background="{DynamicResource SolidColourASMDefaultRed}" Width="180" Height="60" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Column="0" Padding="1" >
<Image Source="{DynamicResource DekLogo}" Height="36" Width="110" />
</Label>
<Label x:Name="Notifications" Background="{DynamicResource SolidColourDefaultBackground}" HorizontalContentAlignment="Stretch" Grid.Column="1" Content="{Binding Content}" />
<Label x:Name="SiplaceLogo" Background="{DynamicResource SolidColourBackground}" Width="180" Height="60" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Column="2" Padding="1">
<Image Source="{DynamicResource Logo}" Height="36" Width="110" />
</Label>
</Grid>
</UserControl>
Solved my own problem after playing around a bit more.
For some reason (probably during refactoring) my references were messed up in release mode. Went through and uninstalled all the nuget packages I had been using for Prism and Ninject, then reinstalled all the packages, low and behold the problem has been fixed.
Still no idea why it happened in the first place though.

Data bind the index of an item in a listbox?

I have a set of local high scores (ObservableCollection) and they're data bound to a ListBox so I can display them. Here's what I currently have, the "{Binding Index}" of the first TextBlock is where I'm trying to grab the index of the current item but I'm not entirely sure if it's possible through data binding:
<ListBox Grid.Row="1" x:Name="localScoresListBox" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Index}" Grid.Column="0" />
<TextBlock Text="{Binding PlayerName}" Grid.Column="1" />
<TextBlock Text="{Binding Score}" Grid.Column="2" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
So my question. Using data binding, can you grab the index of the current item?
If it is possible I believe it would be 0-based and since I want the first rank to be 1 instead of 0, could you do something like {Binding Index+1}?
EDIT: There's a similar question here for WPF and there didn't seem to be a way to do it then either: WPF ListBox bind to index of the item
Not that I know of - especially since the item that you are binding will be of whatever type your object is, and it probably doesn't have an Index property.
I'd suggest adding the index property to your data objects (it can be added / manipulated when you populate the collection).

Which control to use in Windows Phone 7 so i can display data from a webservice

i'm new to windows phone 7 and i have a webservice which returns data from a sql database.
I'm display the data in a gridview in asp.net.
Now i want to make the same in windows phone 7
Which control to use to show the records and how?
Thank you very much
There is no out of the box datagrid control on windows phone 7.
This is because it is really hard to got a lot of data in a grid readable on a small screen as a phone. If you want to have this anyway you have to build your own.
You can use a listbox as some soft of grid like this:
<ListBox x:Name="myListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock width="100" Text="{Binding Field1}"/>
<TextBlock width="100" Text="{Binding Field2}"/>
<TextBlock width="100" Text="{Binding Field3}"/>
<TextBlock width="100" Text="{Binding Field4}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
If this isn't what you're looking for you could maybe have a look at one of the following links:
http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-1.aspx
WpfToolkit DataGrid does not work in Windows Phone 7
ListBox with custom DataTempale is what you looking for.
First of all, download data from the server and put it into some collection. ObservableCollection is a best choice, because it's automatically update view when you add/remove new items. So, code snipped will be like this:
ObservableCollection<CustomItem> items = new ObservableCollection<CustomItem>();
// add items to the `items` list
list.ItemsSource = items; // bind items to the ListBox with a name 'list'
Xaml:
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDifenition Width="Auto" />
<ColumnDifenition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text={Binding Field1} />
<TextBlock Grid.Column="1" Text={Binding Field1} />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Resources