Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Using an Usercontrol as item in a WPF listBox

$
0
0

Hi,

In a WPF application I try to display a usercontrol as List item in a Listbox.

The usercontrol, I call KalenderDag, represent a day in a date range. So the listbox contains a KalenderDag-UserControl for each day in the range.

When I put these KalenderDag-UserControls in a stackpanel everything renders fine. But since the Stackpanel is not as flexible as a listbox (Cannot insert items at the front of the stack) I like to use a (horizontal ) List.  So this is my ListBox:

<ListBox x:Name="MyList"
                     ScrollViewer.HorizontalScrollBarVisibility="Disabled"><ListBox.ItemTemplate><DataTemplate><v:KalenderDag/></DataTemplate></ListBox.ItemTemplate><ListBox.ItemsPanel><ItemsPanelTemplate><StackPanel Orientation="Horizontal" IsItemsHost="True"/></ItemsPanelTemplate></ListBox.ItemsPanel></ListBox>

In the code behind I make a list of KalenderDag that I bind to the Itemssource of the ListBox (myList)

List<KalenderDag> DagenList { get; set; }
                     ...
//Fill the List
{
  Teldatum = StartDatum;
  while(Teldatum <= EindDatum)
  {
         ...
  KalenderDag kd = new KalenderDag();
  //set all the kd properties
         ...
  DagenList.Add(kd);
  Teldatum = Teldatum.AddDays(1);
 }
 MyList.ItemsSource = DagenList;
}

The KalenderDag-UserControl is not very interesting in the context of this question. It has some Textboxes and Borders that are given a value in the codebehind of this usercontrol. The codebehind of the kalenderDag has quite some dependency Properties and logic to render the usercontrol.

The problem now is that when I run the application, I get indeed a line with Kalenderdag-UserControls but there datacontext is not bound, they don't render there datacontex! So it seems that I am adding new  empty Kalenderdag-Usercontrols to the list and not the KalenderDag-usercontrols from the DagenList??

How can I solve this?


Rob





Viewing all articles
Browse latest Browse all 18858

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>