2010-01-21 4 views
0

개체에 데이터 컨텍스트를 바인딩 한 목록 상자가 있습니다. 이 객체에는 여러 속성이 있으며 그 중 일부 속성에는 특정 속성이 있습니다.WPF 특정 특성을 가진 개체의 속성에 바인딩

내가 원하는 것은 항목 소스를 개체의 속성으로 설정하고 특정 속성 집합이있는 속성 만 표시하는 것입니다.

아무도 내가 이것을 시작할 수있는 곳으로 도울 수 있습니까?

답변

2

당신은이 속성의 값을 얻기 위해 LINQ와 반사를 사용할 수 있습니다 속성 세트 :

Class1 class1 = new Class1 { Name = "Sam", DOB = DateTime.Now, SSN = "123" }; 

MyListBox.ItemsSource = from p in typeof(Class1).GetProperties() 
         where p.IsDefined(typeof(Att), false) 
         select p.GetValue(class1, null); 

이름과 생년월일은 내 테스트에서 [ATT]로 표시되며, 그 값은 목록 상자에 추가됩니다. SSN은 그렇지 않습니다.

0

하나의 방법은 데이터 컨텍스트 object dynamically을 빌드하고이 동적으로 빌드 된 객체의 속성에 가시성 속성을 바인딩하는 것입니다. 그런 다음 다음과 같은 방법을 사용합니다 : 뷰에서

var provider = new MyDynamicProvider(); 
// Add the names of the properties with the particular attribute with 
// initial values (found using reflection elsewhere). 
provider.MyValues.Add("PropertyWithAttribute", "Test"); 
provider.MyValues.Add("PropertyWithAttributeVisibility", Visibility.Visible); 
// Add properties that do not have the attribute 
provider.MyValues.Add("PropertyWithoutAttributeVisibility", Visibility.Collapsed); 
view.DataContext = provider.CreateDynamicWrapper(); 

이제 다음을 수행 할 수 있습니다

<TextBlock 
    Visibility="{Binding PropertyWithAttributeVisibility}" 
    Text="{Binding PropertyWithAttribute}" 
    /> 
관련 문제