첨부 된 동작을 사용합니다. 첨부 된 동작은 기본적으로 이벤트를 명령으로 변환합니다. 예를 들어이 링크를 체크 아웃 :
여기
http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx
가 TextChangedBehavior 코드입니다.
public static class TextChangedBehavior
{
public static readonly DependencyProperty TextChangedCommandProperty =
DependencyProperty.RegisterAttached("TextChangedCommand",
typeof(ICommand),
typeof(TextChangedBehavior),
new PropertyMetadata(null, TextChangedCommandChanged));
public static ICommand GetTextChangedCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(TextChangedCommandProperty);
}
public static void SetTextChangedCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(TextChangedCommandProperty, value);
}
private static void TextChangedCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TextBoxBase textBox = obj as TextBoxBase;
if (textBox != null)
{
textBox.TextChanged += new TextChangedEventHandler(HandleTextChanged);
}
}
private static void HandleTextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
ICommand command = GetTextChangedCommand(textBox);
command.Execute(textBox.Text);
}
}
}
XAML :
<TextBox behavior:TextChangedBehavior.TextChangedCommand="{Binding TextChangedCommand}" />
좋아, 내가 명령을 사용할 수 없습니다 가정, 그것을 Textbox.TextChanged라고 가정합니다. – Firoso
그에 따라 편집되었습니다. –
텍스트의 경우 바인딩을 사용하십시오. –
decasteljau