WPF – Bind to Item Index from within ItemTemplate of ItemsControl?

If you’re not using any type of alternating row styles you might be able to hijack the AlternationIndex for this. Set AlternationCount on your ItemsControl to something greater than the max possible count of your items and then use Text=”{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(ItemsControl.AlternationIndex)}” Edit: As bradgonesurfing pointed out in comments, this is not recommended if … Read more

How to dynamically add RowDefinition or ColumnDefinition to a Grid with binding?

You can use attached properties for a Grid that modify the RowDefinitions and ColumnDefinitions when those properties are set or changed. It will allow you to write your Grid like this: <Grid local:GridHelpers.RowCount=”{Binding MaxGridRow}” local:GridHelpers.ColumnCount=”3″ /> Then just expose a property from your ViewModel which returns the largest row number in the Cells collection. You … Read more

How can a separator be added between items in an ItemsControl

<ItemsControl ItemsSource=”{Binding Numbers}”> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <!– could use a WrapPanel if more appropriate for your scenario –> <StackPanel Orientation=”Horizontal”/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation=”Horizontal”> <TextBlock x:Name=”commaTextBlock” Text=”, “/> <TextBlock Text=”{Binding .}”/> </StackPanel> <DataTemplate.Triggers> <DataTrigger Binding=”{Binding RelativeSource={RelativeSource PreviousData}}” Value=”{x:Null}”> <Setter Property=”Visibility” TargetName=”commaTextBlock” Value=”Collapsed”/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> I arrived at your question because I … Read more

Finding control within WPF itemscontrol

Using the ItemContainerGenerator you can obtain the generated container for an item and traverse the visual tree downwards to find your TextBox. In the case of an ItemsControl it will be a ContentPresenter, but a ListBox will return a ListBoxItem, ListView a ListViewItem, etc. ContentPresenter cp = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter; TextBox tb = FindVisualChild<TextBox>(cp); if … Read more

Setting Canvas properties in an ItemsControl DataTemplate

The attached properties only work on direct children of the Canvas. ItemsControl will place ContentPresenter controls as its direct children, so you might want to add a style for that as well: <ItemsControl ItemsSource=”{Binding Path=Nodes}”> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style TargetType=”ContentPresenter”> <Setter Property=”Canvas.Left” Value=”{Binding Path=XPos}” /> <Setter Property=”Canvas.Top” Value=”{Binding Path=YPos}” /> </Style> … Read more

Virtualizing an ItemsControl?

There’s actually much more to it than just making the ItemsPanelTemplate use VirtualizingStackPanel. The default ControlTemplate for ItemsControl does not have a ScrollViewer, which is the key to virtualization. Adding to the the default control template for ItemsControl (using the control template for ListBox as a template) gives us the following: <ItemsControl ItemsSource=”{Binding AccountViews.Tables[0]}”> <ItemsControl.ItemTemplate> … Read more

How to Override PropertyChangedCallback of a predefined Dependency Property ItemsSource in a WPF ItemsControl

Call OverrideMetadata in a static constructor of your derived ItemsSource class: public class MyItemsControl : ItemsControl { static MyItemsControl() { ItemsSourceProperty.OverrideMetadata( typeof(MyItemsControl), new FrameworkPropertyMetadata(OnItemsSourcePropertyChanged)); } private static void OnItemsSourcePropertyChanged( DependencyObject obj, DependencyPropertyChangedEventArgs e) { ((MyItemsControl)obj).OnItemsSourcePropertyChanged(e); } private void OnItemsSourcePropertyChanged(DependencyPropertyChangedEventArgs e) { var oldCollectionChanged = e.OldValue as INotifyCollectionChanged; var newCollectionChanged = e.NewValue as INotifyCollectionChanged; if (oldCollectionChanged … Read more

tech