Binding a ListView to ObjectData

B

Bill McCormick

I'm trying to bind some object data to a ListView contained within a
UserControl as follows:

<!--MyDataGrid.xmal-->
<UserControl x:Class="MyDataGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<ObjectDataProvider
x:Key="MyData"
MethodName="LoadMyData" />
</UserControl.Resources>
<Grid DataContext="{StaticResource MyData}">
<DockPanel>
<ListView ItemsSource="{Binding}">
...
</ListView >
</DockPanel>
</Grid>
</UserControl>

//MyDataGrid.xmal.cs
namespace MyNameSpace {
/// <summary>
/// Interaction logic for MyDataGrid.xaml
/// </summary>
public partial class MyDataGrid: UserControl {
public MyDataCollection myData;

public MyDataGrid() {
InitializeComponent();
}

public void LoadMyData() {
myData = SomeCommandToLoadData();

}
}
}

I've had to change the names to protect the innocent, but it does
compile and run. It's just that the LoadMyData method is never called.

Does anybody out there know how to do this?


Thanks,

Bill
 
H

Hongye Sun [MSFT]

Hi Bill,

Thanks for your post. My name is Hongye Sun [MSFT] and it is my pleasure to
work with you.

ObjectDataProvider cannot call a method without ObjectType or
ObjectInstance. You must assign a value to either of the two properties.
For this case, the method is within the UserControl object, so the solution
is to set the ObjectInstance property to this. However it is not supported
to do it by using binding expression. We must do it in code behind. Here is
the modified code behind:
--------------------------------------
public MyDataGrid() {
InitializeComponent();
ObjectDataProvider dataProvider = this.TryFindResource("MyData") as
ObjectDataProvider;
if (dataProvider != null)
{
dataProvider.ObjectInstance = this;
}
}
--------------------------------------
By setting ObjectInstance, the LoadMyData method will be called immediately
and its return value will stored in the ObjectDataProvider. I also changed
the LoadMyData to let it return values. Here is the modified code:
--------------------------------------
public MyDataCollection LoadMyData() {
myData = SomeCommandToLoadData();
return myData
}
--------------------------------------
The modified code is working properly in our lab.

In addition, there is another suggestion for you to bind myData to ListView
directly by local property binding. Here is the sample xaml and code behind:
--------------------------------------
<!--MyDataGrid.xmal-->
<UserControl x:Class="MyDataGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="myDataGridControl">
<Grid DataContext="{Binding MyData, ElementName=myDataGridControl}">
<DockPanel>
<ListView ItemsSource="{Binding}">
...
</ListView >
</DockPanel>
</Grid>
</UserControl>

//MyDataGrid.xmal.cs
namespace MyNameSpace {
/// <summary>
/// Interaction logic for MyDataGrid.xaml
/// </summary>
public partial class MyDataGrid: UserControl {
public MyDataCollection myData;

public MyDataGrid() {
InitializeComponent();
}

public MyDataCollection MyData
{
get
{
myData = SomeCommandToLoadData();
return myData;
}
}
}
}
--------------------------------------

Please have a try of both suggestions and let us know if they meets your
requirement. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bill McCormick

Hongye said:
Hi Bill,

Thanks for your post. My name is Hongye Sun [MSFT] and it is my pleasure to
work with you.

ObjectDataProvider cannot call a method without ObjectType or
ObjectInstance. You must assign a value to either of the two properties.
For this case, the method is within the UserControl object, so the solution
is to set the ObjectInstance property to this. However it is not supported
to do it by using binding expression. We must do it in code behind. Here is
the modified code behind:
--------------------------------------
public MyDataGrid() {
InitializeComponent();
ObjectDataProvider dataProvider = this.TryFindResource("MyData") as
ObjectDataProvider;
if (dataProvider != null)
{
dataProvider.ObjectInstance = this;
}
}
--------------------------------------
By setting ObjectInstance, the LoadMyData method will be called immediately
and its return value will stored in the ObjectDataProvider. I also changed
the LoadMyData to let it return values. Here is the modified code:
--------------------------------------
public MyDataCollection LoadMyData() {
myData = SomeCommandToLoadData();
return myData
}
--------------------------------------
The modified code is working properly in our lab.

In addition, there is another suggestion for you to bind myData to ListView
directly by local property binding. Here is the sample xaml and code behind:
--------------------------------------
<!--MyDataGrid.xmal-->
<UserControl x:Class="MyDataGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="myDataGridControl">
<Grid DataContext="{Binding MyData, ElementName=myDataGridControl}">
<DockPanel>
<ListView ItemsSource="{Binding}">
...
</ListView >
</DockPanel>
</Grid>
</UserControl>

