Binding to a property of the page in XAML
-
Hello all, I have a small bit of silverlight code that pulls some data down from a silverlight-enabled web service and binds to it. I'm currently doing the bulk of the work in the constructor. Once the data is down, I bind to it in a couple of places and it is also accessible via a property on the code behind. However, I also have some controls that need to bind to this property in order to provide a list of values on screen. Is there a way to do this with the XAML binding syntax, or do I need to do this in the code behind? Thanks, Will
-
Hello all, I have a small bit of silverlight code that pulls some data down from a silverlight-enabled web service and binds to it. I'm currently doing the bulk of the work in the constructor. Once the data is down, I bind to it in a couple of places and it is also accessible via a property on the code behind. However, I also have some controls that need to bind to this property in order to provide a list of values on screen. Is there a way to do this with the XAML binding syntax, or do I need to do this in the code behind? Thanks, Will
What about this? For example:
<UserControl ...
xmlns:model=".........."<UserControl.DataContext>
model:YourClass/
<UserControl.DataContext><ListBox ItemSource="{Binding YourPropertyOfYourClass}" />
</UserControl>
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders
-
What about this? For example:
<UserControl ...
xmlns:model=".........."<UserControl.DataContext>
model:YourClass/
<UserControl.DataContext><ListBox ItemSource="{Binding YourPropertyOfYourClass}" />
</UserControl>
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders
Sorry about the slow response, it took longer to recover from vacation than I thought (I had to wait until I was off to have time to start learning silverlight). Your suggestion worked, so that means I don't understand the whole DataContext thing like I thought I did. I thought the control got it from whatever control contained it, but that is apparently not the case. Strange. Anyway, thanks for the help, I really appreciate it.
-
What about this? For example:
<UserControl ...
xmlns:model=".........."<UserControl.DataContext>
model:YourClass/
<UserControl.DataContext><ListBox ItemSource="{Binding YourPropertyOfYourClass}" />
</UserControl>
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) Microsoft MVP (Silverlight), WPF/Silverlight Insiders
Ugh. I told you wrong. I forgot I had put some hackish stuff in the code behind that set the ItemsSource. Here's roughly what I have. It's buried down in one cell of a datagrid that is in a cell of another datagrid on the page. <ComboBox x:Name="cboTextColor" SelectedItem="{Binding Color}" Grid.Row="2" Grid.Column="1"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{Binding Color}" Width="25"/> <TextBlock Text="{Binding ColorName}"/> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> There is a regular .NET property on the code behind page called ProductColors. It is currently an ObservableCollection (where ColorEntry has an id, a color code, and a description). It seems like I should be able to bind to it by doing something like: <ComboBox ItemsSource={Binding ProductColors}" /> However, even though the list has two items in it, nothing shows in the drop down. Does this need to be a dependency property or something for binding to work?
-
Ugh. I told you wrong. I forgot I had put some hackish stuff in the code behind that set the ItemsSource. Here's roughly what I have. It's buried down in one cell of a datagrid that is in a cell of another datagrid on the page. <ComboBox x:Name="cboTextColor" SelectedItem="{Binding Color}" Grid.Row="2" Grid.Column="1"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{Binding Color}" Width="25"/> <TextBlock Text="{Binding ColorName}"/> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> There is a regular .NET property on the code behind page called ProductColors. It is currently an ObservableCollection (where ColorEntry has an id, a color code, and a description). It seems like I should be able to bind to it by doing something like: <ComboBox ItemsSource={Binding ProductColors}" /> However, even though the list has two items in it, nothing shows in the drop down. Does this need to be a dependency property or something for binding to work?
gantww wrote:
It seems like I should be able to bind to it by doing something like:
That depends on what "ProductColors" is. Assuming it's a collection of ColorEntry objects, you still need a data context set somewhere so the system knows where to find the ProductColors object to bind to.
Mark Salsbery Microsoft MVP - Visual C++ :java: