-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
228 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace WpfDemo.Models | ||
{ | ||
public class ShortcutBinding : INotifyPropertyChanged | ||
{ | ||
private string shortcut; | ||
private bool enabled; | ||
private bool isActive; | ||
public string Shortcut | ||
{ | ||
get => shortcut; | ||
set | ||
{ | ||
if (value != shortcut) | ||
{ | ||
shortcut = value; | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Shortcut")); | ||
} | ||
} | ||
} | ||
public bool Enabled | ||
{ | ||
get => enabled; | ||
set | ||
{ | ||
if (value != enabled) | ||
{ | ||
enabled = value; | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Enabled")); | ||
|
||
} | ||
|
||
} | ||
} | ||
|
||
public bool IsActive | ||
{ | ||
get => isActive; | ||
set | ||
{ | ||
if (value != isActive) | ||
{ | ||
isActive = value; | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("isActive")); | ||
} | ||
} | ||
} | ||
public event PropertyChangedEventHandler PropertyChanged; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<UserControl x:Class="WpfDemo.UserControls.ShortcutEdit" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="clr-namespace:WpfDemo.UserControls" | ||
mc:Ignorable="d" | ||
d:DesignHeight="40" d:DesignWidth="350"> | ||
<Grid x:Name="mainGrid"> | ||
<StackPanel x:Name="panelEdit" Orientation="Horizontal" Margin="0,0,-22,0" Visibility="Visible" Width="372"> | ||
<CheckBox Content="Enable" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" IsChecked="{Binding Enable}" /> | ||
<TextBox x:Name="txtShortcut" VerticalAlignment="Center" Margin="10,0,10,0" TextWrapping="Wrap" Width="230" Height="26" IsReadOnly="True" | ||
KeyDown="TxtShortcut_KeyDown" | ||
KeyUp="TxtShortcut_KeyUp" | ||
LostFocus="TxtShortcut_LostFocus" | ||
Visibility="Collapsed" | ||
/> | ||
<Label x:Name="txtBlockShortcut" VerticalAlignment="Center" Width="230" Height="26" Content="{Binding Shortcut}" Margin="10,0,0,0" | ||
MouseDoubleClick="TxtBlockShortcut_MouseDoubleClick" | ||
Visibility="Visible" | ||
/> | ||
</StackPanel> | ||
</Grid> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Data; | ||
using System.Windows.Documents; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Imaging; | ||
using System.Windows.Navigation; | ||
using System.Windows.Shapes; | ||
using WpfDemo.Models; | ||
|
||
namespace WpfDemo.UserControls | ||
{ | ||
/// <summary> | ||
/// Interaction logic for ShortcutEdit.xaml | ||
/// </summary> | ||
public partial class ShortcutEdit : UserControl | ||
{ | ||
ShortcutBinding binding; | ||
HashSet<Key> keys; | ||
public ShortcutEdit() | ||
{ | ||
InitializeComponent(); | ||
binding = DataContext as ShortcutBinding; | ||
keys = new HashSet<Key>(); | ||
} | ||
|
||
|
||
|
||
private void TxtShortcut_KeyDown(object sender, KeyEventArgs e) | ||
{ | ||
keys.Add(e.Key); | ||
txtShortcut.Text = TransformSetToText(keys); | ||
} | ||
|
||
private void TxtShortcut_KeyUp(object sender, KeyEventArgs e) | ||
{ | ||
//keys.Remove(e.Key); | ||
//txtShortcut.Text = TransformSetToText(keys); | ||
} | ||
|
||
static public string TransformSetToText(HashSet<Key> keys) | ||
{ | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
foreach(var key in keys) | ||
{ | ||
if (stringBuilder.Length > 0) | ||
{ | ||
stringBuilder.Append(" + "); | ||
} | ||
string keyString = key.ToString(); | ||
if (keyString.StartsWith("Left")) | ||
{ | ||
keyString = keyString.Replace("Left", ""); | ||
} else if (keyString.StartsWith("Right")) | ||
{ | ||
keyString = keyString.Replace("Left", ""); | ||
} | ||
stringBuilder.Append(keyString); | ||
} | ||
return stringBuilder.ToString(); | ||
} | ||
|
||
private void TxtBlockShortcut_MouseDoubleClick(object sender, MouseButtonEventArgs e) | ||
{ | ||
txtBlockShortcut.Visibility = Visibility.Collapsed; | ||
txtShortcut.Text = txtBlockShortcut.Content as string ; | ||
keys.Clear(); | ||
txtShortcut.Visibility = Visibility.Visible; | ||
txtShortcut.Focus(); | ||
|
||
} | ||
private void TxtShortcut_LostFocus(object sender, RoutedEventArgs e) | ||
{ | ||
txtShortcut.Visibility = Visibility.Collapsed; | ||
txtBlockShortcut.Visibility = Visibility.Visible; | ||
txtBlockShortcut.Content = txtShortcut.Text; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters