Skip to content

Instantly share code, notes, and snippets.

@jsonw23
jsonw23 / Contact.cs
Created April 15, 2011 04:24
Example Contact Form to illustrate tricky model binding situations in ASP.NET MVC 3
using System.Collections.Generic;
namespace GeekJ.Examples.ContactForm.Models
{
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
@jsonw23
jsonw23 / Drive.cs
Created February 27, 2012 20:39
Folder Tree WPF Control: Part 1 - View Model
namespace GeekJ.FolderTreeControl.Model
{
public class Drive : FolderTreeItem
{
private DriveInfo _driveInfo;
public DriveInfo DriveInfo
{
get
{
return _driveInfo;
@jsonw23
jsonw23 / FolderTree.xaml
Created February 27, 2012 20:42
Folder Tree WPF Control: Part 1 - View
<UserControl x:Class="GeekJ.FolderTreeControl.FolderTree"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Loaded="FolderTree_Loaded">
<Grid>
<TreeView ItemsSource="{Binding Path=Drives}">
@jsonw23
jsonw23 / FolderTree.xaml
Created February 27, 2012 21:32
Folder Tree WPF Control: Part 1 - Lazy Loading
<TreeView ItemsSource="{Binding Path=Drives}" TreeViewItem.Expanded="FolderTree_Expanded">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Folders}">
<TextBlock Text="{Binding Path=Label}" />
</HierarchicalDataTemplate>
@jsonw23
jsonw23 / FolderTree.xaml
Created February 27, 2012 21:51
Folder Tree WPF Control: Part 1 - Factored out Data Templates
<UserControl x:Class="GeekJ.FolderTreeControl.FolderTree"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:model="clr-namespace:GeekJ.FolderTreeControl.Model"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Loaded="FolderTree_Loaded" DataContextChanged="FolderTree_DataContextChanged">
<UserControl.Resources>
@jsonw23
jsonw23 / MainWindow.xaml
Created February 27, 2012 22:04
Folder Tree WPF Control: Part 1 - Testing the Control
<Window x:Class="Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ftc="clr-namespace:GeekJ.FolderTreeControl;assembly=GeekJ.FolderTreeControl"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ftc:FolderTree />
</Grid>
</Window>
@jsonw23
jsonw23 / FolderTree.xaml.cs
Created March 1, 2012 02:43
Folder Tree WPF Control: Part 2 - Folder Selection Model
namespace GeekJ.FolderTreeControl
{
public partial class FolderTree : UserControl
{
private FolderTreeSelection _selection = new FolderTreeSelection();
public FolderTreeSelection Selection
{
get { return _selection; }
set
{
@jsonw23
jsonw23 / FolderTreeSelection.cs
Created April 26, 2012 02:10
Folder Tree WPF Control: Part 2 - Recursive Checkbox Behavior
public void Add(FolderTreeItem folderTreeItem, bool include = true)
{
if (folderTreeItem.SelectionItem == null && include)
{
// node is checked for the first time, add a selection item
folderTreeItem.SelectionItem = new Item() { TreeItem = folderTreeItem, Include = include };
items.Add(folderTreeItem.SelectionItem);
}
else if (folderTreeItem.SelectionItem != null && folderTreeItem.SelectionItem.Include != include)
{
@jsonw23
jsonw23 / FolderTree.xaml.cs
Created April 27, 2012 01:44
Folder Tree WPF Control: Part 2 - Selection Events
public partial class FolderTree : UserControl
{
private FolderTreeSelection _selection;
public FolderTreeSelection Selection
{
get { return _selection; }
set
{
_selection = value;
_selection.Changed += new EventHandler(OnSelectionChanged);
@jsonw23
jsonw23 / FolderTreeSelection-1.cs
Created May 2, 2012 04:17
Folder Tree WPF Control: Part 3 - Enumeration
public class FolderTreeSelection : ViewModelBase
{
private Dictionary<Item, long> itemCounts = new Dictionary<Item, long>();
public long FolderCount {
get
{
return itemCounts.Sum(x => x.Value);
}
}