-
Notifications
You must be signed in to change notification settings - Fork 10
/
FilenameEditor.xaml.cs
66 lines (59 loc) · 2.59 KB
/
FilenameEditor.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Ookii.Dialogs.Wpf;
using RT.Util.Serialization;
using WotDataLib;
using WpfCrutches;
using Xceed.Wpf.Toolkit.PropertyGrid;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
namespace TankIconMaker
{
public partial class FilenameEditor : UserControl, ITypeEditor
{
private BindingExpression _expression;
/// <summary>
/// A hack to give the filename editor access to the relevant context. A non-hacky approach would require the
/// MainWindow to pass to each instance some way of retrieving the current context. However, since these instances
/// are created by a third-party component, this is highly non-trivial, so this hack is used instead.</summary>
public static WotContext LastContext;
public FilenameEditor()
{
InitializeComponent();
}
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
BindingOperations.SetBinding(textbox, TextBox.TextProperty, LambdaBinding.New(
new Binding("Value") { Source = propertyItem, Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay },
(Filename source) => { return (string) source; },
(string source) => { return (Filename) source; }
));
_expression = textbox.GetBindingExpression(TextBox.TextProperty);
return this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var dlg = new VistaOpenFileDialog();
dlg.Filter = App.Translation.Misc.Filter_FilenameEditor;
dlg.FilterIndex = 0;
dlg.Multiselect = false;
dlg.CheckFileExists = false;
if (dlg.ShowDialog() != true)
return;
textbox.Text = Ut.MakeRelativePath(LastContext, dlg.FileName);
_expression.UpdateSource();
}
}
struct Filename
{
private string _name;
public static implicit operator Filename(string value) { return new Filename { _name = value }; }
public static implicit operator string(Filename value) { return value._name; }
public override string ToString() { return this; }
}
class filenameTypeOptions : ClassifyTypeOptions, IClassifySubstitute<Filename, string>
{
public Filename FromSubstitute(string instance) { return instance; }
public string ToSubstitute(Filename instance) { return instance; }
}
}