Loading, Binding and then saving back

Mar 20, 2011 at 6:13 PM

Hello

I load my banks and assign them to listbox

var banks = from bank in App.Database.Query<Bank, int>()
                  select bank;
this
.Banks.ItemsSource = banks;

which is defined as

<ListBox Name="Banks" VerticalAlignment="Top">
	<ListBox.ItemTemplate>
		<DataTemplate>
			<StackPanel Orientation="Horizontal" Margin="5">
				<CheckBox IsChecked="{Binding LazyValue.Value.Active, Mode=TwoWay}" />
				<StackPanel>
					<TextBlock Text="{Binding LazyValue.Value.Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontStretch="UltraCondensed" />
					<TextBlock Text="{Binding LazyValue.Value.NumberOfATMs}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" />
				</StackPanel>
			</StackPanel>
		</DataTemplate>
	</ListBox.ItemTemplate>
</ListBox>

As you can see, I have a checkbox with TwoWay mode. Problem is that I dont know how to correctly save items in save action.

If I try

var banks = this.Banks.Items;
foreach (var bank in banks)
{
    App.Database.Save(bank);
}

I get SterlingTableNotFoundException. The if I try

var banks = this.Banks.Items;
foreach (Bank bank in banks)
{
    App.Database.Save<Bank>(bank);
}
I get CastException. Its probably very simple solution, but I cant find it.

 

Thanks in advance

Coordinator
Mar 20, 2011 at 8:46 PM

The format you receive from queries is a "TableKey" type. It's not meant to be bound directly, but to be a convenient way for you to parse the queries and then manufacture the objects you need.

In your case, I'd recommend binding to the Bank object directly, not the key, like this:

var banks = from bank in App.Database.Query<Bank, int>() 
                  select bank.LazyValue.Value; 
this.Banks.ItemsSource = banks;

Then bind to "Name" and "NumberOfATMs" etc. without the LazyValue.Value prefix.

Your final save (each var bank in banks... Save(bank)) should then work fine, because save works against the actual entity, not the table key.

 

Mar 20, 2011 at 9:24 PM

Hi

Omg, I was so stupid. Thanks for help!