Visual Studio Development Bookmark and Share   
 index > Visual Studio WPF Designer > Making a Datgrid into a User Control loses the data
 

Making a Datgrid into a User Control loses the data

Hi,

I'm playingwith Blend 3 and created a simple datagrid and checked that it displayed data OK. I then made it into a user control and placed it on the stackpanelbut it failed to show the data. Maybe what I'm trying is not possible?

XAML is as below and any clues would be appreciated.

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:local="clr-namespace:TestStuff01" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="Window1"
Title="Window1" Height="449" Width="812" x:Name="Window1" mc:Ignorable="d">
<Window.Resources>
<CollectionViewSource x:Key="LocationsViewSource__BookingsEntities" />
</Window.Resources>
<StackPanel Orientation="Horizontal" >
<my:DataGrid Width="200"
ItemsSource="{Binding Source={StaticResource LocationsViewSource__BookingsEntities}}" />
<local:UserControl1 Width="200"/>
</StackPanel>
</Window>

<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"
mc:Ignorable="d"
xmlns:Custom="http://schemas.microsoft.com/wpf/2008/toolkit"
x:Class="UserControl1"
x:Name="UserControl"
d:DesignWidth="200" d:DesignHeight="415">

<UserControl.Resources>
<CollectionViewSource x:Key="LocationsViewSource__BookingsEntities" />
</UserControl.Resources>

<Grid x:Name="LayoutRoot">
<StackPanel/>
<Custom:DataGrid
ItemsSource="{Binding Source={StaticResource LocationsViewSource__BookingsEntities}}" Width="200" HorizontalAlignment="Right" Margin="0,0,-400,0" />
</Grid>
</UserControl>

Thanks in advance

Craigton  Tuesday, September 22, 2009 5:08 PM
Hi

Since your question seems to relate to performing this task in Expression Blend, you should ask it over at the Expression Blend Forum http://social.expression.microsoft.com/Forums/en-US/blend/threadswhere it will recieve better attention.

There is nothing technically impossible in what you are attempting to do - it's possible that when you made the UserControl, the code that populates the underlying LocationsViewSource__BookingsEntities source didn't get moved into the UserControl's code, so as it stands the UC never has any data to show (since it will be looking in its own local copy of that StaticResource). Check the codebehind of your Window and compare it to the UserControl - if the load code for the CollectionViewSource is there in one but not the other, you have your answer.

If your goal is to have the UserControl pick up the ambient Data Source of your Window (for instance because you are planning to use the same data source for other things on that page), you'll need to modify your control to ItemsSource={Binding} (rather than specifying the source), and then set the DataSource on the UserControl (or a higher container) when you instantiate the UC in the Window.

Mark
Mark Wilson-Thomas - Program Team - WPF & SL Designer for Visual Studio - posts are provided 'as-is'
  • Marked As Answer byCraigton Thursday, September 24, 2009 4:07 PM
  •  
Mark Wilson-Thomas  Thursday, September 24, 2009 12:28 AM
Hi

Since your question seems to relate to performing this task in Expression Blend, you should ask it over at the Expression Blend Forum http://social.expression.microsoft.com/Forums/en-US/blend/threadswhere it will recieve better attention.

There is nothing technically impossible in what you are attempting to do - it's possible that when you made the UserControl, the code that populates the underlying LocationsViewSource__BookingsEntities source didn't get moved into the UserControl's code, so as it stands the UC never has any data to show (since it will be looking in its own local copy of that StaticResource). Check the codebehind of your Window and compare it to the UserControl - if the load code for the CollectionViewSource is there in one but not the other, you have your answer.

If your goal is to have the UserControl pick up the ambient Data Source of your Window (for instance because you are planning to use the same data source for other things on that page), you'll need to modify your control to ItemsSource={Binding} (rather than specifying the source), and then set the DataSource on the UserControl (or a higher container) when you instantiate the UC in the Window.

Mark
Mark Wilson-Thomas - Program Team - WPF & SL Designer for Visual Studio - posts are provided 'as-is'
  • Marked As Answer byCraigton Thursday, September 24, 2009 4:07 PM
  •  
Mark Wilson-Thomas  Thursday, September 24, 2009 12:28 AM
Hi Mark,

Thanks for this extensive response which hit the mark; the code behind from the original bound datagrid was not moved to the User Control code behind. Should have been obvious but it wasn't!

Incidentally, I enjoyed your series on VS2010.

Thanks again,

Jim
Craigton  Thursday, September 24, 2009 10:00 AM
Thanks Jim!

Have you had a chance to try the Visual Studio 2010 Beta releases yet? What do you make of the new features? What kind of software are you building - do you think the new features will be helpful to you?

Cheers
Mark
Mark Wilson-Thomas - Program Team - WPF & SL Designer for Visual Studio - posts are provided 'as-is'
Mark Wilson-Thomas  Thursday, September 24, 2009 4:22 PM
Hi Mark,

Yes I've played with VS2010 and am very impressed with the ease with which one can create master/details/details as per one of your videos although I was not able to do it as straightforwardly as you did in your video (you used datasets and I look to use Entity Framework) but I guess that will be addressed in Beta 2. Extremely impressed with the Sketchflow facility in Blend 3 even though it and VS2010 are not in step - its ability to produce such extensive documentation as a by-product is quite wonderful.

Basically, I'm retired and just keeping my hand in with the various tools but Entity Framework looks like becoming a serious boon since much of the plumbing will be put out of sight in much the same way as it was in Web development with datagrids. WPF is clearly quite astonishing and the new release will ensure fewer people are put off by the (previous) inability to do genuine RAD stuff.

Anyway, many thanks for your help and I'll look out for future videos.

Best wishes,

Jim



Craigton  Thursday, September 24, 2009 5:24 PM
Thanks for the feedback Jim! Glad to hear you are finding the new features work for you - keep us posted if you have time for further explorations!

Mark
Mark Wilson-Thomas - Program Team - WPF & SL Designer for Visual Studio - posts are provided 'as-is'
Mark Wilson-Thomas  Thursday, September 24, 2009 8:13 PM

You can use google to search for other answers

Custom Search

More Threads

• Custom Controls Toolbox
• How to paint Serial Picture Files Real time?
• How to host WPF designer in my application?
• design time features in feb. ctp
• Xbap development Issues
• Store WPF windows in dll file
• Get control from custom property editor?
• LOCAL AREA CONNECTION : DESKTOP PC WITH LAPTOPS
• An unspecified error occurred on the render thread.
• rename Debug config