嗨,我希望我能在这里找到一些帮助...
我正在使用棱镜和MVVM创建WPF应用程序。
我试图创建我发现here的附加属性。
在我的ViewModel中,通过
var control = Keyboard.FocusedElement;
那我做
string value = ExtraTextBehaviourObject.GetExtraText(control as UIElement);
但是返回的值始终为null ...谁能指出我正确的方向???
更新
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"/>
最佳答案
问题是,当我使用Keyboard.FocusedElement时,我没有得到正确的控件。当我使用它们的控件时,这可能是devexpress的事情。因此,向上遍历元素树直到我获得该控件解决了我的问题...谢谢Clemens!
关于wpf - 无法获得附加的属性(property)值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16940030/