Skip to content

Instantly share code, notes, and snippets.

@SpacePurr
Last active January 21, 2020 14:42
Show Gist options
  • Save SpacePurr/7877214ad70a74c0a72f87a4e9e1649a to your computer and use it in GitHub Desktop.
Save SpacePurr/7877214ad70a74c0a72f87a4e9e1649a to your computer and use it in GitHub Desktop.
ColorBox
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using TDMS3DControl.Commands;
using TDMS3DControl.Models;
using TDMS3DControl.Views;
namespace TDMS3DControl.ViewModels
{
class ColorBoxViewModel : BaseViewModel
{
public int ClickResult { get; set; }
private SolidColorBrush elementColor;
public SolidColorBrush ElementColor { get => elementColor; set { elementColor = value; OnPropertyChanged(); } }
public System.Drawing.Color StatusColor { get; set; } = System.Drawing.Color.Empty;
public ICommand CheckColor { get; set; }
public ICommand SetColor { get; set; }
public ICommand SetBaseColor { get; set; }
private readonly ColorBox Box;
public ColorBoxViewModel(ColorBox box)
{
Box = box;
CheckColor = new Command(CheckColorMethod);
SetColor = new Command(SetColorMethod);
SetBaseColor = new Command(SetBaseColorMethod);
}
private void CheckColorMethod(object obj)
{
ElementColor = (SolidColorBrush)obj;
}
public System.Drawing.Color ToDrawingColor(SolidColorBrush mediaColor)
{
if (mediaColor == null) return System.Drawing.Color.Empty;
if (mediaColor.Color.R.ToString() == "255")
return System.Drawing.Color.Red;
else if(mediaColor.Color.G.ToString() == "128")
return System.Drawing.Color.Green;
else
return System.Drawing.Color.Blue;
}
private void SetBaseColorMethod(object obj)
{
ClickResult = 2;
Box.Close();
}
private void SetColorMethod(object obj)
{
ClickResult = 1;
StatusColor = ToDrawingColor(ElementColor);
Box.Close();
}
}
}
<Window x:Class="TDMS3DControl.Views.ColorBox"
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:TDMS3DControl.Views"
xmlns:TestXBim="clr-namespace:TDMS3DControl.ViewModels"
xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
Title="ColorBox" Height="250" Width="250">
<Window.Resources>
<Style TargetType="{x:Type RadioButton}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Border>
<Rectangle x:Name="Rectangle" Width="30" Height="50" Fill="{TemplateBinding Background}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="Rectangle" Property="Width" Value="40"/>
<Setter TargetName="Rectangle" Property="Height" Value="60"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Rectangle" Property="Width" Value="40"/>
<Setter TargetName="Rectangle" Property="Height" Value="60"/>
<Setter TargetName="Rectangle" Property="Cursor" Value="Hand"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Rectangle" Property="Width" Value="35"/>
<Setter TargetName="Rectangle" Property="Height" Value="55"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="5*"/>
<RowDefinition Height="1*" />
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Grid.IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="103*"/>
<RowDefinition Height="32*"/>
</Grid.RowDefinitions>
<RadioButton Width="40" Grid.Column="0" Background="Red" VerticalAlignment="Center" GroupName="Colors"
Command="{Binding CheckColor}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Background}" />
<RadioButton Width="40" Grid.Column="1" Background="Green" VerticalAlignment="Center" GroupName="Colors"
Command="{Binding CheckColor}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Background}" />
<RadioButton Width="40" Grid.Column="2" Background="Blue" VerticalAlignment="Center" GroupName="Colors"
Command="{Binding CheckColor}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Background}" />
</Grid>
<Button Grid.Row="1" Background="AliceBlue" Content="Установить новый цвет" Margin="5" Command="{Binding SetColor}" />
<Button Grid.Row="2" Background="AliceBlue" Content="Установить базовый цвет" Margin="5" Command="{Binding SetBaseColor}" />
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment