WPF Wrap ListView, or Grid DataTable - c#

i am trying to make a XAML with wrapping list view, or is it a grid, or table. I am just not sure. Please take a look at the image. How would i go about formatting my "list" that needs to wrap around the view, can someone please give me sample XAML, as i have no idea how to realize how to make this view.
It seems i need to use a ListView inside a Wrappanel of some sort? The data is dynamic, such as i can have any number of key/value pairs as seen here (open, high, lose, close, etc...) so keep in mind the pairs can have any number and they need to wrap around the control/window.
Any idea how to define such a XAML?
Thanks.

I've since figured it out. I'm using an itemcontrol instead of a list and put a wrappanel. Like this.
<ItemsControl ItemsSource="{Binding DataItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Width="100" Text="{Binding KeyText}"></TextBlock>
<TextBlock Width="100" Text="{Binding ValueText}"></TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Related

Custom ComboBox layout

Im trying to make a custom selector that fills the whole page and looks like this (each box represents different selection):
The elements have to be loaded from xml, allways 3 per row, infinite amount of rows possible (based on xml file).
So far i have gotten to building custom DataTemplate with this example:
example
But i have no idea how to approach making selection show fullscreen with multiple rows.
Since you have a fixed number of columns you can use an ItemsControl or a ListBox using a UniformGrid as the ItemPanel used to lay out the items
Sample Xaml Template
<ListBox VerticalContentAlignment="Center" HorizontalContentAlignment="Center" ItemsSource="{Binding MyElementsLoadedFromXml}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="26" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding AFieldInMyXmlElement}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
It should look like this by default:
The IsItemsHost="True" is optional when not using a scrollviewer but as it is meant to be used in this use case I think it is better to use it here anyway
Edit: I forgot it was about ComboBox but as they behave really close to each other, if you replace ListBox by ComboBox you get what I think is your expected result :

Windows Phone 8.1 flow container for dynamic buttons

SOF Tribe,
I have a request for the identity of a flow container into which I can pour an indeterminate number of dynamically created XAML buttons. Said buttons should wrap the text as closely as possible (I think I have a template for this). But the buttons should wrap like a gridview, but run like a stackpanel. Does anyone know a container that will allow me to accomplish what the image portrays?
Currently, for the existing image below, I'm using a gridview, but the gridview sets a specific width but does flow unevenly. A stackpanel has the desired end-to-butt style but won't wrap. So, any ideas?
There is what is called the UniversalWrapPanel made by Greg Stoll. So, that's the direction I'll be taking. Now however, I've got the last little bit to tackle. How to get the bound data to respect the parent container's ability to wrap content and not just stack it up in a vertical column.
The below XAML gives me the below image result which is not quite what I want. Statically added buttons work just fine to the UniversalWrapPanel. But trying to get a binding and ItemsSource to work, well, that's beyond me at this time.
<support:UniversalWrapPanel Orientation="Horizontal" Background="White">
<ItemsControl x:Name="GridViewRecipients" ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Style="{StaticResource ButtonLozengeStyle}" Content="{Binding FullName}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</support:UniversalWrapPanel>
OK, so a bit more searching and I found this:
http://www.visuallylocated.com/post/2015/02/20/Creating-a-WrapPanel-for-your-Windows-Runtime-apps.aspx
and so here is the correct way to reference the UniversalWrapPanel within the ItemsControl:
<ItemsControl x:Name="GridViewRecipients" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<support:UniversalWrapPanel Orientation="Horizontal" Background="DodgerBlue"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Style="{StaticResource ButtonLozengeStyle}" Content="{Binding FullName}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
For the result of:

WrapPanel in a ListView (Metro)

I have the following code in a Metro app. The listview binds up to a list of objects. My problem is that the WrapGrid assigns them equal width. But some Titles are longer then others so width should be set width to Auto. But this dont work.. anyone have any idea?
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Title}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
This works for me:
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid ItemWidth="250" ItemHeight="80" MaximumRowsOrColumns="7" VerticalAlignment="Center" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
The key point here is MaximumRowsOrColumns property. Have fun :)
I think it's not the WrapGrid, but the ListView itself through the ListViewItem/ItemContainerStyle that makes them same size. I don't think there really is a workaround for that though. You could use your own ItemsControl and custom panel or calculate the desired size of each item and based on that - put the items in something like a VariableSizedWrapGrid, setting RowSpan/ColumnSpan appropriately to match that desired size. Other than that - you can either make all items wide enough to fit everything or simply clip the content and display the full strings in the detailed view once the user clicks an item.

Add more of my usercontrol container or usercontrol collection display

I do not know what the container/control im looking for would be called, so I cannot really search for it.
Add More of Same Usercontrol Usercontrol
Clicking on + would add a new instance of My Usercontrol to the right of the existing ones
Clicking on X would dispose the usercontrol that was clicked
I'm not really looking for a tab control that would put each new instance on a new tab, but if there is nothing else then it might do.
The design is not to be as shown in the image obviously, the image just illustrates the basic idea
Any keyword/name suggestions or links to existing implementations?
e.g. Maybe there is a style that turns a ListBox into something suitable?
I would use an ItemsControl and customize it's ItemsPanelTemplate to be whatever you want.
ItemsControls are meant for iterating through a collection of objects, and displaying them in whatever form you want. I wrote some simple code samples of them here if you're interested, or here's another quick example:
<DockPanel x:Name="RootPanel">
<Button Style="{StaticResource AddButtonStyle}"
DockPanel.Dock="Right" VerticalAlignment="Center"
Command="{Binding AddItemCommand" />
<ScrollViewer>
<ItemsControl ItemsSource="{Binding MyCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<local:MyUserControl />
<Button Style="{StaticResource RemoveButtonStyle}"
Command="{Binding ElementName=RootPanel, Path=DataContext.RemoveItemCommand}"
CommandParameter="{Binding }"
HorizontalAlignment="Left" VerticalAlignment="Top" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</DockPanel>
Your ItemsControl would be bound to an ObservableCollection of objects, and your Add/Remove buttons would simply add/remove items from that collection. Since it is an ObservableCollection, it will notify the UI when the collection gets changed and automatically update.
You can indeed use a ListBox and set its ItemTemplate and ItemsPanelTemplate:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Of course, your ItemTemplate would be a reference to your control.
You could look at something called a carousel control which uses a list of objects behind it and displays them similarly to itunes. This could be a bit over the top but is one solution. An example can be seen here
If this is too advanced for your needs, could it be as simple as a stackpanel with a scrollbar which is bound to a list of your user controls?

Showing a dynamic size list in XAML (readonly)

I want to show a list of "properties" in my application. A property is simply a name/value pair. The number of properties is dynamic.
The best way to do this? The only thing I can come up with is creating a ListView with an ItemTemplate. But then the items are selectable, and that's not what I want. If I make the list read-only, it becomes gray. Don't like that either.
Anyone has a better suggestion?
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Properties}">
<ItemsControl.ItemTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Value}"/>
</StackPanel>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
Use a Grid for ItemsControl.ItemsPanel with SharedSizeGroup if you want all items to line up nicely.
Look here:
http://www.codeplex.com/wpg or here
http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!448.entry
They are both implmentations of PropertyGrid from WinForms

Categories

Resources