MVVM and Databinding with UniformGrid - wpf

I'm trying to style the back of a WPF chart with some rectangles. I'm using MVVM, and I need the rectangles to be uniformly sized. When defined via Xaml, this works with a fixed "BucketCount" of 4:
<VisualBrush>
<VisualBrush.Visual>
<UniformGrid Height="500" Width="500" Rows="1" Columns="{Binding BucketCount}">
<Rectangle Grid.Row="0" Grid.Column="0" Fill="#22ADD8E6" />
<Rectangle Grid.Row="0" Grid.Column="1" Fill="#22D3D3D3"/>
<Rectangle Grid.Row="0" Grid.Column="2" Fill="#22ADD8E6"/>
<Rectangle Grid.Row="0" Grid.Column="3" Fill="#22D3D3D3"/>
</UniformGrid>
</VisualBrush.Visual>
<VisualBrush>
How can I bind my ObservableCollection of Rectangles? There is no "ItemsSource" property on UniformGrid. Do I need to use an ItemsControl? If so, how can I do this?
Thanks in advance.

You could use ItemsControl to Bind like this. Simple example where ItemsSource is just an ObservableCollection<Brush>
<VisualBrush>
<VisualBrush.Visual>
<ItemsControl x:Name="itemsControl" ItemsSource="{Binding MyBrushes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Height="500" Width="500" Rows="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Fill="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</VisualBrush.Visual>
</VisualBrush>
Update
It works for my usage scenario, but I might be missing something here. Here's the full code I've tried. I get the same result from both
MainWindow.xaml
<Grid>
<Grid.Background>
<VisualBrush>
<VisualBrush.Visual>
<ItemsControl x:Name="itemsControl" ItemsSource="{Binding MyBrushes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Height="500" Width="500" Rows="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Fill="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!--<UniformGrid Height="500" Width="500" Rows="1" Columns="4">
<Rectangle Grid.Row="0" Grid.Column="0" Fill="#22ADD8E6" />
<Rectangle Grid.Row="0" Grid.Column="1" Fill="#22D3D3D3"/>
<Rectangle Grid.Row="0" Grid.Column="2" Fill="#22ADD8E6"/>
<Rectangle Grid.Row="0" Grid.Column="3" Fill="#22D3D3D3"/>
</UniformGrid>-->
</VisualBrush.Visual>
</VisualBrush>
</Grid.Background>
</Grid>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
BrushConverter brushConverter = new BrushConverter();
MyBrushes = new ObservableCollection<Brush>();
MyBrushes.Add(brushConverter.ConvertFrom("#22ADD8E6") as Brush);
MyBrushes.Add(brushConverter.ConvertFrom("#22D3D3D3") as Brush);
MyBrushes.Add(brushConverter.ConvertFrom("#22ADD8E6") as Brush);
MyBrushes.Add(brushConverter.ConvertFrom("#22D3D3D3") as Brush);
this.DataContext = this;
}
public ObservableCollection<Brush> MyBrushes
{
get;
set;
}
}

Related

Border extending too far

I got this xaml code:
<UserControl x:Class="CharacterMap.CharacterMapControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CharacterMap"
xmlns:cxml="clr-namespace:CharacterMap.Xml">
<UserControl.Resources>
<ResourceDictionary>
<Style x:Key="Flat">
<Setter Property="Control.Background" Value="White" />
<Setter Property="Control.FontWeight" Value="DemiBold" />
<Setter Property="Control.FontFamily" Value="Arial" />
</Style>
<DataTemplate x:Key="ItemsTemplate">
<Border BorderThickness="0,0,1,1" BorderBrush="Black">
<Button Width="26" Height="23" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Content="{Binding TheChar}"
Style="{StaticResource Flat}" BorderThickness="0"
ToolTipService.InitialShowDelay="800" ToolTipService.ShowDuration="10000" ToolTipService.BetweenShowDelay="300"
ToolTipService.ShowOnDisabled="True" ToolTip="{Binding AggregatedTooltip}" ToolTipService.IsEnabled="True" />
</Border>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Border BorderBrush="{x:Null}" Height="25" Margin="10,0,20,0">
<TextBlock Text="Filtern nach Buchstabe: " TextWrapping="Wrap" VerticalAlignment="Center"/>
</Border>
<TextBox Text="{Binding FilterString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="125" Height="25" VerticalContentAlignment="Center" />
</StackPanel>
<ScrollViewer x:Name="ScrollViewer" Margin="10,0,10,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1" >
<Border BorderBrush="Black" BorderThickness="1,1,0,0" SnapsToDevicePixels="True" >
<ItemsControl ItemsSource="{Binding CharDescriptions}" ItemTemplate="{StaticResource ItemsTemplate}" Name="CharItemsControl">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Border>
</ScrollViewer>
</Grid>
</UserControl>
It looks something like this:
Before i had the BorderThickness from the button just on 1 but then the borders were overlaying and it looked not so nice.
Now i tried to set the buttons borders thickness to "0,0,1,1" and put a border around the itemscontrol which is "1,1,0,0". The problem is that when i resize the window or in generall the border from the itemscontrol just extends too far and that doesn't look nice either.
How to achieve something like this: ?
You can do something like below. Essentially, you use negative margins to meld all the borders together. On a parent border set a Padding="1" so that the negative margins along the edges can be seen.
The Border around your Button in your DataTemplate would look like this:
<DataTemplate x:Key="ItemsTemplate">
<Border
Margin="-1,-1,0,0"
BorderBrush="Black"
BorderThickness="1">
<Button
Width="26"
Height="23"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
BorderThickness="0"
Content="{Binding TheChar}"
Style="{StaticResource Flat}"
ToolTip="{Binding AggregatedTooltip}"
ToolTipService.BetweenShowDelay="300"
ToolTipService.InitialShowDelay="800"
ToolTipService.IsEnabled="True"
ToolTipService.ShowDuration="10000"
ToolTipService.ShowOnDisabled="True" />
</Border>
</DataTemplate>
The Border around your ItemsControl in your ScrollViewer would have a Padding="1" and look like this:
<ScrollViewer x:Name="ScrollViewer"
Grid.Row="1"
Margin="10,0,10,10"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Border
Padding="1"
SnapsToDevicePixels="True">
<ItemsControl
Name="CharItemsControl"
ItemTemplate="{StaticResource ItemsTemplate}"
ItemsSource="{Binding CharDescriptions}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Border>
</ScrollViewer>

WPF nested ItemsControl binding issue

Why is the second ItemsControl binding to FirstName not working?
<ItemsControl Grid.Row="1" x:Name="PMItmsControl" VerticalAlignment="Top" ItemsSource="{Binding FirstEntityCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border CornerRadius="5" BorderBrush="YellowGreen" BorderThickness="1" Margin="5">
<Grid Width="auto" Height="100" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60*"/>
<ColumnDefinition Width="40*"/>
</Grid.ColumnDefinitions>
<TextBlock **x:Name="tb"** Width="100" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Text="{Binding FirstName}" FontSize="10" >
</TextBlock>
<ItemsControl ItemsSource="{Binding SecondEntityCollection}" Grid.Column="1" VerticalAlignment="Top">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding SecondName}" Margin="5" Foreground="White" />
<TextBlock Grid.Column="1" Text="{Binding **Path= Text, ElementName=tb**}" Margin="5" Width="40">
</TextBlock>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
and in my code behind, i declare following classes:
public class FirstEntity
{
public string FirstName { get; set; }
public ObservableCollection<SecondEntity> SecondEntityCollection { get; set; }
}
public class SecondEntity
{
public string SecondName { get; set; }
}
and the FirstEntityCollection is declare as following in my code behind:
public ObservableCollection<FirstEntity> FirstEntityCollection {get;set;}
not sure if i make my issue clear?
can someone give me any idea? please.
thanks much. very appreciate.
updated: I just found another solution use x:Name="tb" to above Bold code. and then changes Text="{Binding Path= DataContext.FirstName, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ItemsControl},AncestorLevel=2}}" to Text="{Binding Text,ElementName=tb}"
thanks Maxi.
----------------updated again-------------
this is the first time I asked question in stackoverflow, the question editor is not easy to input code and comments and the formatting is so bad, maybe I don't find a easy way to use. anyway thanks.
The first mess-up you need to get rid of is setting Foreground="White" on the TextBlock.
Based on your question update, please find below XAML bindings.
<ItemsControl Grid.Row="1" x:Name="PMItmsControl" VerticalAlignment="Top" ItemsSource="{Binding FirstEntityCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border CornerRadius="5" BorderBrush="YellowGreen" BorderThickness="1" Margin="5">
<Grid Width="auto" Height="100" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60*"/>
<ColumnDefinition Width="40*"/>
</Grid.ColumnDefinitions>
<TextBlock Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Text="{Binding FirstName}" FontSize="10" >
</TextBlock>
<ItemsControl ItemsSource="{Binding SecondEntityCollection}" Grid.Column="1" VerticalAlignment="Top">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding SecondName}" Margin="5" />
<!--Notice setting DataContext to TextBlock below-->
<TextBlock Grid.Column="1" DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ItemsControl}}}" Text="{Binding Path= FirstName}" Margin="5" Width="Auto"/> </Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Hope this give you an idea to start with...

Make attached properties work inside DataTemplate

I got an ItemsControl which uses a Canvas as ItemsPanel and its items are rendered to different WPF shapes depending on the bound type, basically like this:
<ItemsControl ItemsSource="{Binding PreviewShapes}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:UiPreviewLineViewModel}">
<Line X1="{Binding Start.X}" Y1="{Binding Start.Y}"
X2="{Binding End.X}" Y2="{Binding End.Y}"
StrokeThickness="0.75" Stroke="{Binding Brush}" x:Name="Line" ToolTip="{Binding Text}">
</Line>
</DataTemplate>
<DataTemplate DataType="{x:Type local:UiPreviewEllipsisViewModel}">
<Ellipse Canvas.Left="{Binding UpperLeft.X" Canvas.Top="{Binding UpperLeft.Y}"
Width="{Binding Width}" Height="{Binding Height}"
StrokeThickness="0.75" Stroke="{Binding Brush}" x:Name="Ellipse" ToolTip="{Binding Text}">
</Ellipse>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="SketchCanvas" ClipToBounds="False">
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
So I basically add objects to PreviewShapes of the viewmodel and depending on the type they are rendered to WPF Lines or Ellipses. That basically works but the attached properties Canvas.Left and Canvas.Top are ignored, even when using static values.
Also VS or ReSharper notifies me that the attached property has no effect in the current context.
How can I position the Ellipse on the canvas without using the attached properties? Or what other solution would be appropiate?
Unfortunately nobody felt like posting an answer.
First, Clemens links are helpful. The items will be inside a ContentPresenter which is the reason why setting Canvas.Left/Top on the Ellipsis does not work.
Solution 1
By adding a style to the item container the bindings for the position can be set there:
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding UpperLeft.X}" />
<Setter Property="Canvas.Top" Value="{Binding UpperLeft.Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
This works but the DataTemplate placing <Line> will produce binding warnings because that view model does not have a property UpperLeft. Nevertheless it works for the ellipsis and the lines are placed by their X1, Y1, X2 and Y2 values.
Solution 2
If you would like to use a more fine grained control approach you can set the attached Canvas properties to the ContentPresenter by proxing them with a custom behaviour / attached property. Let's name it CanvasPointProxyBehavior, you could use it for the Ellipse like this:
<DataTemplate DataType="{x:Type local:UiPreviewEllipsisViewModel}">
<Ellipse behaviors:CanvasPointProxyBehavior.Point="{Binding UpperLeft}"
Width="{Binding Width}" Height="{Binding Height}"
StrokeThickness="0.75" Stroke="{Binding Brush}" x:Name="Ellipse" ToolTip="{Binding Text}">
</Ellipse>
</DataTemplate>
The Line will not need it. The code for this attached property might look like this:
public class CanvasPointProxyBehavior
{
public static readonly DependencyProperty PointProperty = DependencyProperty.RegisterAttached("Point", typeof(Point), typeof(CanvasPointProxyBehavior), new UIPropertyMetadata(null, PointChangedCallback));
public static void SetPoint(DependencyObject depObj, Point point)
{
depObj.SetValue(PointProperty, point);
}
public static Point GetPoint(DependencyObject depObj)
{
return depObj.GetValue(PointProperty) as Point;
}
private static void PointChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
UIElement uiElement = (dependencyObject as UIElement);
if (uiElement == null) return;
UIElement elementToBePositioned = uiElement;
var visualParent = VisualTreeHelper.GetParent(uiElement);
if (visualParent is ContentPresenter)
{
elementToBePositioned = visualParent as ContentPresenter;
}
var point = e.NewValue as Point;
if (point != null)
{
Canvas.SetLeft(elementToBePositioned, point.X);
Canvas.SetTop(elementToBePositioned, point.Y);
}
}
}
Hoping someone will find one or both solution useful.
Please note that I encountered the same ReSharper warning message as #ZoolWay, but in my case it was within a data grid, where I wanted the button on the right to be right-aligned instead of left-aligned:
Attached property setting "Grid.Column" has no effect in current
context and can be removed.
Here is the code where I had the warning, on the Button Grid.Column="2":
<Border VerticalAlignment="Center" Style="{DynamicResource InspectionsCustomDataGridHeader}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="10*"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<Label Name="InspectionsDataGridTitle" Background="White" Content="{Binding InspectionsCollectionView.View.Count, ConverterParameter={x:Static resx:Resources.InspectionsDataGridTitle}, Converter={StaticResource DataGridHeaderCountConverter}}" Style="{DynamicResource InspectionsCustomDataGridHeaderLabel}" MouseDown="InspectionsDataGridTitle_MouseDown" />
<!-- more labels, etc... -->
<Button Grid.Column="2" Width="30" Height="30" Margin="0,0,10,0" Background="Transparent" BorderThickness="0" HorizontalAlignment="Right" Command="{Binding CreatePDFCommand,Mode=OneWay}" >
<StackPanel Orientation="Horizontal" Background="Transparent">
<Image Width="30" Height="30" Source="/Inspections;component/Icons/pdf_btn.png" />
</StackPanel>
</Button>
</StackPanel>
</Grid>
</Border>
This is how I fixed the warning, where I moved the button below the first StackPanel:
<Border VerticalAlignment="Center" Style="{DynamicResource InspectionsCustomDataGridHeader}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<Label Name="InspectionsDataGridTitle" Background="White" Content="{Binding InspectionsCollectionView.View.Count, ConverterParameter={x:Static resx:Resources.InspectionsDataGridTitle}, Converter={StaticResource DataGridHeaderCountConverter}}" Style="{DynamicResource InspectionsCustomDataGridHeaderLabel}" MouseDown="InspectionsDataGridTitle_MouseDown" />
<!-- more labels, etc... -->
</StackPanel>
<Button Grid.Column="2" Width="30" Height="30" Margin="0,0,10,0" Background="Transparent" BorderThickness="0" HorizontalAlignment="Right" Command="{Binding CreatePDFCommand,Mode=OneWay}" >
<StackPanel Orientation="Horizontal" Background="Transparent">
<Image Width="30" Height="30" Source="/Inspections;component/Icons/pdf_btn.png" />
</StackPanel>
</Button>
</Grid>
</Border>
HTH

Cannot associate CarouselNavigator with CarouselPanel when CarouselPanel is a child element within CarouselItemsControl ItemsPanelTemplate

i have xaml layout as shown below
<DockPanel Grid.Row="1" Grid.ColumnSpan="2">
<dxca:CarouselItemsControl x:Name="carouselItemsControl" ItemsSource="{Binding ImageList}" DockPanel.Dock="Top">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<dxca:CarouselPanel x:Name="carousel" AttractorPointIndex="2" IsRepeat="True" PathSizingMode="Proportional" FirstVisibleItemIndex="0" Height="Auto" ItemSize="180,120" IsAutoSizeItem="True" VisibleItemCount="5" ActiveItemIndex="1" AnimationTime="500" IsInvertedDirection="True" PathVisible="False">
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<Image Source="{Binding Data.Data}" Width="480" Height="320" Margin="10"/>
<TextBlock Canvas.Left="100" Canvas.Top="10" Text="{Binding Identifier}" FontSize="12" FontWeight="Bold"></TextBlock>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</dxca:CarouselItemsControl>
<dxca:CarouselNavigator DockPanel.Dock="Bottom" Carousel="< TODO : BINDING EXPRESSION TO BIND TO CarouselPanel>" Width="300" Height="40" />
</DockPanel>
How do i bind the CarouselNavigator to the carousel object in the carousel property using xaml only?
Just use the CarouselItemsControl.Carousel property for this purpose:
<dxca:CarouselNavigator Carousel="{Binding Path=Carousel, ElementName=carouselItemsControl}" />

WPF Access grid items onClick

I'm new to wpf and have what I hope is a simple question.
I have created a simple template to display some grouped items
Currently when I click on one of the items ItemsClick is called (as expected)
private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
{
MyCustomClass selectedItem = (MyCustomClass)e.ClickedItem;
GridView g = (GridView)sender;
I can get a copy of the class object by casting e.ClickedItem and the grid from sender. But what I don't see is how to I get a reference to the custom items I added in the template for example if I wanted to change the text in the testName TextBlock ?
Template:
<DataTemplate x:Key="MyTestTemplate">
<Grid HorizontalAlignment="Left" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Grid.RowSpan="3">
<Image Source="{Binding ThumbnailUrl}" Stretch="Fill" Width="175" Height="175" />
</Border>
<Image Source="{Binding MyCustomImage}" Height="30" Width="30" Grid.RowSpan="3" Margin="0,0,0,30"/>
<Grid x:Name="ItemDetails" VerticalAlignment="Bottom" Height="75" Margin="0,0,0,2" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="testName" Text="{Binding Name}" Grid.Row="0" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" MaxWidth="100" MaxHeight="80" TextWrapping="Wrap" HorizontalAlignment="Left" Style="{StaticResource TitleTextStyle}" Margin="5,0,5,0"/>
<TextBlock Text="{Binding Address}" Grid.Row="0" HorizontalAlignment="Right" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource
CaptionTextStyle}" TextWrapping="NoWrap" Margin="5,0,5,0" />
</Grid>
</Grid>
</DataTemplate>
<GridView
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Grouped Items"
Grid.RowSpan="2"
Padding="116,137,40,46"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
ItemTemplate="{StaticResource MyTestTemplate}"
SelectionMode="None"
IsSwipeEnabled="True"
IsItemClickEnabled="True"
RightTapped="itemGridView_RightTapped"
ItemClick="itemGridView_ItemClick" SelectionChanged="itemGridView_SelectionChanged">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
AutomationProperties.Name="Group Title"
>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding state}"/>
</StackPanel>
</Button>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
Ok I found the answer on msdn.
The link here http://msdn.microsoft.com/en-us/library/bb613579.aspx
The basic steps
get the selected item (I was responding to click so this info was easy), find the ContentPresenter within that Item, and then call FindName on the DataTemplate that is set on that ContentPresenter.

Resources