嗨,我希望我可以在這裏找到一些幫助...無法獲得附加屬性值
我正在創建一個使用棱鏡和MVVM的WPF應用程序。
我想創建一個附加的屬性,我發現here。
在我的ViewModel我得到
var control = Keyboard.FocusedElement;
聚焦元素那時我
string value = ExtraTextBehaviourObject.GetExtraText(control as UIElement);
,但返回的值總是空...任何人都可以點我朝着正確的方向? ?
UPDATE
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"/>
你也許應該也發佈你的財產的實施。 – Clemens
當然,爲什麼不! – sevdalone
感謝您的回覆...我剛剛更新了「設置」代碼 – sevdalone