How to add a button after each control in ItemsControl? - c#

I do no know how to get the current item. If I did not need the button, it would work without the item template. Is there a better option instace of using ItemsControl?

Here's an example of how to do it using an ItemsControl:
<ItemsControl x:Name="ItemsControl" ItemsSource="{Binding Path=Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:Type1}">
<StackPanel>
<TextBlock Text="{Binding Property1}"/>
<TextBlock Text="{Binding Path=Property2, Mode=OneWay}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>

Related

Horizontal Custom ListBox

I want my custom build ListBox to display the items on the next line if their size exceeds the available width of their parent control.
Following is my XAML for this purpose.
<materialDesign:Card>
<StackPanel>
<TextBlock FontWeight="DemiBold" FontSize="30">Schedule</TextBlock>
<ListBox ItemsSource="{Binding Schedule, Source={StaticResource dc}}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal">
<TextBlock>Day:</TextBlock>
<TextBlock Text="{Binding Key, Mode=OneWay}" />
</StackPanel>
<materialDesign:TimePicker materialDesign:HintAssist.Hint="Time In" SelectedTime="{Binding Value.TimeIn}" />
<materialDesign:TimePicker materialDesign:HintAssist.Hint="Time Out" SelectedTime="{Binding Value.TimeOut}" />
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</materialDesign:Card>
Following is the Output for the above XAML
You should use a WrapPanel instead of a StackPanel, then.
As H.B. said, the ItemPanel control you should use is a WrapPanel that let you achieve what you want (when there isn't enough space for your control, it creates a new line).
Here it is an example:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>

Only first control of dynamic created comboboxes has edited style template?

I have a ItemsControl that creates dynamically some ComboBoxes. I have my own style, but for some reason only the first ComboBox has that style.
<ItemsControl Name="MyItemsControl" Grid.Row="4" ItemsSource="{Binding MyList}" Margin="10,0,10,10">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<ComboBox ItemsSource="{Binding ObjectsList}"
Style="{StaticResource MyComboBoxStyle}"
SelectionChanged="ComboBox_SelectionChanged" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Orientation of StackPanel is not Working in ItemsControl (WPF)

My WPF Application is generating dynamic Buttons. I want to show these Button Horizontally. I wrote Code for this. Code is working fine but rather than showing button in Horizontal Direction, it is showing all Buttons in Vertical Direction! Where i also set Orientation of StackPanel!
Can anyone solve my Problem?
My Code is:
<Grid>
<dxlc:ScrollBox>
<ItemsControl x:Name="Buttonslist">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="{Binding Text}" Tag="{Binding Text}" x:Name="New" Margin="5,0,5,0" Click="New_Click" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</dxlc:ScrollBox>
</Grid>
You're actually creating a StackPanel for each item/Button. To get just one for all the items you need to set the ItemsPanel of the control to a StackPanel.
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Text}" Tag="{Binding Text}" x:Name="New" Margin="5,0,5,0" Click="New_Click" />
</DataTemplate>
</ItemsControl.ItemTemplate>

adding dynamic buttons wpf in a wrappanel

I am trying to display buttons dynamically on the screen in a wrap panel so that they are laid our nicely without any scrollbars. I have the markup as below, but the scrollbars appear for some reason. how do we make the scrollbar not appear and the buttons laid out without any scrollbars.
<ListBox x:Name="ItemsListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<ToggleButton Content="{Binding Name}" Click="Click" MinWidth="120" MinHeight="70" FontWeight="Bold" FontSize="18"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel></WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
What control is your ListBox inside? Most often the problem you descrive is caused by the parent control allowing the ListBox to grow.
You can prove whether this is the problem by setting an explicit Width="200" on your ListBox and testing what happens. If it wraps, then the problem is the ListBox's parent.
Add
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
to you containers.
This does it what I was trying to achieve.
<ItemsControl x:Name="ListBox" Grid.Row="5" Grid.ColumnSpan="2">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton Content="{Binding Name}" MinWidth="120" MinHeight="50" FontWeight="Bold" FontSize="16" Margin="5"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Accessing ItemsControl.Items from ItemsControl.DataTemplate

Is it possible to somehow pass the collection of ItemsControl.Items to each individual item in the control via bindings?
What I want to do would look like this (the WorkspaceCubes binding specifically): (it doesn't currently work because you can't bind to an ItemsControl or its Items)
<ItemsControl x:Name="workspace" ItemsSource="{Binding Path=CubeViewModels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="SkyBlue" Margin="0"></Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<Sift:CubeView WorkspaceCubes="{Binding ElementName=workspace, Path=Items}" DataContext="{Binding}"></Sift:CubeView>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Not sure of your purposes, but you can try with:
<ItemsControl x:Name="workspace" ItemsSource="{Binding Path=CubeViewModels}" DataContext="{Binding Path=CubeViewModels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="SkyBlue" Margin="0"></Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<Sift:CubeView WorkspaceCubes="{Binding}"></Sift:CubeView>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Categories