This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
/
SelectProductForm.xaml.cs
136 lines (127 loc) · 3.66 KB
/
SelectProductForm.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using DevExpress.Xpf.Core;
using DevExpress.Utils;
using DevExpress.XtraRichEdit.API.Native;
namespace DXRichEditHyperlinkHandling.Forms
{
/// <summary>
/// Interaction logic for SelectProductForm.xaml
/// </summary>
public partial class SelectProductForm : UserControl
{
#region #Properties
object fEditValue;
DocumentRange fRange;
public virtual object EditValue
{
get
{
return fEditValue;
}
}
public DocumentRange Range
{
get
{
return fRange;
}
set
{
fRange = value;
}
}
#endregion #Properties
#region #FloatingContainer
FloatingContainer fOwnerWindow;
public FloatingContainer OwnerWindow
{
get
{
return fOwnerWindow;
}
set
{
if (fOwnerWindow == value) return; fOwnerWindow = value; OnOwnerWindowChanged();
}
}
#endregion #FloatingContainer
#region #CommitEvent
EventHandler onCommit;
public event EventHandler Commit { add { onCommit += value; } remove { onCommit -= value; } }
#endregion #CommitEvent
public static readonly DependencyProperty ProductsProperty;
static SelectProductForm()
{
ProductsProperty = DependencyProperty.Register("Products", typeof(List<string>), typeof(SelectProductForm), new PropertyMetadata(null));
}
protected void RaiseCommitEvent()
{
if (onCommit != null)
onCommit(this, EventArgs.Empty);
}
public SelectProductForm()
{
this.KeyDown += PopupControlBase_KeyDown;
}
protected virtual void SetEditValueCore(object value)
{
this.fEditValue = value;
}
private void PopupControlBase_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
Close();
else if (e.Key == Key.Enter)
PerformCommit();
}
protected virtual void PerformCommit()
{
SetEditValue();
RaiseCommitEvent();
Close();
}
protected void Close()
{
if (OwnerWindow != null && OwnerWindow.IsOpen)
OwnerWindow.IsOpen = false;
}
public SelectProductForm(List<string> list)
{
Guard.ArgumentNotNull(list, "list");
Products = list;
InitializeComponent();
Dispatcher.BeginInvoke(new Action(() => this.listBox.Focus()));
}
public List<string> Products
{
get
{
return (List<string>)GetValue(ProductsProperty);
}
set
{
SetValue(ProductsProperty, value);
}
}
protected void SetEditValue()
{
SetEditValueCore((string)this.listBox.SelectedItem);
}
#region #OnOwnerWindowChanged
protected void OnOwnerWindowChanged()
{
if (OwnerWindow != null)
OwnerWindow.Caption = "Select a product";
}
#endregion #OnOwnerWindowChanged
private void listBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (this.listBox.SelectedIndex >= 0)
PerformCommit();
}
}
}