C# 如何在实例化时将PreviewKeyDown事件附加到Dependencyobject?
我想将PreviewKeyDown事件附加到Instantiation上的dependencyobject 代码:C# 如何在实例化时将PreviewKeyDown事件附加到Dependencyobject?,c#,wpf,xaml,mvvm,dependencyobject,C#,Wpf,Xaml,Mvvm,Dependencyobject,我想将PreviewKeyDown事件附加到Instantiation上的dependencyobject 代码: public class PriceFieldExtension : DependencyObject { public static decimal GetPriceInputField(DependencyObject obj) { return (decimal)obj.GetValue(PriceInputFieldProperty);
public class PriceFieldExtension : DependencyObject
{
public static decimal GetPriceInputField(DependencyObject obj)
{
return (decimal)obj.GetValue(PriceInputFieldProperty);
}
public static void SetPriceInputField(DependencyObject obj, decimal value)
{
obj.SetValue(PriceInputFieldProperty, value);
}
public static readonly DependencyProperty PriceInputFieldProperty =
DependencyProperty.RegisterAttached("PriceInputField", typeof (decimal), typeof (PriceFieldExtension), new FrameworkPropertyMetadata(0.00M, new PropertyChangedCallback(OnIsTextPropertyChanged)));
private static void OnIsTextPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
TextBox targetTextbox = d as TextBox;
if (targetTextbox != null)
{
targetTextbox.PreviewKeyDown += targetTextbox_PreviewKeyDown;
}
}
static void targetTextbox_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = (e.Key == Key.Decimal);
}
}
<TextBox Text="{Binding InputPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=F2}" Style="{StaticResource DefaultTextBox}" classes:PriceFieldExtension.PriceInputField="{Binding InputPrice, StringFormat=F2, Converter={StaticResource StringToDecimalConverter}}" TextAlignment="Right" Margin="0,6,0,0" Height="45">
</TextBox>
现在,我必须在事件绑定到依赖对象之前更改文本框中的某些内容,但如何在实例化时进行更改
基本问题是我希望textbox只接受小数,但这里有一个陷阱:
当我在文本框中键入TextChanged event fire时,如下所示:
- 零火
- 不要开枪
- 0,0火
- 0,00火灾
- 不要开枪
public class PriceFieldExtension : DependencyObject
{
public static decimal GetPriceInputField(DependencyObject obj)
{
return (decimal)obj.GetValue(PriceInputFieldProperty);
}
public static void SetPriceInputField(DependencyObject obj, decimal value)
{
obj.SetValue(PriceInputFieldProperty, value);
}
public static readonly DependencyProperty PriceInputFieldProperty =
DependencyProperty.RegisterAttached("PriceInputField", typeof (decimal), typeof (PriceFieldExtension), new FrameworkPropertyMetadata(0.00M, new PropertyChangedCallback(OnIsTextPropertyChanged)));
private static void OnIsTextPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
TextBox targetTextbox = d as TextBox;
if (targetTextbox != null)
{
targetTextbox.PreviewKeyDown += targetTextbox_PreviewKeyDown;
}
}
static void targetTextbox_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = (e.Key == Key.Decimal);
}
}
<TextBox Text="{Binding InputPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=F2}" Style="{StaticResource DefaultTextBox}" classes:PriceFieldExtension.PriceInputField="{Binding InputPrice, StringFormat=F2, Converter={StaticResource StringToDecimalConverter}}" TextAlignment="Right" Margin="0,6,0,0" Height="45">
</TextBox>
若我将InputPrice属性更改为string,则每次都会触发TextChanged事件
我想通过捕捉“,”键来避免这种不一致。也许有更好的解决方案?使用屏蔽文本框怎么样:
行为如何?请注意,您的PriceFieldExtension不需要从DependencyObject派生,因为它只声明附加的属性。它可以声明为
静态类
,因为它的所有成员都是静态的。