<
GroupBox
x:Name
=
"GroupGrid"
Header
"New user"
HorizontalAlignment
"Left"
Margin
"13,17,0,0"
VerticalAlignment
"Top"
Height
"125"
Width
"258"
Grid.ColumnSpan
"2"
IsEnabled
"False"
>
using
System.Windows;
System.Windows.Controls;
namespace
WpfAppExample1.Controls
{
public
class
CanEnableTextBox : TextBox
static
CanEnableTextBox()
IsEnabledProperty.OverrideMetadata(
typeof
(CanEnableTextBox),
new
UIPropertyMetadata(
true
,
IsEnabledPropertyChanged,
CoerceIsEnabled));
}
private
void
IsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
// Overriding PropertyChanged results in merged metadata, which is what we want--
// the PropertyChanged logic in UIElement.IsEnabled will still get invoked.
object
CoerceIsEnabled(DependencyObject source,
value)
return
value;
NumberOnlyBehaviour
readonly
DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached(
"IsEnabled"
(
bool
),
(NumberOnlyBehaviour),
false
, OnValueChanged));
GetIsEnabled(Control o) {
)o.GetValue(IsEnabledProperty); }
SetIsEnabled(Control o,
value) { o.SetValue(IsEnabledProperty, value); }
OnValueChanged(DependencyObject dependencyObject,DependencyPropertyChangedEventArgs e)
if
(!(dependencyObject
is
Control uiElement))
;
(e.NewValue
value && value)
uiElement.PreviewTextInput += OnTextInput;
uiElement.PreviewKeyDown += OnPreviewKeyDown;
DataObject.AddPastingHandler(uiElement, OnPaste);
else
uiElement.PreviewTextInput -= OnTextInput;
uiElement.PreviewKeyDown -= OnPreviewKeyDown;
DataObject.RemovePastingHandler(uiElement, OnPaste);
OnTextInput(
sender, TextCompositionEventArgs e)
(e.Text.Any(c => !
char
.IsDigit(c))) { e.Handled =
; }
OnPreviewKeyDown(
sender, KeyEventArgs e)
(e.Key == Key.Space)e.Handled =
OnPaste(
sender, DataObjectPastingEventArgs e)
(e.DataObject.GetDataPresent(DataFormats.Text))
var text = Convert.ToString(e.DataObject.GetData(DataFormats.Text)).Trim();
(text.Any(c => !
.IsDigit(c))) { e.CancelCommand(); }
e.CancelCommand();
System.Collections.Generic;
System.Linq;
WpfAppExample1.Classes
MockedData
List<Person> PeopleList()
List<Person>()
Person() {Id = 1,FirstName =
"Karen"
,LastName =
"Payne"
},
Person() {Id = 2,FirstName =
"Mary"
"Jones"
Person() {Id = 3,FirstName =
"Jack"
"Lebow"
};
Person FindPerson(
int
code)
var person =
Person() {Id = -1};
var result = NewUsers.SystemCodes().ContainsKey(code);
(result)
person = PeopleList().First(user => user.Id == NewUsers.SystemCodes()[code]);
person;
Image.Style
Style
TargetType
"{x:Type Image}"
Setter
Property
"Visibility"
Value
"Hidden"
/>
Style.Triggers
DataTrigger
Binding
"{Binding IsEnabled, ElementName=GroupGrid}"
"True"
"Visible"
</
TextBox.Style
"{x:Type TextBox}"
"Text"
"{Binding ElementName=IsEnabledProperty, Path=DefaultMetadata}"
Trigger
"{x:Null}"
Button.Style
"{Binding ElementName=GroupGrid, Path=IsEnabled}"
"Button.IsEnabled"