Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <UserControl x:Class="SilverlightApplication2.Page"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Width="400" Height="300">
- <UserControl.Resources>
- <DataTemplate x:Key="icTemplate">
- <ComboBox ItemsSource="{Binding StringsChild}" SelectedItem="{Binding SelectedItem}"/>
- </DataTemplate>
- </UserControl.Resources>
- <Grid x:Name="LayoutRoot" Background="White">
- <Grid.RowDefinitions>
- <RowDefinition/>
- <RowDefinition/>
- </Grid.RowDefinitions>
- <ItemsControl x:Name="ic" ItemTemplate="{StaticResource icTemplate}">
- <ItemsControl.ItemsPanel>
- <ItemsPanelTemplate>
- <StackPanel Orientation="Vertical"/>
- </ItemsPanelTemplate>
- </ItemsControl.ItemsPanel>
- </ItemsControl>
- <Button Click="Save" Grid.Row="1" Content="GO"/>
- </Grid>
- namespace SilverlightApplication2
- {
- public partial class Page : UserControl
- {
- public ObservableCollection<SomeClass> StringsParent { get; set; }
- public Page()
- {
- InitializeComponent();
- StringsParent = new ObservableCollection<SomeClass>();
- ic.ItemsSource = StringsParent;
- }
- private void Save(object sender, RoutedEventArgs e)
- {
- SomeClass c = new SomeClass();
- c.StringsChild.Add("First");
- c.StringsChild.Add("Second");
- c.StringsChild.SetSelectedItem("Second");
- StringsParent.Add(c);
- }
- }
- public class SomeClass
- {
- public SelectableObservablecollection<string> StringsChild { get; set; }
- public SomeClass()
- {
- StringsChild = new SelectableObservablecollection<string>();
- }
- }
- public class SelectableObservablecollection<T> : ObservableCollection<T>
- {
- public SelectableObservablecollection()
- : base()
- {
- }
- public void SetSelectedItem<Q>(Q selectedItem)
- {
- foreach (T item in this)
- {
- if (item.Equals(selectedItem))
- {
- SelectedItem = item;
- return;
- }
- }
- }
- private T _selectedItem;
- public T SelectedItem
- {
- get
- {
- return _selectedItem;
- }
- set
- {
- _selectedItem = value;
- OnPropertyChanged(new PropertyChangedEventArgs("SelectedItem"));
- }
- }
- }
- }
- <DataTemplate x:Key="icTemplate">
- <ComboBox ItemsSource="{Binding StringsChild}"
- SelectedItem="{Binding StringsChild.SelectedItem}"/>
- </DataTemplate>
Add Comment
Please, Sign In to add comment