How to draw a border around each element in ItemsControl? - c#

I'm using an ItemsControl and a ItemTemplateSelector to draw display the UI of my items. But now, all my elements need to be inside in a Grid (and one of its columns the element should be there).
At the beginning I supposed was right to have a ContentControl nested in an ItemsControl, and this ContentControl should have the ItemTemplateSelector, but I'm not sure if this is the best way to do it.

Stuff like that should be in the Template of the item container, for ItemsControls that is a bit problematic as the containers are ContentPresenters which have no Template. You could subclass ItemsControl to use a ContentControl, then use the ItemsControl.ItemContainerStyle to edit the Template of those containers.

<ItemsControl x:Name="lst">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="10" CornerRadius="1" BorderBrush="Navy">
<TextBox Text="{Binding Name}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I hope this will help.

Related

Is there a way to bind to the UIElement children in an ItemsControl?

Let's say I have a simple ItemsControl, bound to a list of simple data objects:
<ItemsControl ItemsSource="{Binding dataModelCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Fill="Red" Width="10" Height="10"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The children for the ItemsControl here, are the items in dataModelCollection, and these are simple model types. The Items control, however, will fill itself out with a bunch of Rectangles. Is there a way to bind to these rectangles, and not the data objects?
My assumption is that I can, since when an ItemsControl uses ItemsSource, it locks the Items property from being interfered with - you can't add other elements outside the ItemsSource. However, I have been searching for such a way, and have found nothing.
EDIT: This might look like a silly question, but the reason I am looking for an answer on this, is because I would like to use the ItemsControl to generate some UIElements for each item in dataModelCollection, and then put them on a Panel; I can't put them in the Panel directly, and I can't define the Panel as the ItemsControl.ItemsPanel because I need a reference to the actual Panel for some code-behind purposes.
Please keep my reasons mentioned above in mind when reading my following example of my desired outcome:
<ItemsControl x:Name=elementWrapper ItemsSource="{Binding dataModelCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Fill="Red" Width="10" Height="10"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<CustomControls:SpecialPanel x:Name="desiredHome" Elements="{Binding ElementName=elementWrapper, Path=ItemContainerGenerator.Items}" />

What WPF control can I use for a list of readonly items on WP7?

Right now I use a ListBox but that allows the user to select an item which I don't want. Is there a way to disable selecting or a more suitable control I can use?
Right now I have this:
<ListBox ItemsSource="{Binding Path=PersonNames}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontSize="20"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Use an ItemsControl, it's a base class without selection. (As it does not provide its own ScrollViewer you may need to add one (either in the template or around the control) if you need scrolling)
See the ListView control.

How to create items control derivative with such separators?

This is design I need to implement
Basically, this is menu items separated with vertical bar.
I'm using PRISM and whole bar is basically ItemsControl where I inject menu items
I wonder if it's possible to style (without writing code) ItemsControl in such a way that it will automatically insert bar after all items except last one?
It wouldn't be a big deal if I didn't want to skip last vertical bar.
Important! I can't just insert separator manually because menu items inserted from different modules (PRISM) and I never know which one is the last one, so I need to solve this problem on container level.
This can be done with a Clip and RectangleGeometry. By inserting the pipe at the left of your data template, Set a Rectangle Clip to Trim off the first pipe. A little hacky and you may have to fudge the Clip start to get it to look right, but here's a working sample:
<ItemsControl xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String>No</sys:String>
<sys:String>Pipe</sys:String>
<sys:String>On</sys:String>
<sys:String>First</sys:String>
<sys:String>Item</sys:String>
<ItemsControl.Clip>
<RectangleGeometry Rect="5,0,1000,10000" />
</ItemsControl.Clip>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="|" />
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
You could specify the vertical bar in the ItemsTemplate in your ItemsControl.

How to align the columns of an ItemsControl control (WPF)

I have defined a ItemsControl control in xaml as follows:
<ItemsControl BorderThickness="0" Name="SummaryList">
</ItemsControl>
In the same xaml file, I've defined the following DateTemplate for each item in the ItemsControl:
<DataTemplate DataType="{x:Type app:UpgradeItem}">
<StackPanel Orientation="Horizontal">
<Label Margin="5,5" Content="{Binding Path=ItemText,Mode=OneTime}" />
<Label Margin="5,5" Content="{Binding Path=FromVersion,Mode=OneTime}" />
<Label Margin="5,5" Content="{Binding Path=ToVersion,Mode=OneTime}" />
</StackPanel>
</DataTemplate>
I have bound the ItemsControl's ItemSource to a generlic list of UpgradeItems (i.e. List) and programmatically add to this list during execution. The items in the ItemsControl control populate correctly except that the data in the columns are not aligned. How can I modify this so that the three separate columns (ItemText, FromVersion and ToVersion) are aligned?
TIA
You should be able to use a SharedSize groups on grids, however I feel there is a better way to do this. Here's a link on the subject: http://blogs.microsoft.co.il/blogs/guy/archive/2009/06/30/wpf-grid-shared-size-groups.aspx
Part of what makes this tricky is that the ItemsControl doesn't have "columns": the control itself has no idea how its items' contents are laid out in relation to each other.
Your best bet would probably be to use one of the subclasses of ItemsControl that does support columnar data, such as ListView/GridView or the full-fledged DataGrid.

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