Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created October 9, 2019 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/974d517c79c0e5249a245a0356da0bb5 to your computer and use it in GitHub Desktop.
Save danielplawgo/974d517c79c0e5249a245a0356da0bb5 to your computer and use it in GitHub Desktop.
Jak budować okno ustawień w aplikacji
public partial class AdminSettingsView : Window, ISettingsView
{
public AdminSettingsView()
{
InitializeComponent();
}
public double OrderNumber => 10;
public bool CanShow(ApplicationContext context)
{
return context.IsAdmin;
}
}
public partial class App : Application
{
protected IContainer BuildContainer()
{
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.AsImplementedInterfaces();
var applicationContext = new ApplicationContext() { IsAdmin = true };
builder.RegisterInstance(applicationContext)
.AsSelf();
return builder.Build();
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var container = BuildContainer();
MainWindow = container.Resolve<ISettingsWindowWindow>() as Window;
MainWindow.Show();
}
}
public class ApplicationContext
{
public bool IsAdmin { get; set; }
}
public partial class ChangePasswordView : Window, ISettingsView
{
public ChangePasswordView()
{
Title = "Change Password";
InitializeComponent();
}
public double OrderNumber => 2;
public bool CanShow(ApplicationContext context)
{
return true;
}
}
public partial class GeneralSettingsView : Window, ISettingsView
{
public GeneralSettingsView()
{
Title = "General Settings";
InitializeComponent();
}
public double OrderNumber => 1;
public bool CanShow(ApplicationContext context)
{
return true;
}
}
public interface ISettingsView
{
string Title { get; }
double OrderNumber { get; }
void Show();
bool CanShow(ApplicationContext context);
}
public partial class SettingsWindow : Window, ISettingsWindowWindow
{
public ObservableCollection<ISettingsView> SettingsViews { get; set; }
public SettingsWindow(IEnumerable<ISettingsView> settingsViews,
ApplicationContext applicationContext)
{
InitializeComponent();
var processedSettingsViews = settingsViews
.Where(v => v.CanShow(applicationContext))
.OrderBy(v => v.OrderNumber);
SettingsViews = new ObservableCollection<ISettingsView>(processedSettingsViews);
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if(button == null)
{
return;
}
var view = button.DataContext as ISettingsView;
if(view == null)
{
return;
}
view.Show();
}
}
public interface ISettingsWindowWindow
{
void Show();
}
<Window x:Class="OpenClosePrinciple.SettingsWindow"
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"
xmlns:local="clr-namespace:OpenClosePrinciple"
mc:Ignorable="d"
Title="Open Close Principle" Height="450" Width="800">
<Grid>
<ListBox ItemsSource="{Binding SettingsViews}"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Title}"
Click="Button_Click" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment