mirror of
https://github.com/ckaczor/Common.Wpf.git
synced 2026-02-01 09:35:38 -05:00
Add generic category window to WPF library
This commit is contained in:
27
Windows/CategoryPanel.cs
Normal file
27
Windows/CategoryPanel.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
36
Windows/CategoryWindow.xaml
Normal file
36
Windows/CategoryWindow.xaml
Normal 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>
|
||||
113
Windows/CategoryWindow.xaml.cs
Normal file
113
Windows/CategoryWindow.xaml.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user