In expression blend I created a sample data source in visual editor. In case of using a listbox I simply drag the collection there and data is automatically shown.
Now, I am interested to retrieve data from datasource from code behind. Is this possible?
Asked
Active
Viewed 320 times
1

Aliens
- 984
- 3
- 14
- 23
1 Answers
1
There are a couple ways to do this, I will give you the simplest. I have a ListPicker that is bascially the same as a ListBox: Here is my ListPicker markup: Also here is a link
<toolkit:ListPicker Name="lpDrag" Grid.Row="4" Grid.Column="1" Loaded="lptest_Loaded" SelectedIndex="0">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding name}" />
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding name}" FontSize="26" Grid.Column="0" Grid.Row="0"/>
<TextBlock Text="{Binding desc}" TextWrapping="Wrap" FontSize="26" Grid.Column="1" Grid.Row="0" />
</Grid>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
Here is the code behind:
lpDrag.ItemsSource = //Whatever your datasource is

Etch
- 3,044
- 25
- 31
-
Ok, what about if you dont use a control in your page? An empty page and codebehind that needs to access the sample data. – Aliens Apr 06 '12 at 20:57
-
I guess I dont understand your question. Are you asking how to retrieve data from a database or something like that? – Etch Apr 07 '12 at 13:34
-
In Expression Blend you can create some sample data that can be used in your application. I know how to bind that data to controls but I dont know how can I use the same data in code. I can see the data is located in folder SampleData and can be read in XAML files. Any idea how to call and use it? – Aliens Apr 07 '12 at 15:41
-
I figured it out. ((SampleDataSource)Application.Current.Resources["SampleDataSource"]).Collection; Thanks for your help, anyway. I marked your response as accepted. – Aliens Apr 08 '12 at 16:01