//MyDataGrid.xmal.cs
namespace MyNameSpace {
/// <summary>
/// Interaction logic for MyDataGrid.xaml
/// </summary>
public partial class MyDataGrid: UserControl {
public MyDataCollection myData;

public MyDataGrid() {
InitializeComponent();
}

public MyDataCollection MyData
{
get
{
myData = SomeCommandToLoadData();
return myData;
}
}
}
}
--------------------------------------

Please have a try of both suggestions and let us know if they meets your
requirement. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support
Thank you very much Hongye.

Additionally, I have 2 other issues detailed in the following posts:

1. Broken WCF service reference in VS 2008
A client application contained in the same solution as the host service
application cannot connect.

2. Can't reference ObservableCollection
A host service data contact is an ObservableCollection<T>, however, the
proxy generates a List<T> for the client. This is the collection that I
want my grid to bind to.

Thanks,

Bill
 
B

Bill McCormick

Hongye said:
Hi Bill,

Thanks for your post. My name is Hongye Sun [MSFT] and it is my pleasure to
work with you.

ObjectDataProvider cannot call a method without ObjectType or
ObjectInstance. You must assign a value to either of the two properties.
For this case, the method is within the UserControl object, so the solution
is to set the ObjectInstance property to this. However it is not supported
to do it by using binding expression. We must do it in code behind. Here is
the modified code behind:
--------------------------------------
public MyDataGrid() {
InitializeComponent();
ObjectDataProvider dataProvider = this.TryFindResource("MyData") as
ObjectDataProvider;
if (dataProvider != null)
{
dataProvider.ObjectInstance = this;
}
}
--------------------------------------
By setting ObjectInstance, the LoadMyData method will be called immediately
and its return value will stored in the ObjectDataProvider. I also changed
the LoadMyData to let it return values. Here is the modified code:
--------------------------------------
public MyDataCollection LoadMyData() {
myData = SomeCommandToLoadData();
return myData
}
--------------------------------------
The modified code is working properly in our lab.

In addition, there is another suggestion for you to bind myData to ListView
directly by local property binding. Here is the sample xaml and code behind:
--------------------------------------
<!--MyDataGrid.xmal-->
<UserControl x:Class="MyDataGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="myDataGridControl">
<Grid DataContext="{Binding MyData, ElementName=myDataGridControl}">
<DockPanel>
<ListView ItemsSource="{Binding}">
...
</ListView >
</DockPanel>
</Grid>
</UserControl>

//MyDataGrid.xmal.cs
namespace MyNameSpace {
/// <summary>
/// Interaction logic for MyDataGrid.xaml
/// </summary>
public partial class MyDataGrid: UserControl {
public MyDataCollection myData;

public MyDataGrid() {
InitializeComponent();
}

public MyDataCollection MyData
{
get
{
myData = SomeCommandToLoadData();
return myData;
}
}
}
}
Also, can you tell me if I should be using an control other that
ListView if my intent is to have the user edit the data?

Thanks,

Bill
 
B

Bill McCormick

Also, can you tell me if I should be using an control other that
ListView if my intent is to have the user edit the data?
I just found the DataGrid in the WPFToolKit ... I'll fly with that and
post any questions I have in another thread.

Thanks.
 
H

Hongye Sun [MSFT]

Hi Bill,

Thanks for your reply.

For issue: 1. Broken WCF service reference in VS 2008
I saw it in your another post, shall we continue to work on this issue in
that thread.

For issue: 2. Can't reference ObservableCollection
One of my coworker will work with you on this issue soon in another thread.

For issue: If I should be using an control other that ListView if my intent
is to have the user edit the data?
I have noticed that you are going to have a look of DataGrid. That is a
good idea. Here is an edit feature introduction of DataGrid for you to
reference:
http://blogs.msdn.com/vinsibal/archive/2008/10/01/overview-of-the-editing-fe
atures-in-the-wpf-datagrid.aspx

One thing need to be mentioned that DataGrid is only in CTP version now.
Here are the detaisl:
http://blogs.msdn.com/vinsibal/archive/2008/08/11/net-3-5-sp1-and-wpf-datagr
id-ctp-is-out-now.aspx.

Another option for you is to use ListBox.ItemTemplate. It is documented at
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol
..itemtemplate.aspx.

Here is the sample code:
-----------------------------------------------
<ListBox Width="400" Margin="10"
ItemsSource="{Binding Source={StaticResource myTodoList}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=TaskName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
-----------------------------------------------

You can define sub-controls in ListBox.ItemTemplate to represent each field
in one item and bind it to the data collection.

Please let us know if you have anthing unclear. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Hongye Sun [MSFT]

Hi Bill,

I have not heard from you for many days. I am writing to follow up this
issue and check if it has been well resolved. In my last reply, I have
provided my suggestion on this issue and some resource about DataGrid. Do
you think they are useful to you? Please let us know if you need any
further help on this issue and we will more than happy to help you.

Have a great holiday.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top