Bind Silverlight Grid To Data Provider and selecte new item

Bind Silverlight Grid To Data Provider and selecte new item

Old forum URL: forums.lhotka.net/forums/t/7056.aspx


esaulsberry posted on Friday, June 05, 2009

Hi.  I'm trying to tie a Silverlight data grid and data provider together and set the grid selection to a new item when it's added through the data provider.  So far, no luck.  The provider adds the new object but the grid selects the first item in the grid, not the new one.  I realize this is more of a WPF thing than a csla thing but I thought someone might have a suggestion.

<csla:CslaDataProvider x:Key="CslaDpClient"
DataChanged="CslaDpClient_DataChanged"
ManageObjectLifetime="True"
IsInitialLoadEnabled="False"
ObjectType="HorizonSoftware.OSMobileStock.BusinessObjects.ClientList, HorizonSoftware.OSMobileStock.BusinessObjects, Version=..., Culture=neutral, PublicKeyToken=null"
FactoryMethod="BeginGetClientList"
PropertyChanged="CslaDpClient_PropertyChanged" />

<data:DataGrid AutoGenerateColumns="False" HeadersVisibility="Column" x:Name="dgClient" LoadingRow="dgClient_LoadingRow" ItemsSource="{Binding}" Margin="8,0,8,0" SelectionChanged="DataGrid_SelectionChanged">

<Button x:Name="BtnAdd" Margin="0,0,3,0"
csla:InvokeMethod.MethodName="AddNewItem"
csla:InvokeMethod.Resource="{StaticResource CslaDpClient}"
csla:InvokeMethod.TriggerEvent="Click"
IsEnabled="{Binding Mode=OneWay, Path=CanAddNewItem, Source={StaticResource CslaDpClient}}">

No luck trying to do it in code either, which is wierd...

private void dgClient_LoadingRow(object sender, DataGridRowEventArgs e){
HzBo.
Client client = e.Row.DataContext as HzBo.Client;
if (client != null && client.IsNew){
dgClient.SelectedItem = client;
dgClient.UpdateLayout();
//HzBo.ClientList list = dgClient.DataContext as HzBo.ClientList;
//dgClient.SelectedIndex = list.Count - 1;}}

sergeyb replied on Sunday, June 07, 2009

I think doing this in code is the easiest route, and you would need not to use InvokeMethod on your button, but have a direct Click event.

The AddNewCore in your list would be like this

 

AddNewCore()

{

Client newClient = Client.NewClient()

Add(newClient)

OnAddedNew(newClient)

}

 

In your Button_Click

 

((ClientList)this.DataContext).AddedNew+=CLientAdded;

((ClientList)this.DataContext).AddNew();

 

 

Private void ClientAdded(….)

{

myGrid.SelectedItem = e.newObject

myGrid.ScrollIntoView(e.NewObject)

((ClientList)this.DataContext).AddedNew-=CLientAdded;

 

}

 

 

I do not guarantee the exact syntax on the grid, but it should be pretty close.

 

 

 

 

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: esaulsberry [mailto:cslanet@lhotka.net]
Sent: Friday, June 05, 2009 6:10 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Bind Silverlight Grid To Data Provider and selecte new item

 

Hi.  I'm trying to tie a Silverlight data grid and data provider together and set the grid selection to a new item when it's added through the data provider.  So far, no luck.  The provider adds the new object but the grid selects the first item in the grid, not the new one.  I realize this is more of a WPF thing than a csla thing but I thought someone might have a suggestion.

<csla:CslaDataProvider x:Key="CslaDpClient"
DataChanged="CslaDpClient_DataChanged"
ManageObjectLifetime="True"
IsInitialLoadEnabled="False"
ObjectType="HorizonSoftware.OSMobileStock.BusinessObjects.ClientList, HorizonSoftware.OSMobileStock.BusinessObjects, Version=..., Culture=neutral, PublicKeyToken=null"
FactoryMethod="BeginGetClientList"
PropertyChanged="CslaDpClient_PropertyChanged" />

<data:DataGrid AutoGenerateColumns="False" HeadersVisibility="Column" x:Name="dgClient" LoadingRow="dgClient_LoadingRow" ItemsSource="{Binding}" Margin="8,0,8,0" SelectionChanged="DataGrid_SelectionChanged">

<Button x:Name="BtnAdd" Margin="0,0,3,0"
csla:InvokeMethod.MethodName="AddNewItem"
csla:InvokeMethod.Resource="{StaticResource CslaDpClient}"
csla:InvokeMethod.TriggerEvent="Click"
IsEnabled="{Binding Mode=OneWay, Path=CanAddNewItem, Source={StaticResource CslaDpClient}}">

No luck trying to do it in code either, which is wierd...

private void dgClient_LoadingRow(object sender, DataGridRowEventArgs e){
HzBo.
Client client = e.Row.DataContext as HzBo.Client;
if (client != null && client.IsNew){
dgClient.SelectedItem = client;
dgClient.UpdateLayout();
//HzBo.ClientList list = dgClient.DataContext as HzBo.ClientList;
//dgClient.SelectedIndex = list.Count - 1;
}}



esaulsberry replied on Monday, June 08, 2009

Code it is.  I think sometimes I try too hard to do things in the declarative syntax instead of "code" (as if that markup goobly-gook isn't code).

Fel replied on Friday, July 31, 2009

This is your code:

private void dgClient_LoadingRow(object sender, DataGridRowEventArgs e){
HzBo.
Client client = e.Row.DataContext as HzBo.Client;
if (client != null && client.IsNew){
dgClient.SelectedItem = client;
dgClient.UpdateLayout();

Try

private void dgClient_LoadingRow(object sender, DataGridRowEventArgs e){
HzBo.
Client client = e.Row.DataContext as HzBo.Client;
if (client != null && client.IsNew){
dgClient.SelectedItem = client;
dgClient.UpdateLayout();
dgClient.ScrollIntoView(client,dgClient.Columns[0])   //new line


 

Copyright (c) Marimer LLC