Created
December 11, 2012 05:57
-
-
Save kenji4569/4256217 to your computer and use it in GitHub Desktop.
Breadcrumbs; My first winrt custom control.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////// | |
// In SamplePage.xaml | |
////////////////////////////////////////////////////// | |
<local:Breadcrumbs Height="25" MaxWidth="475" | |
ItemsSource="{Binding Source={StaticResource sampleBreadcrumbsViewSource}}" | |
> | |
<local:Breadcrumbs.ItemTemplate> | |
<DataTemplate> | |
<Button VerticalAlignment="Top" Margin="0,0,5,0" | |
MaxWidth="128" | |
Content="{Binding}" | |
BorderBrush="{x:Null}" | |
Click="BreadcrumbsItem_Click"> | |
</Button> | |
</DataTemplate> | |
</local:Breadcrumbs.ItemTemplate> | |
<local:Breadcrumbs.SeparatorTemplate> | |
<DataTemplate> | |
<TextBlock Text=">>" Margin="5,2,5,0"/> | |
</DataTemplate> | |
</local:Breadcrumbs.SeparatorTemplate> | |
</local:Breadcrumbs> | |
////////////////////////////////////////////////////// | |
// In SamplePage.xaml.cs | |
////////////////////////////////////////////////////// | |
private void BreadcrumbsItem_Click(object sender, RoutedEventArgs e) | |
{ | |
var item = ((FrameworkElement)e.OriginalSource).DataContext; | |
// ... | |
} | |
////////////////////////////////////////////////////// | |
// In Breadcrumbs.cs | |
////////////////////////////////////////////////////// | |
public sealed class Breadcrumbs : Control | |
{ | |
private ObservableCollection<FrameworkElement> _items; | |
private ItemsControl _itemContainerList; | |
public ObservableCollection<FrameworkElement> Items | |
{ | |
get { return _items; } | |
} | |
public Breadcrumbs() | |
{ | |
this.DefaultStyleKey = typeof(Breadcrumbs); | |
_items = new ObservableCollection<FrameworkElement>(); | |
} | |
#region ItemsSource | |
public object ItemsSource | |
{ | |
get { return (object)GetValue(ItemsSourceProperty); } | |
set { SetValue(ItemsSourceProperty, value); } | |
} | |
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", | |
typeof(object), | |
typeof(Breadcrumbs), | |
new PropertyMetadata(null, OnItemsSourcePropertyChanged)); | |
private static void OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
var breadcrumbs = (Breadcrumbs)d; | |
if (breadcrumbs.ItemsSource == null || | |
breadcrumbs.SeparatorTemplate == null || | |
breadcrumbs.ItemTemplate == null) | |
{ | |
return; | |
} | |
breadcrumbs.Items.Clear(); | |
var itemSource = (IList<object>)breadcrumbs.ItemsSource; | |
int cnt = itemSource.Count(); | |
if (cnt == 0) | |
{ | |
return; | |
} | |
for (int i = 0; i < cnt - 1; ++i) | |
{ | |
FrameworkElement itemElement = (FrameworkElement)breadcrumbs.ItemTemplate.LoadContent(); | |
itemElement.DataContext = itemSource[i]; | |
breadcrumbs.Items.Add(itemElement); | |
FrameworkElement separatorElement = (FrameworkElement)breadcrumbs.SeparatorTemplate.LoadContent(); | |
separatorElement.DataContext = itemSource[i]; | |
breadcrumbs.Items.Add(separatorElement); | |
} | |
FrameworkElement lastItemElement = (FrameworkElement)breadcrumbs.ItemTemplate.LoadContent(); | |
lastItemElement.DataContext = itemSource[cnt - 1]; | |
breadcrumbs.Items.Add(lastItemElement); | |
} | |
#endregion | |
#region ItemTemplate | |
public DataTemplate ItemTemplate | |
{ | |
get { return (DataTemplate)GetValue(ItemTemplateProperty); } | |
set { SetValue(ItemTemplateProperty, value); } | |
} | |
public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", | |
typeof(DataTemplate), | |
typeof(Breadcrumbs), | |
new PropertyMetadata(null, OnItemsSourcePropertyChanged)); | |
#endregion | |
#region SeparatorTemplate | |
public DataTemplate SeparatorTemplate | |
{ | |
get { return (DataTemplate)GetValue(SeparatorTemplateProperty); } | |
set { SetValue(SeparatorTemplateProperty, value); } | |
} | |
public static readonly DependencyProperty SeparatorTemplateProperty = DependencyProperty.Register("SeparatorTemplate", | |
typeof(DataTemplate), | |
typeof(Breadcrumbs), | |
new PropertyMetadata(null, OnItemsSourcePropertyChanged)); | |
#endregion | |
protected override void OnApplyTemplate() | |
{ | |
base.OnApplyTemplate(); | |
_itemContainerList = GetTemplateChild("ItemList") as ItemsControl; | |
_itemContainerList.ItemsSource = _items; | |
} | |
} | |
////////////////////////////////////////////////////// | |
// In Generic.xaml | |
////////////////////////////////////////////////////// | |
<Style TargetType="local:Breadcrumbs"> | |
<Setter Property="VerticalAlignment" Value="Top" /> | |
<Setter Property="HorizontalAlignment" Value="Left" /> | |
<Setter Property="Template"> | |
<Setter.Value> | |
<ControlTemplate TargetType="local:Breadcrumbs"> | |
<Border Background="{TemplateBinding Background}" | |
BorderBrush="{TemplateBinding BorderBrush}" | |
BorderThickness="{TemplateBinding BorderThickness}"> | |
<ItemsControl x:Name="ItemList" TabNavigation="Cycle" Margin="5,0,5,0"> | |
<ItemsControl.ItemsPanel> | |
<ItemsPanelTemplate> | |
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right"/> | |
</ItemsPanelTemplate> | |
</ItemsControl.ItemsPanel> | |
</ItemsControl> | |
</Border> | |
</ControlTemplate> | |
</Setter.Value> | |
</Setter> | |
</Style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment