Working ColorPickerUserControl

This commit is contained in:
Geoffroy BONNEVILLE
2020-06-01 10:32:06 +02:00
parent 4000d51f70
commit 5d8d996f44
2 changed files with 8 additions and 13 deletions

View File

@@ -11,7 +11,6 @@
SelectedItem="{Binding SelectedItem}" SelectedItem="{Binding SelectedItem}"
SelectedValue="{Binding SelectedColor, ElementName=UserControl}" SelectedValue="{Binding SelectedColor, ElementName=UserControl}"
SelectedValuePath="ColorBrush" SelectedValuePath="ColorBrush"
Loaded="ComboBox_OnLoaded"
SelectionChanged="ComboBox_OnSelectionChanged" SelectionChanged="ComboBox_OnSelectionChanged"
IsEnabled="{Binding IsEnabled, ElementName=UserControl}"> IsEnabled="{Binding IsEnabled, ElementName=UserControl}">
<ComboBox.ItemTemplate> <ComboBox.ItemTemplate>

View File

@@ -1,5 +1,4 @@
using System.Linq; using Windows.UI.Xaml;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
using ModernKeePass.ViewModels; using ModernKeePass.ViewModels;
@@ -10,8 +9,6 @@ namespace ModernKeePass.Views.UserControls
{ {
public sealed partial class ColorPickerUserControl public sealed partial class ColorPickerUserControl
{ {
private ColorPickerControlVm Model => ComboBox.DataContext as ColorPickerControlVm;
public SolidColorBrush SelectedColor public SolidColorBrush SelectedColor
{ {
get { return (SolidColorBrush)GetValue(SelectedColorProperty); } get { return (SolidColorBrush)GetValue(SelectedColorProperty); }
@@ -22,22 +19,21 @@ namespace ModernKeePass.Views.UserControls
nameof(SelectedColor), nameof(SelectedColor),
typeof(SolidColorBrush), typeof(SolidColorBrush),
typeof(ColorPickerUserControl), typeof(ColorPickerUserControl),
new PropertyMetadata(new SolidColorBrush(), (o, args) => { })); new PropertyMetadata(new SolidColorBrush(), (o, args) =>
{
var colorPickerUserControl = o as ColorPickerUserControl;
var vm = colorPickerUserControl?.ComboBox.DataContext as ColorPickerControlVm;
vm?.Initialize(args.NewValue as SolidColorBrush);
}));
public ColorPickerUserControl() public ColorPickerUserControl()
{ {
InitializeComponent(); InitializeComponent();
} }
private void ComboBox_OnLoaded(object sender, RoutedEventArgs e)
{
Model.Initialize(SelectedColor);
}
private void ComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e) private void ComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
if (e.RemovedItems.Any()) SelectedColor = (e.AddedItems[0] as ColorPickerControlVm.Color)?.ColorBrush;
SelectedColor = ComboBox.SelectedValue as SolidColorBrush;
} }
} }
} }