Add generic category window to WPF library

This commit is contained in:
2015-11-10 20:28:58 -05:00
parent 8e24cd0b12
commit f7ddae093e
5 changed files with 216 additions and 0 deletions

View File

@@ -158,6 +158,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CheckedListItem.cs" />
<Compile Include="Extensions\IconExtensions.cs" />
<Compile Include="Resource\EnumToResourceConverter.cs" />
<Compile Include="ExtendedListBoxControl\ExtendedListBox.cs" />
<Compile Include="ExtendedListBoxControl\ExtendedListBoxItem.cs" />
@@ -195,6 +196,10 @@
</Compile>
<Compile Include="Toolbar\SplitButton\SplitButton.cs" />
<Compile Include="Validation\ValidationModelBase.cs" />
<Compile Include="Windows\CategoryPanel.cs" />
<Compile Include="Windows\CategoryWindow.xaml.cs">
<DependentUpon>CategoryWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Windows\ControlBox.cs" />
<Compile Include="Windows\SnappingWindow.cs" />
<Compile Include="Windows\WindowInformation.cs" />
@@ -230,6 +235,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Windows\CategoryWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
@@ -257,6 +266,7 @@
<ItemGroup>
<None Include="ChrisKaczor.pfx" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@@ -0,0 +1,30 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Common.Wpf.Extensions
{
public static class IconExtensions
{
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
public static ImageSource ToImageSource(this Icon icon)
{
var bitmap = icon.ToBitmap();
var hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
if (!DeleteObject(hBitmap))
throw new Win32Exception();
return wpfBitmap;
}
}
}

27
Windows/CategoryPanel.cs Normal file
View File

@@ -0,0 +1,27 @@
using System;
using System.Windows.Controls;
namespace Common.Wpf.Windows
{
public class CategoryPanel : UserControl
{
private object Data { get; set; }
public virtual void LoadPanel(object data)
{
Data = data;
}
public virtual bool ValidatePanel()
{
throw new NotImplementedException();
}
public virtual void SavePanel()
{
throw new NotImplementedException();
}
public virtual string CategoryName => null;
}
}

View File

@@ -0,0 +1,36 @@
<Window x:Class="Common.Wpf.Windows.CategoryWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="[Title]"
Height="320"
Width="566"
ResizeMode="CanResize"
WindowStartupLocation="CenterScreen">
<Grid>
<ListBox HorizontalAlignment="Left"
Name="CategoryList"
Width="126"
SelectionChanged="HandleSelectedCategoryChanged"
Margin="12,12,0,41" />
<ContentControl Margin="144,12,12,41"
Name="CategoryContent"
IsTabStop="False" />
<Button Content="[OK]"
Height="23"
HorizontalAlignment="Right"
Margin="0,0,93,12"
Name="OkayButton"
VerticalAlignment="Bottom"
Width="75"
IsDefault="True"
Click="HandleOkayButtonClick" />
<Button Content="[Cancel]"
Margin="0,0,12,12"
Name="CancelButton"
Height="23"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Width="75"
IsCancel="True" />
</Grid>
</Window>

View File

@@ -0,0 +1,113 @@
using Common.Wpf.Extensions;
using System.Collections.Generic;
using System.Drawing;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
namespace Common.Wpf.Windows
{
public partial class CategoryWindow
{
private readonly List<CategoryPanel> _optionPanels;
private readonly object _data;
public CategoryWindow(object data, List<CategoryPanel> panels, ResourceManager resourceManager, string resourcePrefix)
{
InitializeComponent();
_data = data;
Icon = ((Icon) resourceManager.GetObject(resourcePrefix + "_Icon")).ToImageSource();
Title = resourceManager.GetString(resourcePrefix + "_Title");
OkayButton.Content = resourceManager.GetString(resourcePrefix + "_OkayButton");
CancelButton.Content = resourceManager.GetString(resourcePrefix + "_CancelButton");
// Add all the option categories
_optionPanels = panels;
// Load the category list
LoadCategories();
}
private void LoadCategories()
{
// Loop over each panel
foreach (CategoryPanel optionsPanel in _optionPanels)
{
// Tell the panel to load itself
optionsPanel.LoadPanel(_data);
// Add the panel to the category ist
CategoryList.Items.Add(new CategoryListItem(optionsPanel));
// Set the panel into the right side
CategoryContent.Content = optionsPanel;
}
// Select the first item
CategoryList.SelectedItem = CategoryList.Items[0];
}
private void SelectCategory(CategoryPanel panel)
{
// Set the content
CategoryContent.Content = panel;
}
private void HandleSelectedCategoryChanged(object sender, SelectionChangedEventArgs e)
{
// Select the right category
SelectCategory(((CategoryListItem) CategoryList.SelectedItem).Panel);
}
private class CategoryListItem
{
public CategoryPanel Panel { get; private set; }
public CategoryListItem(CategoryPanel panel)
{
Panel = panel;
}
public override string ToString()
{
return Panel.CategoryName;
}
}
private void HandleOkayButtonClick(object sender, RoutedEventArgs e)
{
// Loop over each panel and ask them to validate
foreach (var optionsPanel in _optionPanels)
{
// If validation fails...
if (!optionsPanel.ValidatePanel())
{
// ...select the right category
SelectCategory(optionsPanel);
// Stop validation
return;
}
}
// Loop over each panel and ask them to save
foreach (CategoryPanel optionsPanel in _optionPanels)
{
// Save!
optionsPanel.SavePanel();
}
// Save the actual settings
//_data.SaveChanges();
//Properties.Settings.Default.Save();
DialogResult = true;
// Close the window
Close();
}
}
}