How to create a main layout template in XAML - xaml

I'm new to windows development. I'm making a simple windows app which has a few pages and each page has a similar layout in XAML. Like this:
Each page is separated into 3 sections. A will have a title, B is where the content will be inserted and C is for other stuff. My question is: what is the simplest way to build a general layout template so that I can reuse it for every page? And is it possible?
For example, I have a MainTemplate.xaml file with this layout:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
And then my Page1.xaml will load MainTemplate so I don't have to copy and paste the same layout into every page of mine. I've tried looking online but the solutions are going way over my head. I was wondering if there's a simple way to do this like with webpages.Thanks a lot.

One feasible way to do this is using UserControl with ContentPresenter. For example:
Add a UserControl named MainTemplate. In the XAML, set the layout with ContentPresenter and bind it to the DependencyProperty defined in code-behind.
<UserControl x:Class="UWPTest.MainTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:UWPTest"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ContentPresenter Content="{x:Bind Title}" />
<ContentPresenter Grid.Row="1" Content="{x:Bind Main}" />
<ContentPresenter Grid.Row="2" Content="{x:Bind Stuff}" />
</Grid>
</UserControl>
In the code-behind, set the DependencyProperty so that we can use them to set the content in other pages.
public sealed partial class MainTemplate : UserControl
{
public MainTemplate()
{
this.InitializeComponent();
}
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(object), typeof(MainTemplate), new PropertyMetadata(null));
public object Title
{
get { return GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty MainProperty = DependencyProperty.Register("Main", typeof(object), typeof(MainTemplate), new PropertyMetadata(null));
public object Main
{
get { return GetValue(MainProperty); }
set { SetValue(MainProperty, value); }
}
public static readonly DependencyProperty StuffProperty = DependencyProperty.Register("Stuff", typeof(object), typeof(MainTemplate), new PropertyMetadata(null));
public object Stuff
{
get { return GetValue(StuffProperty); }
set { SetValue(StuffProperty, value); }
}
}
After this, we can use the UserControl in other pages to reuse the general layout. For example, using it in "MainPage.xaml":
<Page x:Class="UWPTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:UWPTest"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<local:MainTemplate>
<local:MainTemplate.Title>
<Grid Background="Red">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="60">A</TextBlock>
</Grid>
</local:MainTemplate.Title>
<local:MainTemplate.Main>
<Grid Background="Green">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="60">B</TextBlock>
</Grid>
</local:MainTemplate.Main>
<local:MainTemplate.Stuff>
<Grid Background="Yellow">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="60">C</TextBlock>
</Grid>
</local:MainTemplate.Stuff>
</local:MainTemplate>
</Page>
Then the "MainPage" will look like follwoing:

Related

Contentcontrol not updating Text

I have a View that contains a lot of input parameters. Each parameter consists of a label (name of the parameter) a textbox for its value and another label for its units (eg. kNm).
The Control displays in a horizontal Stackpanel.
When changing the Text (ToWay Binding) of the Textbox the underlying Property (eg. DesignLife) does not update anymore, since I switched from straight WPF (OnPropertyChanged) to MVVM Light (RaisePropertyChanged).
Here are some relevant pieces of code:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace xxxx.Views.CustomControls
{
public class InputParameter : ContentControl
{
static InputParameter()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(InputParameter), new FrameworkPropertyMetadata(typeof(InputParameter)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
// some properties left out
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(InputParameter));
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
}
}
<UserControl
x:Class="xxxx.Views.MainUserControls.Column"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:xxxx.Views.CustomControls"
xmlns:cf="clr-namespace:xxxx.Model.Config"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tt="clr-namespace:xxxx.Properties"
DataContext="{Binding Main.Calc, Source={StaticResource Locator}}"
mc:Ignorable="d">
<GroupBox Margin="3">
<StackPanel Orientation="Vertical">
<!-- Stuff left out -->
<cc:InputParameter
NameContent="Design life"
Text="{Binding DesignLife, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, StringFormat=N0}"
UnitContent="years" />
<!-- Stuff left out -->
</StackPanel>
</GroupBox>
</UserControl>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:CoCa.Views.CustomControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Style x:Key="InputParm" TargetType="{x:Type cc:InputParameter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type cc:InputParameter}">
<StackPanel Style="{StaticResource SPanel}">
<Label
x:Name="PART_NameLabel"
Width="130"
HorizontalContentAlignment="Left"
Content="{TemplateBinding NameContent}"
Foreground="{TemplateBinding Foreground}"
Style="{StaticResource LabelStyle1}"
ToolTip="{TemplateBinding ToolTip}" />
<TextBox
x:Name="PART_TextBox"
Width="70"
Background="{TemplateBinding TbBackground}"
Foreground="{TemplateBinding Foreground}"
Style="{StaticResource Tb}"
Text="{TemplateBinding Property=Text}" />
<Label
x:Name="PART_UnitLabel"
Content="{TemplateBinding UnitContent}"
Foreground="{Binding ElementName=PART_NameLabel, Path=Foreground}"
Style="{StaticResource Unit}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
private int _designLife;
public int DesignLife
{
get => _designLife;
set
{
Set<int>(() => DesignLife, ref _designLife, value, true);
}
}
I suspect that RaisePropertyChanged does not get called because it is not used anywhere in the class InputParameter's Text DependencyProperty. (Maybe under the hood it does?)
Questions:
1: Is the way I made InputParameter the way to go?
2: Is my suspicion correct?
3 a: If so, How do I get RaisePropertyChanged called?
b: If not: what could be the problem?

XamlParseException when trying to bind subclass Loaded event handler

I am having these same problems in my Windows Phone Project:
XamlParseException when adding event handler in XAML
http://social.msdn.microsoft.com/Forums/vstudio/en-US/7624b2fc-0dea-415f-a71d-1739020d9d2e/xamlparseexception-when-trying-to-bind-subclass-loaded-event-handler?forum=wpf
However, it seems like my handler does have the correct signature?
Even stranger, I did the same thing with the UIElement_Tap event, in another page and it works fine.
MainPage.xaml
<v:PageBase
x:Class="PhoneApp1.Views.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:v="clr-namespace:PhoneApp1.Views"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ItemsControl Grid.Row="0"
Loaded="ItemsControl_Loaded"
Margin="0,0,0,120">
<TextBlock Text="Item1"/>
<TextBlock Text="Item2"/>
<TextBlock Text="Item3"/>
</ItemsControl>
<ItemsControl Grid.Row="1"
Loaded="ItemsControl_Loaded"
Margin="0,120,0,0">
<TextBlock Text="Item1"/>
<TextBlock Text="Item2"/>
<TextBlock Text="Item3"/>
<TextBlock Text="Item4"/>
</ItemsControl>
</Grid>
</v:PageBase>
MainPage.xaml.cs
namespace PhoneApp1.Views
{
public partial class MainPage : PageBase
{
// Constructor
public MainPage()
{
InitializeComponent();
}
}
}
PageBase.cs
using Microsoft.Phone.Controls;
namespace PhoneApp1.Views
{
// Common code across pages.
public class PageBase : PhoneApplicationPage
{
protected virtual void ItemsControl_Loaded(object sender, System.Windows.RoutedEventArgs e )
{
//...
}
}
}
I know I could override the method and call the base type in my MainPage, but that defeats the purpose of having a common base, no?
This is how I ended up fixing this:
Gave the itemscontrol a name and wired up the event in the MainPage constructor:
itemControl1.Loaded += base.ItemsControl_Loaded;
Try to replace System.Windows.RoutedEventArgs with EventArgs

Placing PopUp, which is inside a Itemscontrol

I've got an itemscontrol, in which all my elements are PopUps. The problem is I do not know how to place them compared to the grid in which the itemscontrol is placed. I've tried using horizontal- and vertical alignment but this does not help. The xaml code I've got so far is:
<Grid>
<ItemsControl Grid.Row="1" VerticalContentAlignment="Top" HorizontalAlignment="Left" ItemsSource="{Binding PopUp}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ViewModel:PopUpTemplateSelector.EndTurnMenuTemplate>
<DataTemplate>
<Popup IsOpen="{Binding PU.IsOpen}" HorizontalAlignment="Left" VerticalAlignment="Top">
<Design:InGame_NextTurn_UserControl/>
</Popup>
</DataTemplate>
</ViewModel:PopUpTemplateSelector.EndTurnMenuTemplate>
</ViewModel:PopUpTemplateSelector>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
I would suggest maybe using the following overlay method instead.
From this Nokia page:
Overlay XMAL:
<UserControl x:Class="WindowsPhoneSample7.OverLay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
d:DesignHeight="800" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="400"/>
<RowDefinition Height="400"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1">
<ProgressBar IsIndeterminate="True" Foreground="Orange" Height="50" Width="480" VerticalAlignment="Center"/>
<TextBlock Text="Wait" Foreground="Orange" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</UserControl>
Overlay code-behind:
public OverLay()
{
InitializeComponent();
this.LayoutRoot.Height = Application.Current.Host.Content.ActualHeight;
this.LayoutRoot.Width = Application.Current.Host.Content.ActualWidth;
SystemTray.IsVisible = false; //to hide system tray
}
MainPage code-behind:
public partial class MainPage : PhoneApplicationPage
{
private Popup popup;
// Constructor
public MainPage()
{
InitializeComponent();
this.popup = new Popup();
}
private void BtnStart_Click(object sender, RoutedEventArgs e)
{
this.LayoutRoot.Opacity = 0.2;
OverLay ovr = new OverLay();
this.popup.Child = ovr;
this.popup.IsOpen = true;
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s, a) =>
{
Thread.Sleep(5000);
};
worker.RunWorkerCompleted += (s, a) =>
{
popup.IsOpen = false;
this.LayoutRoot.Opacity = 1.0;
};
worker.RunWorkerAsync();
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
this.popup.IsOpen = false;
}
}

