如果我有一个PropertyPath,是否有可能获得它的属性?如果没有,我需要哪些最少的信息?从这个例子我需要得到SomeAttribute。我需要它我的自定义绑定类。来自PropertyPath的属性
例如,
Test.xaml
<TextBox Text={Binding SomeValue}/>
Test.xaml.cs
[SomeAttribute]
public string SomeValue { get; set; }
如果我有一个PropertyPath,是否有可能获得它的属性?如果没有,我需要哪些最少的信息?从这个例子我需要得到SomeAttribute。我需要它我的自定义绑定类。来自PropertyPath的属性
例如,
Test.xaml
<TextBox Text={Binding SomeValue}/>
Test.xaml.cs
[SomeAttribute]
public string SomeValue { get; set; }
通过的PropertyPath您可以采取的唯一属性或子属性。 阅读data binding overview了解更多信息。
您可以通过反射技术获得绑定属性的属性。
以下是示例代码。
SomeEntity.cs
public class SomeEntity
{
[SomeAttribute]
public string SomeValue { get; set; }
}
MainWindow.xaml
<Window x:Class="WpfApplication4.MainWindow" ...>
<StackPanel>
<TextBox Name="textBox" Text="{Binding SomeValue}"/>
<Button Click="Button_Click">Button</Button>
</StackPanel>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new SomeEntity();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Get bound object from TextBox.DataContext.
object obj = this.textBox.DataContext;
// Get property name from Binding.Path.Path.
Binding binding = BindingOperations.GetBinding(this.textBox, TextBox.TextProperty);
string propertyName = binding.Path.Path;
// Get an attribute of bound property.
PropertyInfo property = obj.GetType().GetProperty(propertyName);
object[] attributes = property.GetCustomAttributes(typeof(SomeAttribute), false);
SomeAttribute attr = (SomeAttribute)attributes[0];
}
}