2011-02-17 5 views
0

PropertyPath가있는 경우 속성을 가져올 수 있습니까? 그렇지 않다면 최소한의 정보가 필요합니까? 예제에서 나는 SomeAttribute를 얻을 필요가있다. 내 맞춤 바인딩 클래스가 필요해.PropertyPath의 특성

예 :

Test.xaml

<TextBox Text={Binding SomeValue}/> 

Test.xaml.cs PropertyPath으로

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

답변

0

을 당신은 단지 재산이나 하위 속성이 걸릴 수 있습니다. 자세한 내용은 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]; 
    } 
}