Developing CustomControl and properties

i'm seeking for information - videos, tutorials, books etc. for developing custom controls like http://www.telerik.com/
It means i want to develop my custom control, lets take for example - Expander.
This is my expander code:
<UserControl x:Class="PhoneApp16.Expander"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Rectangle Fill="Wheat"/>
<StackPanel Grid.Row="1"/>
</Grid>
This is how it looks on main form:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<local:Expander HorizontalAlignment="Left" VerticalAlignment="Top" Width="456"/>
</Grid>
But i want to add my own properties like:
IsExpanded=true/false which sets Expanders StackPanel visibility to visible or collapsed
I know about ValueConverters but how to achieve this property in XAML of my expander so it looked like:
<local:Expander IsExpanded="false" HorizontalAlignment="Left" VerticalAlignment="Top" Width="456"/>
Links to books, videos etc. are appreciated - best of all from the beginning ( for dummies );
You have to add DependencyProperties in code benind of your user control. For example imagine that following XAML is your user control:
<UserControl
x:Class="MyProject.Data.Controls.Elements.Editors.Select"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Root">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border
Grid.Column="0"
CornerRadius="0,0,0,0">
<TextBlock
x:Name="MyBlock"
TextTrimming="CharacterEllipsis"
Text="{Binding ElementName=Root, Path=Label, Mode=OneWay}"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
FontSize="12"
TextAlignment="Right"
FontFamily="Segoe UI Semibold"
Margin="4,0,4,0"/>
</Border>
</Grid>
</UserControl>
Here is its code behind:
public partial class Select : UserControl
{
/// <summary>
///
/// </summary>
public Select()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
/// <summary>
/// Identifies the <see cref="Label"/> dependency property.
/// </summary>
public static readonly DependencyProperty LabelProperty =
DependencyProperty.Register("Label", typeof(string), typeof(Select), new PropertyMetadata(string.Empty));
}
Now you can set Label property of the control in your form. When you set it this value goes to the Text property of the TextBlock named MyBlock.
It is important to create dependency properties otherwise bindings will not work.
Here is good reference book:
WPF Control Development Unleashed: Building Advanced User Experiences
Besides MSDN has several video and training materials:
http://msdn.microsoft.com/en-us/vstudio/aa496123

How to Make Content Hidden in Expander Windows Phone Control

I am playing around with an example of Expander Control from Windows Phone Toolkit (I am using it for wp7).
When I load up the stripped down version everything seems expanded. When I click on Customer Pizza or 2 nothing happens. I would like the sub stuff to be collapsed but I don't know how.
<phone:PhoneApplicationPage
x:Class="ExpanderViewSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<toolkit:RelativeTimeConverter x:Key="RelativeTimeConverter"/>
<DataTemplate x:Key="CustomHeaderTemplate">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Stretch="None"/>
<TextBlock Text="{Binding Name}"
FontSize="{StaticResource PhoneFontSizeExtraLarge}"
FontFamily="{StaticResource PhoneFontFamilySemiLight}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="CustomExpanderTemplate">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Stretch="None"/>
<TextBlock Foreground="{StaticResource PhoneSubtleBrush}" VerticalAlignment="Center"
FontSize="{StaticResource PhoneFontSizeNormal}">
<TextBlock.Text>
<Binding Path="DateAdded" Converter="{StaticResource RelativeTimeConverter}" StringFormat="Date added: {0}" />
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="WindowsPhoneGeek.com" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="ExpanderViewSample" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle2Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox Grid.Row="0" x:Name="listBox">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<toolkit:ExpanderView Header="{Binding}" Expander="{Binding}"
IsExpanded="False"
HeaderTemplate="{StaticResource CustomHeaderTemplate}"
ExpanderTemplate="{StaticResource CustomExpanderTemplate}"></toolkit:ExpanderView>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
List<CustomPizza> customPizzas = new List<CustomPizza>()
{
new CustomPizza()
{
Name = "Custom Pizza 1",
IsExpanded = false,
DateAdded = new DateTime(2010, 7, 8),
Image="Images/pizza1.png"
},
new CustomPizza() { Name = "Custom Pizza 2", DateAdded = new DateTime(2011, 2, 10), Image="Images/pizza2.png"}
};
this.listBox.ItemsSource = customPizzas;
// Important properties:
// IsExpanded
// Header
// Expander
// ItemsSource
// HeaderTemplate
// ExpanderTemplate
// ItemTemplate
// NonExpandableHeader
// IsNonExpandable
// NonExpandableHeaderTemplate
}
}
public class CustomPizza : INotifyPropertyChanged
{
private bool isExpanded;
public string Image
{
get;
set;
}
public string Name
{
get;
set;
}
public DateTime DateAdded
{
get;
set;
}
public bool IsExpanded
{
get
{
return this.isExpanded;
}
set
{
if (this.isExpanded != value)
{
this.isExpanded = value;
this.OnPropertyChanged("IsExpanded");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I also don't get what this really is for
ExpanderView Header="{Binding}" Expander="{Binding}"
I don't get what "binding" is referring too. It just seems to know which data to use but I don't know how it knows.
To change the expanded state of the expander view you can do the following
-register for tap event and add binding to IsExpanded (this will bind to the IsExpanded property of CustomPizza)
<toolkit:ExpanderView Header="{Binding}" Expander="{Binding}"
IsExpanded="{Binding IsExpanded}"
HeaderTemplate="{StaticResource CustomHeaderTemplate}"
ExpanderTemplate="{StaticResource CustomExpanderTemplate}"
Tap="expander_OnTap"></toolkit:ExpanderView>
-in the tap event switch the IsExpanded flag of the CustomPizza:
private void expander_OnTap(object sender, System.Windows.Input.GestureEventArgs e)
{
ExpanderView expander = sender as ExpanderView;
CustomPizza customPizza = expander.DataContext as CustomPizza;
customPizza.IsExpanded = !customPizza.IsExpanded;
}
Regarding the question about ExpanderView Header="{Binding}" Expander="{Binding}", when you set (or bind) the ItemsSource property of an ItemsControl to a list (ListBox is inheriting from a ItemsControl), the DataTemplate inside the ItemTemplate will be automatically set to each individual item. For example here you are setting it to a List of CustomPizza so each ItemTemplate DataContext will be a CustomPiza. So the ExpanderView will have the CustomPizza as DataContext. {Binding} will just pass the DataContext so like this whatever is inside the HEaderTemplate will get the same DataContext (CustomPizza ). If you had put {Binding Image} then the HeaderTemplate will just have the Image string as DataContext.