0
我想要這樣做的原因是UI自動化。我們的UI自動化軟件通過Name/Uid屬性查找元素。 name屬性不能綁定任何東西,而且Uid似乎只有一個OneTime綁定(即使設置爲OneWay)。綁定標籤屬性起作用但具有視覺效果。所以這個想法是爲覆蓋Uid屬性的所有UI元素創建我自己的屬性。WPF DependencyProperty覆蓋所有控件的Uid
這裏是我到目前爲止,我怎麼想它可以工作
namespace MyApplication.Shared.Controls
{
public class RxUidAttributeExtension : Control
{
public static readonly DependencyProperty RxUidProperty = DependencyProperty.RegisterAttached("RxUid", typeof(string), typeof(RxUidAttributeExtension), new FrameworkPropertyMetadata("", RxUidChanged)
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
private static void RxUidChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var metadata = RxUidProperty.GetMetadata(typeof(RxUidAttributeExtension));
UidProperty.OverrideMetadata(typeof(UIElement), new UIPropertyMetadata("RxOverridden")); //override Uid property -> exception
}
public static string GetRxUid(DependencyObject d)
{
return (string)d.GetValue(RxUidProperty);
}
public static void SetRxUid(DependencyObject d, string value)
{
d.SetValue(RxUidProperty, value);
}
}
}
而且在XAML:
<DockPanel Name="myNameNeverChanges"
controls:RxUidAttributeExtension.RxUid="{Binding MyProperty, Mode=OneWay}" />
我要的是,在RxUid變化影響的uid屬性。
爲什麼我認爲這應該工作:Link - 「你可以覆蓋任何依賴屬性的元數據」。 例外了一些關於已註冊:/
- 列表項