How do I make a WPF data template fill the entire width of the listbox?
I also had to set: HorizontalContentAlignment=”Stretch” on the containing ListBox.
I also had to set: HorizontalContentAlignment=”Stretch” on the containing ListBox.
Due to the issue with data binding on CollectionContainer as described http://social.msdn.microsoft.com/Forums/vstudio/en-US/b15cbd9d-95aa-47c6-8068-7ae9f7dca88a/collectioncontainer-does-not-support-relativesource?forum=wpf I now use the following approach: <ListBox> <ListBox.Resources> <CollectionViewSource x:Key=”DogCollection” Source=”{Binding Dogs}”/> <CollectionViewSource x:Key=”CatCollection” Source=”{Binding Cats}”/> </ListBox.Resources> <ListBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection=”{Binding Source={StaticResource DogCollection}}”/> <CollectionContainer Collection=”{Binding Source={StaticResource CatCollection}}”/> </CompositeCollection> </ListBox.ItemsSource> <!– … –> </ListBox> Edit: The CompositeCollection class does not derive from FrameworkElement and … Read more
I usually use a ContentControl to display the data, and swap out the ContentTemplate in a trigger based on the property that changes. Here’s an example I have posted on my blog that swaps a template based on a bound property <DataTemplate x:Key=”PersonTemplate” DataType=”{x:Type local:ConsumerViewModel}”> <TextBlock Text=”I’m a Person” /> </DataTemplate> <DataTemplate x:Key=”BusinessTemplate” DataType=”{x:Type local:ConsumerViewModel}”> … Read more
A DataTemplateSelector does not respond to PropertyChange notifications, so it doesn’t get re-evaluated when your properties change. The alternative I use is DataTriggers that changes the Template based on a property. For example, this will draw all TaskModel objects using a ContentControl, and the ContentControl.Template is based on the TaskStatus property of the TaskModel <DataTemplate … Read more
I discovered a simple workaround for this. For any elements that are not able to search outside the data template encapsulation boundary (i.e. are not being implicitly styled), you can just declare an empty style within the data template for that element type and use the BasedOn attribute of the style to find the correct … Read more
You could create an IValueConverter, which converts an integer to a boolean based on the CutOff. Then use DataTrigger.Value of True (or False, depending on what you are returning). WPF DataTriggers are strictly equality comparers if I remember correctly. So something similar to: public class CutoffConverter : IValueConverter { public object Convert(object value, Type targetType, … Read more
The only thing that I have found do to for this kind of thing is this: <DataTemplate x:Key=”BaseClass”> <!– base class template here –> </DataTemplate> <DataTemplate DataType=”{x:Type app:BaseClass}”> <ContentPresenter Content=”{Binding}” ContentTemplate=”{StaticResource BaseClass}”/> </DataTemplate> <DataTemplate DataType=”{x:Type app:DerivedClass}”> <StackPanel> <ContentPresenter Content=”{Binding}” ContentTemplate=”{StaticResource BaseClass}”/> <!– derived class extra template here –> </StackPanel> </DataTemplate> Basically this creates a “common” … Read more
Although Archedius’s method works, officially it is deprecated and instead recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class like this… public DataTemplate Create(Type type) { StringReader stringReader = new StringReader( @”<DataTemplate xmlns=””http://schemas.microsoft.com/winfx/2006/xaml/presentation””> <” + type.Name + @” … Read more
Assuming that you’ve already set up the ItemsSource etc for drpCreditCardNumberWpf… //create the data template DataTemplate cardLayout = new DataTemplate(); cardLayout.DataType = typeof(CreditCardPayment); //set up the stack panel FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel)); spFactory.Name = “myComboFactory”; spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); //set up the card holder textblock FrameworkElementFactory cardHolder = new FrameworkElementFactory(typeof(TextBlock)); cardHolder.SetBinding(TextBlock.TextProperty, new Binding(“BillToName”)); cardHolder.SetValue(TextBlock.ToolTipProperty, “Card Holder … Read more
Intro In MVVM the usual practice is to have the Views find their ViewModels by resolving them from a dependency injection (DI) container. This happens automatically when the container is asked to provide (resolve) an instance of the View class. The container injects the ViewModel into the View by calling a constructor of the View … Read more