2009-07-31 84 views
2

我有一个UserControl,调用InputSensitiveTextBox,它继承自TextBox。它有一个我定义为CurrentInputType的属性,它的类型为MyControlsNamespace.SupportedInputTypes(值为Keyboard,Mouse,Touchpad,VirtualKey)。我需要有这个属性在XAML中设置就像我可能会设置HorizontalAlignmentScrollbarVisibility这样:带有Enum类型属性的WPF用户控件

MyControlsNamespace.InputSensitiveTextBox Background="Black" CurrentInputType="Keyboard" 

请指教:)

+0

您是否尝试过只需在Horizo​​ntalAlignment/ScrollbarVisibility中输入枚举值?你有任何错误? – 2009-07-31 15:34:52

+1

是的。它说它不能将字符串转换为枚举值,正如我所料。 :/ – Kamiikoneko 2009-07-31 15:37:05

回答

2

您需要使用Static标记扩展名来引用xaml中的枚举,并且还需要将其名称空间添加到您的名称空间声明中。

xmlns:MyControlsNamespace ="clr-namespace:MyControlsNamespace" 

<MyControlsNamespace:InputSensitiveTextBox 
    CurrentInputType="{x:Static MyControlsNamespace:SupportedInputTypes.Keyboard}" 
    /> 
1

是您CurrentInputType依赖属性?

如果不是这里是它的代码,以取代旧的属性:

public SupportedInputTypes CurrentInputType 
{ 
    get { return (SupportedInputTypes)GetValue(CurrentInputTypeProperty); } 
    set { SetValue(CurrentInputTypeProperty, value); } 
} 

// Using a DependencyProperty as the backing store for CurrentInputType. This enables animation, styling, binding, etc... 
public static readonly DependencyProperty CurrentInputTypeProperty = 
    DependencyProperty.Register("CurrentInputType", typeof(SupportedInputTypes), typeof(InputSensitiveTextBox), new PropertyMetadata(SupportedInputTypes.Keyboard)); 

在PropertyMetadata定义默认..

希望这能解决您的问题!