Rohit
Rohit

Reputation: 10236

How can I get data from user Control in MainWindow

I have user control which has two controls Label and TextBlock

   <UserControl x:Class=MyClass....
                 d:DesignHeight="300" d:DesignWidth="300" x:Name="MyUsrCtrl">
      <StackPanel>
           <Label Content={Binding MyLabelContent} x:Name="MyLabel"...../>
           <TextBlock Content={Binding MyTextBlockContent} x:Name="MyTextBlock"...../>
     </StackPanel>

  </UserControl> 

and In my MainWindow I have a ListBox whose ItemSource is binded to collection of this usercontrol

 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    <Grid>
        <ListBox x:Name="myListBox" Grid.Row="0"
             ItemsSource="{Binding Path=_myControl}"> // collection of user controls
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <local:MyUserControl x:Name="myUserControl" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>

How can I get the value of the Textblock and Label when any item is selected in Listbox?

Upvotes: 1

Views: 698

Answers (2)

Moddaman
Moddaman

Reputation: 2778

this worked for me:

  • Created a model called MyControl that "represents" the data in MyUserControl
  • Created an ObservableCollection That "represents" the data in the Listbox
  • This way you can also the delete all the x:Name
  • Seperates Data from UI

MainWindow.xaml

   <ListBox x:Name="MyListBox" Grid.Row="0"
         ItemsSource="{Binding MyControls}"
             SelectionChanged="MyListBox_OnSelectionChanged"
             SelectionMode="Single"
             >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <local:MyUserControl></local:MyUserControl>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>

MainWindow.cs

   public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        MyControls = new ObservableCollection<MyControl>();
        var a = new MyControl { MyLabelContent = "label content 1", MyTextBlockContent = "Text content 1" };
        var b = new MyControl { MyLabelContent = "label content 2", MyTextBlockContent = "Text content 2" };

        MyControls.Add(a);
        MyControls.Add(b);

    }


    private void MyListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var listBox = sender as ListBox;
        if (listBox != null)
        {
            var selectedItem = listBox.SelectedItems[0] as MyControl;

            var textBlockContent = selectedItem.MyTextBlockContent; //text in textblock
            var labelContent = selectedItem.MyLabelContent; //text in label

        }
    }


    private ObservableCollection<MyControl> _myControls;


    public ObservableCollection<MyControl> MyControls
    {
        get { return _myControls; }
        set
        {
            _myControls = value;
            NotifyPropertyChanged("MyControls");
        }
    }


    #region PropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }


    #endregion


}

MyUserControl.XAML

   <StackPanel>
        <Label Content="{Binding MyLabelContent}" />
      <TextBlock Text="{Binding MyTextBlockContent}" />
 </StackPanel>

MyControl.cs

 public class MyControl
{
    public string MyLabelContent { get; set; }
    public string MyTextBlockContent { get; set; }

}

Hopes this works for you :)

Here is a link to a working sample: https://drive.google.com/file/d/0B8O-XH0V_o1hNXprX2c0S0xJUFU/view?usp=sharing

Upvotes: 2

Jamaxack
Jamaxack

Reputation: 2460

If you want to get TextBlock from selected item, you can do like this:

 var selectedUserControl = myListBox.SelectedItem as MyUserControl;
 TextBlock textBlock = selectedUserControl.MyTextBlock;

Hope helps!

Upvotes: 0

Related Questions