Skip to content

Commit 143bf9d

Browse files
committed
Add search and copy
1 parent 25f0120 commit 143bf9d

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

MainDemo.Wpf/IconPack.xaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@
5353
</ListBox>
5454
<materialDesign:ColorZone Mode="PrimaryLight" Grid.Row="2" Margin="0 8 0 0" CornerRadius="2" materialDesign:ShadowAssist.ShadowDepth="Depth2">
5555
<StackPanel Orientation="Horizontal" Margin="8">
56+
<Border Background="White" MaxHeight="30" CornerRadius="3" ToolTip="Enter to search, ignore case">
57+
<Grid>
58+
<Grid.ColumnDefinitions>
59+
<ColumnDefinition Width="Auto" />
60+
<ColumnDefinition Width="*" />
61+
</Grid.ColumnDefinitions>
62+
<Button Style="{DynamicResource MaterialDesignToolButton}"
63+
Command="{Binding SearchCommand}" x:Name="SearchButton"
64+
Height="24" Width="24">
65+
<materialDesign:PackIcon Kind="Magnify" Opacity=".56" />
66+
</Button>
67+
<TextBox Grid.Column="1" Margin="5 0 0 0" KeyDown="Search_OnKeyDown"
68+
materialDesign:TextFieldAssist.Hint="Search"
69+
materialDesign:TextFieldAssist.DecorationVisibility="Hidden" BorderThickness="0"
70+
MinWidth="200" VerticalAlignment="Center" />
71+
</Grid>
72+
</Border>
5673
<TextBlock Margin="8 0 0 0" Style="{StaticResource MaterialDesignSubheadingTextBlock}" VerticalAlignment="Center">Usage:</TextBlock>
5774
<materialDesign:ColorZone Mode="Standard" Margin="8" CornerRadius="2" Padding="6 2 6 2">
5875
<TextBox IsReadOnly="True"
@@ -64,6 +81,12 @@
6481
Text="{Binding ElementName=KindsListBox, Path=SelectedValue, StringFormat='&lt;materialDesign:PackIcon Kind=&quot;{0}&quot; \/>'}" />
6582
</materialDesign:ColorZone>
6683
<materialDesign:PackIcon Kind="{Binding ElementName=KindsListBox, Path=SelectedValue}" VerticalAlignment="Center" />
84+
<Button Margin="8 0" Command="{Binding CopyToClipboardCommand, Mode=OneTime}" CommandParameter="{Binding ElementName=KindsListBox, Path=SelectedValue}">
85+
<StackPanel Orientation="Horizontal">
86+
<materialDesign:PackIcon Kind="ContentCopy"/>
87+
<TextBlock Text="Copy To Clipboard"/>
88+
</StackPanel>
89+
</Button>
6790
</StackPanel>
6891
</materialDesign:ColorZone>
6992
</Grid>

MainDemo.Wpf/IconPack.xaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,12 @@ private void TextBox_OnGotFocus(object sender, RoutedEventArgs e)
3030
var textBox = (TextBox)sender;
3131
textBox.Dispatcher.BeginInvoke(new Action(textBox.SelectAll));
3232
}
33+
34+
private void Search_OnKeyDown(object sender, KeyEventArgs e)
35+
{
36+
var textBox = (TextBox)sender;
37+
if (e.Key == Key.Enter)
38+
SearchButton.Command.Execute(textBox.Text);
39+
}
3340
}
3441
}

MainDemo.Wpf/IconPackViewModel.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Diagnostics;
45
using System.Linq;
6+
using System.Runtime.CompilerServices;
57
using System.Text;
68
using System.Threading.Tasks;
9+
using System.Windows;
710
using System.Windows.Input;
811
using MaterialDesignColors.WpfExample.Domain;
912
using MaterialDesignThemes.Wpf;
1013

1114
namespace MaterialDesignDemo
1215
{
13-
public class IconPackViewModel
16+
public class IconPackViewModel : INotifyPropertyChanged
1417
{
1518
private readonly Lazy<IEnumerable<PackIconKind>> _packIconKinds;
1619

1720
public IconPackViewModel()
1821
{
1922
OpenDotComCommand = new AnotherCommandImplementation(OpenDotCom);
23+
SearchCommand = new AnotherCommandImplementation(Search);
24+
CopyToClipboardCommand = new AnotherCommandImplementation(CopyToClipboard);
2025
_packIconKinds = new Lazy<IEnumerable<PackIconKind>>(() =>
2126
Enum.GetValues(typeof (PackIconKind)).OfType<PackIconKind>()
2227
.OrderBy(k => k.ToString(), StringComparer.InvariantCultureIgnoreCase).ToList()
@@ -25,14 +30,47 @@ public IconPackViewModel()
2530
}
2631

2732
public ICommand OpenDotComCommand { get; }
33+
public ICommand SearchCommand { get; }
34+
public ICommand CopyToClipboardCommand { get; }
2835

29-
public IEnumerable<PackIconKind> Kinds => _packIconKinds.Value;
36+
private IEnumerable<PackIconKind> _kinds;
37+
public IEnumerable<PackIconKind> Kinds
38+
{
39+
get { return _kinds ?? (_kinds = _packIconKinds.Value); }
40+
set
41+
{
42+
_kinds = value;
43+
OnPropertyChanged();
44+
}
45+
}
3046

3147
private void OpenDotCom(object obj)
3248
{
3349
Process.Start("https://materialdesignicons.com/");
3450
}
3551

52+
private void Search(object obj)
53+
{
54+
var text = obj as string;
55+
if (string.IsNullOrWhiteSpace(text))
56+
Kinds = _packIconKinds.Value;
57+
else
58+
Kinds =
59+
_packIconKinds.Value.Where(
60+
x => x.ToString().IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0);
61+
}
62+
63+
private void CopyToClipboard(object obj)
64+
{
65+
var kind = (PackIconKind?)obj;
66+
Clipboard.SetText($"<materialDesign:PackIcon Kind=\"{kind}\" />");
67+
}
68+
69+
public event PropertyChangedEventHandler PropertyChanged;
3670

71+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
72+
{
73+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
74+
}
3775
}
3876
}

0 commit comments

Comments
 (0)