2010-12-09 6 views
1

itemstemplate의 속성을 itemsource의 proptery에 바인딩하는 방법

public class ItemInfo : DependencyObject 
{ 
    public string Name { get; set; } 
    public ObservableCollection<SomeDataItem> DataValues { get; set; } 
    ... 
    Dependency object registration and event handling 
    ... 
} 

public class MyItemSource : ObservableCollection<ItemInfo> 
{ 
    ... 
} 

이제이 데이터를 항목을 표시하는 컨트롤이 사용자 지정 목록보기에 표시하고자합니다. 이를 위해 MyItemSource를 listview의 ItemSource로 설정하고 ItemTemplate을 정의했습니다. 그러나 ItemTemplate에 ItemInfo에 대한 액세스 권한이없는 것으로 보입니다. 이것은 내 XAML입니다.

ItemSource의 ItemInfo에 바인딩하는 대신 ItemInfoUserControl 속성에 바인딩합니다.이 속성은 ItemInfoUserControl 속성에 바인딩됩니다. itemstemplate의 속성을 itemsource의 속성에 바인딩하는 방법이 있습니까? 아니면 내가 궁극적으로 성취하려고 노력하고있는 대안적인 접근법이 있습니까?

고마워!

+0

는 당신이'X 설정하려는 것을는 사실에 기인 할 수있다 생각합니다. 아래 편집을 참조하십시오. – Donut

답변

1

글쎄, 우선 당신은 binding syntax 적절한 사용해야합니다

Name = "{Binding Name}" 

하고 대신 그냥 "Name = "Binding Name""와 "Data = "Binding DataValues""의

Data = "{Binding DataValues}" 

. 바인딩 표현식에 " {"과 " }"을 추가합니다.
ItemInfoUserControlDependencyProperties이다 오래 Name로하고 DataValues, 문제를 해결하기에 충분 될 수 있습니다. 그렇지 않은 경우 XAML에서 바인딩 할 수 있으려면 DependencyProperties으로 구현해야합니다. 사용자 지정 종속성 속성 정의에 대한 좋은 MSDN 기사는 here을 참조하십시오.


편집 : 또한, 단지 발견 - 당신은 x:NameName 모두를 설정하고 있습니다. MSDN에 this article에서 :

If Name is available as a property on the class, Name and x:Name can be used interchangeably as attributes, but a parse exception will result if both are specified on the same element. If the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load.

x:Name="itemInfoUserControl"을 제거하고 도움이되는지 확인합니다. Name`와`Name`을 동시에 :

+0

음, 죄송합니다, 저는 서두름했습니다. 사실 여기에 게시 할 때 바인딩 구문은 오타 였고 종속 속성을 정의했습니다. – user315648

+0

올바른 구문이 아닙니다. Name = "{Binding Path = Name"}? – wangburger

+0

'Name = {Binding Path = Name}'과'Name = {Binding Name} '은 동일합니다. http://msdn.microsoft.com/en-us/library/cc189022(v=vs.95).aspx – Donut

관련 문제