Skip to content

Commit bc0f631

Browse files
committed
testing radiobutton databinding to enum
1 parent b7cea1e commit bc0f631

File tree

6 files changed

+93
-57
lines changed

6 files changed

+93
-57
lines changed

PixelArtTool/App.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
55
</startup>
6-
</configuration>
6+
</configuration>

PixelArtTool/MainWindow.xaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
<Window.InputBindings>
1414
<KeyBinding Command="ApplicationCommands.Undo" Gesture="Ctrl+Z"/>
1515
</Window.InputBindings>
16-
16+
1717
<Grid>
18+
<Grid.Resources>
19+
<local:EnumBooleanConverter x:Key="ComparisonConverter" />
20+
</Grid.Resources>
1821
<ToolBarTray Background="White" Height="36" VerticalAlignment="Top">
1922
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
2023
<Button x:Name="btnNew" ToolTip="New (clear image)" Click="OnClearButton">
@@ -26,10 +29,12 @@
2629
</ToolBar>
2730

2831
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top">
29-
<RadioButton GroupName="Toolbar" Tag="Draw" ToolTip="Pencil" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="True" Click="OnToolChanged">
32+
<RadioButton GroupName="Toolbar" Tag="Draw" ToolTip="Pencil" Style="{StaticResource {x:Type ToggleButton}}"
33+
IsChecked="{Binding Path=CurrentTool, Converter={StaticResource ComparisonConverter}, ConverterParameter={x:Static local:ToolMode.Draw},Mode=TwoWay}">
3034
<Image Source="/Resources/Buttons/drawmode.png" />
3135
</RadioButton>
32-
<RadioButton GroupName="Toolbar" Tag="Fill" ToolTip="Flood Fill" Style="{StaticResource {x:Type ToggleButton}}" Click="OnToolChanged">
36+
<RadioButton GroupName="Toolbar" Tag="Fill" ToolTip="Flood Fill" Style="{StaticResource {x:Type ToggleButton}}"
37+
IsChecked="{Binding Path=CurrentTool, Converter={StaticResource ComparisonConverter}, ConverterParameter={x:Static local:ToolMode.Fill},Mode=TwoWay}">
3338
<Image Source="/Resources/Buttons/emptybutton.png" />
3439
</RadioButton>
3540
</ToolBar>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Runtime.InteropServices;
66
using System.Windows;
77
using System.Windows.Controls;
8+
using System.Windows.Data;
89
using System.Windows.Input;
910
using System.Windows.Media;
1011
using System.Windows.Media.Imaging;
@@ -18,10 +19,10 @@ public enum BlendMode : byte
1819
Additive = 1
1920
}
2021

21-
public enum ToolMode : byte
22+
public enum ToolMode
2223
{
23-
Draw = 0,
24-
Fill = 1
24+
Draw,
25+
Fill
2526
}
2627

2728
/// <summary>
@@ -70,7 +71,22 @@ public partial class MainWindow : Window
7071

7172
// modes
7273
BlendMode blendMode;
73-
ToolMode currentTool;
74+
75+
// TEST property binding
76+
private ToolMode myVar = ToolMode.Draw;
77+
public ToolMode CurrentTool
78+
{
79+
get
80+
{
81+
Console.WriteLine("get:"+myVar);
82+
return myVar; }
83+
set {
84+
Console.WriteLine("set:"+value);
85+
myVar = value; }
86+
}
87+
88+
89+
//public MyLovelyEnum VeryLovelyEnum { get; set; }
7490

7591
public MainWindow()
7692
{
@@ -80,6 +96,7 @@ public MainWindow()
8096

8197
void Start()
8298
{
99+
DataContext = this;
83100

84101
// setup background grid
85102
gridImage = imgGrid;
@@ -139,9 +156,6 @@ void Start()
139156
currentColorIndex = 5;
140157
currentColor = palette[currentColorIndex];
141158
UpdateCurrentColor();
142-
143-
//this.KeyDown += new KeyEventHandler(OnKeyDown);
144-
//this.keyup += new KeyEventHandler(OnKeyDown);
145159
}
146160

147161

@@ -489,7 +503,7 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
489503
int y = (int)(e.GetPosition(drawingImage).Y / canvasScaleX);
490504

491505

492-
switch (currentTool)
506+
switch (CurrentTool)
493507
{
494508
case ToolMode.Draw:
495509
DrawPixel(x, y);
@@ -526,7 +540,7 @@ void DrawingAreaMouseMoved(object sender, MouseEventArgs e)
526540

527541
if (e.LeftButton == MouseButtonState.Pressed)
528542
{
529-
switch (currentTool)
543+
switch (CurrentTool)
530544
{
531545
case ToolMode.Draw:
532546
DrawPixel(x, y);
@@ -680,7 +694,15 @@ void OnKeyDown(object sender, KeyEventArgs e)
680694
// TODO: add tool shortcut keys
681695
switch (e.Key)
682696
{
683-
case Key.LeftShift:
697+
case Key.B: // brush
698+
CurrentTool = ToolMode.Draw;
699+
Console.WriteLine("drawmode");
700+
break;
701+
case Key.F: // floodfill
702+
CurrentTool = ToolMode.Fill;
703+
Console.WriteLine("fillmode");
704+
break;
705+
case Key.LeftShift: // left shift
684706
lblToolInfo.Content = "Straight Lines";
685707
leftShiftDown = true;
686708
break;
@@ -906,9 +928,29 @@ int Repeat(int val, int max)
906928

907929
private void OnToolChanged(object sender, RoutedEventArgs e)
908930
{
909-
string tag = (string)((RadioButton)sender).Tag;
910-
Enum.TryParse(tag, out currentTool);
931+
//string tag = (string)((RadioButton)sender).Tag;
932+
//Enum.TryParse(tag, out currentTool);
911933
}
912934

935+
936+
913937
} // class
938+
939+
public class EnumBooleanConverter : IValueConverter
940+
{
941+
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
942+
{
943+
Console.WriteLine("valueA:"+ value);
944+
Console.WriteLine("parameter:" + parameter);
945+
return value?.Equals(parameter);
946+
}
947+
948+
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
949+
{
950+
Console.WriteLine("valueB:" + value);
951+
Console.WriteLine("parameter:" + parameter);
952+
return value?.Equals(true) == true ? parameter : Binding.DoNothing;
953+
}
954+
}
955+
914956
} // namespace

PixelArtTool/PixelArtTool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<FileAlignment>512</FileAlignment>
1313
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
1414
<WarningLevel>4</WarningLevel>
15+
<TargetFrameworkProfile />
1516
</PropertyGroup>
1617
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1718
<PlatformTarget>AnyCPU</PlatformTarget>

PixelArtTool/Properties/Resources.Designer.cs

Lines changed: 18 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PixelArtTool/Properties/Settings.Designer.cs

Lines changed: 9 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)