Bind ItemsPanelTemplate to property of ItemsSource - wpf

I am trying to create a custom MessageBox with variable number of buttons, this number being equal to the Count of ItemsSource. The buttons are to be placed in a UniformGrid. ItemsSource of ButtonView is assigned to ObservableCollection Buttons. Buttons.Count is not recognized by the code. How could I correctly assign UniformGrid Columns value?
<ItemsControl Grid.Row="2" x:Name="ButtonView">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="{Binding Buttons.Count}">
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="Button">
<Button Margin="2" Content="{Binding Content}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Set the Rows property instead of Columns:
<ItemsPanelTemplate>
<UniformGrid Rows="1"/>
</ItemsPanelTemplate>

Related

WPF WrapPanel inside WrapPanel without free space

I have the following situation:
Inside my main page, I have an ItemsControl and the ItemsPanelTemplate is set to a WrapPanel
<ItemsControl Grid.Row="1" ItemsSource="{Binding StepItems}" x:Name="ItemsControl" Visibility="{Binding ActualView, Converter={StaticResource ActualViewConverter}, ConverterParameter=TableView}" Width="{Binding TableWidth}" Margin="0,5,0,0">
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="FrameworkElement.Margin" Value="0,5,10,5"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel x:Name="itemsWrapPanel" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top"></WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
The bound List "StepITems" contains several controls which itself also represents an ItemsControl as WrapPanel
<ItemsControl ItemsSource="{Binding Rows, ElementName=Root}" x:Name="ItemsControl">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel x:Name="itemsWrapPanel" Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Center"></WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<hmiControl:ScrewPointControl Margin="0,0,10,0"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The Problem is now, that if the next list has not enough space inside the actual row, it starts in a new row, instead of wrapping.
Three ItemsControls with space between each other
Is there a way to manage that the second ItemsControl starts directly after the first and then the second wraps if there is no space anymore?
Greetings,
Caipigott

WPF - How to add textboxes to a wrap panel dynamically when number of textboxes come from a datasource

I need to add textboxes to a wrap panel but the number of textboxes come from a database. How can I do this in XAML binding instead of programmatically.
Thank you in advance
Try something like below code:
<ItemsControl ItemsSource="{Binding NumberOfTextBoxes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding SomeProperty}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Wrap Panel is not wrapping DataItems

I have WrapPanel where I want the control inside of it to go horizontally and centered, but when I have a listbox or a ItemsControl those elements just go downwards.
<toolkit:WrapPanel>
<ItemsControl x:Name="AnswerListBox" ItemsSource="{Binding Answers}" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- those don't wrap horizontally and go downwards -->
<local:spriteToggleButton Text="{Binding text}" Selected="{Binding selected}" Sprites="{Binding Path=DataContext.UISprites, ElementName=questionField}" IsChecked="{Binding selected, Mode=TwoWay}" GroupName="{Binding Path=DataContext.QuestionTitle, ElementName=questionField}" ClickMode="Press" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</toolkit:WrapPanel>
I came across some similar issues and found out about ItemsPanel, so I tried that but it wrap but only to Content and didn't display the rest of the control inside of it.
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
I take it that ItemsPanel is telling the ItemsControl which control to wrap it with but then it seems to ignore the rest of my datatemplate.
Joseph,
Slightly shooting in the dark here; but this is how I have used the WrapPanel (not through the ItemControl).
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Width="700" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
....
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Essentially the Listbox's ItemsPanel tells it to use the WrapPanel and then you could have anything you need in the DataTemplate. Note that the width is important as it tells the WrapPanel where to start wrapping.
Does this help?

WPF: How to apply data template for items in ItemsControl if items are strings?

The ItemsControl defined below is filled with string[] WeekDays. The DataTemplate defined for ItemsControl.ItemTemplate doesn't work, i.e. the week day items are not filled with red background. How do I fix this? Thanks.
...
<ItemsControl
Grid.Row="1"
Margin="20,0,0,0"
ItemsSource="{Binding Path=WeekDays}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Background="Red" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
...
Note: string[] WeekDays is a dependency property of this control. I am not sure if this information might be relevant to finding the solution.
You need to bind the TextBox's Text property to something in order it to work. So, since the data context of the data template is the string itself the binding should be like this:
<DataTemplate>
<TextBlock Text="{Binding}" Background="Red" />
</DataTemplate>

Can I use a DataTemplateSelector within a DataTemplate?

I have an ItemsControl using a StackPanel to display a list of items.
I would like a label to appear for each row, but for the content to the left of the label to be defined by a DataTemplateSelector. I do not want to redefine the label for each DataTemplate generated by the TemplateSelector.
Is this possible?
<ItemsControl ItemsSource="{Binding Path=Values}" >
<ItemsControl.Resources>
<v:MyTemplateSelector x:Key="myTemplateSelector"></v:MyTemplateSelector>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<Label>Test: </Label>
<!--What goes here should be defined by myTemplateSelector-->
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I figured it out. The solution was to use a ContentPresenter element with a ContentTemplateSelector attribute:
<DataTemplate>
<WrapPanel>
<Label>Test: </Label>
<ContentPresenter
ContentTemplateSelector="{StaticResource ResourceKey=myTemplateSelector}">
</ContentPresenter>
</WrapPanel>
</DataTemplate>

Resources