嗨,我希望我能在这里找到一些帮助......
我正在使用 prism 和 MVVM 创建一个 WPF 应用程序。
我正在尝试创建我在这里找到的附加属性。
在我的 ViewModel 中,我通过
var control = Keyboard.FocusedElement;
然后我做
string value = ExtraTextBehaviourObject.GetExtraText(control as UIElement);
但返回的值始终为空......谁能指出我正确的方向???
更新
public class ExtraTextBehaviourObject : DependencyObject
{
//Declare the dependency property
public static readonly DependencyProperty ExtraTextProperty;
static ExtraTextBehaviourObject()
{
//register it as attached property
ExtraTextProperty = DependencyProperty.RegisterAttached("ExtraText", typeof(string),
typeof(ExtraTextBehaviourObject));
}
//static function for setting the text
public static void SetExtraText(UIElement uiElement, string value)
{
if (uiElement != null)
{
uiElement.SetValue(ExtraTextProperty, value);
}
}
//static function for getting the text
public static string GetExtraText(UIElement uiElement)
{
if (uiElement != null)
{
return (string)uiElement.GetValue(ExtraTextProperty);
}
return "";
}
}
在 XAML 中设置代码
<dxe:TextEdit Text="{Binding Path=Customer.Comments, Mode=TwoWay}" AcceptsReturn="True" VerticalContentAlignment="Top"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Behaviors:ExtraTextBehaviourObject.ExtraText="HelloExtraText"
ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto"/>