code_junkie

Access parent datacontext in listbox in Silverlight

Nov 13th, 2011
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. <ListBox DataContext="{Binding}" x:Name="AnswerListBox" ItemContainerStyle="{StaticResource QuestionStyle}" Grid.Row="1" Width="Auto" Grid.Column="2" ItemsSource="{Binding Path=AnswerList}" BorderBrush="{x:Null}" />
  2.  
  3. <Style TargetType="ListBoxItem" x:Key="QuestionStyle">
  4. <Setter Property="Template">
  5. <Setter.Value>
  6. <ControlTemplate TargetType="ListBoxItem">
  7. <StackPanel Background="Transparent" >
  8. <RadioButton Visibility="{Binding Path=QuestionType, Converter={StaticResource QuestionTypeConverter}, ConverterParameter='RadioButtonStyle'}" Height="auto" Margin="0,0,0,10" IsChecked="{TemplateBinding IsSelected}" IsHitTestVisible="False" Content="{Binding Path=AnswerText}">
  9. </RadioButton>
  10. <CheckBox Visibility="{Binding Path=QuestionType, Converter={StaticResource QuestionTypeConverter}, ConverterParameter='CheckBoxStyle'}" Height="auto" Margin="0,0,0,10" Content="{Binding Path=AnswerText}">
  11. </CheckBox>
  12. </StackPanel>
  13. </ControlTemplate>
  14. </Setter.Value>
  15. </Setter>
  16. </Style>
  17.  
  18. <UserControl x:Class="SomeApp.Views.MainPageView" x:Name="control" >
  19.  
  20. <ListBox ItemsSource="{Binding Path=SomeItems}" >
  21. <ListBox.ItemTemplate>
  22. <DataTemplate>
  23. <StackPanel Orientation="Horizontal">
  24. <TextBlock Text="{Binding ElementName=control, Path=DataContext.Threshold}"/>
  25. </StackPanel>
  26. </DataTemplate>
  27. </ListBox.ItemTemplate>
  28. </ListBox>
  29.  
  30. class ViewModel{
  31. ObservableCollection<ComboboxListItemType> DataForTheComboBoxList;
  32. ObservableCollection<MyDataType> DataForTheListBox;
  33. ...
  34. }
  35.  
  36. public partial class MyDataType
  37. {
  38. private ObservableCollection<ComboboxListItemType> m_AllComboboxItems;
  39. public ObservableCollection<ComboboxListItemType> AllComboboxItems
  40. {
  41. get { return m_AllComboboxItems; }
  42. set
  43. {
  44. if (m_AllComboboxItems != value)
  45. {
  46. m_AllComboboxItems = value;
  47. RaisePropertyChanged("AllComboboxItems");
  48. }
  49. }
  50. }
  51. }
  52.  
  53. // in ViewModel class
  54. foreach(var x in this.DataForTheListBox)
  55. {
  56. x.AllComboboxItems = this.DataForTheComboBoxList;
  57. }
  58.  
  59. <DataTemplate x:Key="ListBoxItemTemplate">
  60. ...
  61. <Combobox
  62. ItemsSource="{Binding AllComboboxItems}"
  63. SelectedItem="{Binding CurrentBlah}"/>
  64. </DataTemplate>
Add Comment
Please, Sign In to add comment