2

I want to make a wpf user control with list box. in this list box i add a stackpanel as a listbox item.But when i want to use my user control in a application,this user control not showing any data .

here my xaml code of user control

     <Grid>
    <ListBox x:Name="lb" >
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <ContentPresenter/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

i add data in list box like this

 StackPanel sp1 = new StackPanel();
 sp1.Children.Add(someuielement);// some ui element i generate dynamically 
 lb.Items.Add(sp1);

my problem is how to show data in main application. i am not using any binding and not able to figure out how to do this.

thanks

Rohit
  • 52
  • 6
  • [this](http://stackoverflow.com/questions/16546143/whats-the-difference-between-itemtemplate-and-itemcontainerstyle-in-a-wpf-listb) may help.... – Abhinav Sharma Jun 09 '15 at 07:03
  • i am new for xaml. I don't have any stack panel on compile time, i generate dynamically. in this case can i cse ItemTemplate ? @AbhinavSharma – Rohit Jun 09 '15 at 07:09
  • Are you adding StackPanel at runtime.. – Abhinav Sharma Jun 09 '15 at 07:27
  • In the above link it`s explained clearly..ItemContainerStyle is used for styling the container i.e ListBoxItemStyle..You can define DataTemplate for items in the resource and then use in ItemTemplate – Abhinav Sharma Jun 09 '15 at 07:28
  • This should work. If you are not seeing anything in your view you should inspect it using Snoop. You should check if the `ListBoxItem` was created for your `StackPanel` item. My best guess is that some is wrong with `someuielement` you are creating dynamically (maybe try adding a simple `TextBlock` instead, for a simple test). – Novitchi S Jun 09 '15 at 07:45
  • @AbhinavSharma yas i adding stackpanel at runtime in listbox item. and now if you know how to do this please post some code.. thnx – Rohit Jun 09 '15 at 07:58

1 Answers1

0

The recommended way is to use a view model and bind it to the Items property of the ListBox. Like this:

<ListBox ItemsSource="{Binding DataItems}">...

Then in the codebehind instead of dynamically generating the ui elements you would simply bind your view models to the control, or the parent window.

lb.DataContext = new MyViewModel();

which may look like this:

class MyViewModel : ObservableObject
{
    public ObservableCollection<MyDataItem> DataItems {get; private set;}
    public MyViewModel()
    {
        this.DataItems = new ObservableCollection<MyDataItem>();
    }
}

See here for a simple ObservableObject implementation: http://justinmchase.com/2010/08/26/observableobject-for-wpf-mvvm/

justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
  • 1
    You would bind the `ItemsSource` property, not `Items`. – Clemens Jun 09 '15 at 08:14
  • in my case number of ui element which i add to listbox item are not specified, i generate ui element as per my requirement, and also there are many ui element in one stackpanel which are part of one listbox item, please give some extra information or code of your answer thnx@justin.m.chase – Rohit Jun 09 '15 at 09:42
  • All you do is add models to the DataItems collection and more listbox items will appear. You should go onto msdn and read about databinding, this is the right way to do what you are trying to do. – justin.m.chase Jun 09 '15 at 14:27