2011-02-17 40 views
0

如果我有一個PropertyPath,是否有可能獲得它的屬性?如果沒有,我需要哪些最少的信息?從這個例子我需要得到SomeAttribute。我需要它我的自定義綁定類。來自PropertyPath的屬性

例如,

Test.xaml

<TextBox Text={Binding SomeValue}/> 

Test.xaml.cs

[SomeAttribute] 
public string SomeValue { get; set; } 

回答

0

通過的PropertyPath您可以採取的唯一屬性或子屬性。 閱讀data binding overview瞭解更多信息。

0

您可以通過反射技術獲得綁定屬性的屬性。

以下是示例代碼。

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]; 
    } 
}