WPF listbox empty datatemplate

I’ve had some success with this code: <Style TargetType=”ListBox” x:Key=”ListStyle” BasedOn=”{StaticResource {x:Type ListBox}}”> <Style.Triggers> <DataTrigger Binding=”{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}” Value=”0″ > <Setter Property=”Template”> <Setter.Value> <ControlTemplate> <TextBlock>No items to display</TextBlock> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style>

How to get DataTemplate.DataTrigger to check for greater than or less than?

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

How do I build a DataTemplate in c# code?

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

What is a ViewModelLocator and what are its pros/cons compared to DataTemplates?

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

Access parent DataContext from DataTemplate

I had problems with the relative source in Silverlight. After searching and reading I did not find a suitable solution without using some additional Binding library. But, here is another approach for gaining access to the parent DataContext by directly referencing an element of which you know the data context. It uses Binding ElementName and … Read more

Difference between Control Template and DataTemplate in WPF

Typically a control is rendered for its own sake, and doesn’t reflect underlying data. For example, a Button wouldn’t be bound to a business object – it’s there purely so it can be clicked on. A ContentControl or ListBox, however, generally appear so that they can present data for the user. A DataTemplate, therefore, is … Read more