WrapPanel as ItemPanel for ItemsControl

You might want to take a look at the ItemsPanel property: Gets or sets the template that defines the panel that controls the layout of items. Example: <ItemsControl> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> And you can set it in a Style as follows: <Style TargetType=”ItemsControl”> <Setter Property=”ItemsPanel”> <Setter.Value> <ItemsPanelTemplate> <WrapPanel /> </ItemsPanelTemplate> </Setter.Value> … Read more

Is there a way to determine where a WPF Binding is declared/created?

When debugging WPF binding errors, I find it easiest to break up the error by the semicolons, and start from the end System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘RelativeSource FindAncestor, AncestorType=”System.Windows.Controls.ItemsControl”, AncestorLevel=”1″‘. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is ‘MenuItem’ (Name=””); target property is ‘HorizontalContentAlignment’ (type ‘HorizontalAlignment’) So starting from the end: … Read more

WPF progressbar style is blocky?

Your progress bar has a style that corresponds to your current windows theme. If you run your application on Windows 7 with an Aero theme your progress bar will look accordingly. If you want it to look always the same (no matter what windows theme is selected) you will need to define your own style … Read more

How do I group items in a WPF ListView

I notice one thing right away – the GroupStyle.HeaderTemplate will be applied to a CollectionViewGroup, so your DataTemplate should probably look like this: <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock FontSize=”15″ FontWeight=”Bold” Text=”{Binding Name}”/> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> CollectionViewGroup.Name will be assigned the value of Status for that group.

MenuItem style with icon creates only one icon

Add x:Shared=false for your Icon value. To do that you should declare Image in resources: <Grid> <Grid.Resources> <Image x:Key=”imgCTX” x:Shared=”false” Source=”{Binding Path=Icon}” Height=”16px” Width=”16px”/> <HierarchicalDataTemplate DataType=”{x:Type ViewModels:HeaderedItemViewModel}” ItemsSource=”{Binding Path=Children}”> <ContentPresenter RecognizesAccessKey=”True”></ContentPresenter> </HierarchicalDataTemplate> <Style TargetType=”{x:Type MenuItem}”> <Setter Property=”Header” Value=”{Binding Path=Header}” /> <Setter Property=”InputGestureText” Value=”{Binding Path=InputGestureText}” /> <Setter Property=”Command” Value=”{Binding Path=Command}” /> <Setter Property=”Icon” Value=”{StaticResource imgCTX}” /> … Read more

Can you use a Binding ValidationRule within 1 line in xaml?

Even though the xaml interpreter happens to turn the markup extension into something working, this is not really supported. See MSDN – Binding Markup Extension The following are properties of Binding that cannot be set using the Binding markup extension/{Binding} expression form. … ValidationRules: the property takes a generic collection of ValidationRule objects. This could … Read more

How to access a control from a ContextMenu menuitem via the visual tree?

This is happening because DataContext=”{Binding PlacementTarget,… binding would set the button as MenuItems DataContext but that won’t add the ContextMenu to the VisualTree of your window and that’s why ElementName binding’s won’t work. A simple workaround to use ElementName bindings is to add this in your Window/UserControl’s code-behind: NameScope.SetNameScope(contextMenuName, NameScope.GetNameScope(this)); Another solution is to do … Read more

What exactly are “WPF services”?

Martin Fowler has a description of what a service is in his Dependency Injection article. Put simply, a service is an object that provides functionality to be used by other objects. You’ll find the term used heavily when discussing the patterns Inversion of Control and Service Locator. To make this concrete with the topic at … Read more