2012-08-15 3 views
0

제목이 새겨 져 있습니다. 더 잘하면 편집하십시오. 나는이 같은 자원 WPF에서 만든 객체를 사용하는 경우메인 윈도우에있는 오브젝트를 붙이십시오.

내가 작업 코드를 가지고 :

<Grid> 
     <Grid.Resources> 
      <local:PersonView x:Key="Persons"/> 
      <CollectionViewSource x:Key="ViewPersons" Source="{Binding Source={StaticResource Persons}, Path=Persons}"> 
       <CollectionViewSource.GroupDescriptions> 
        <PropertyGroupDescription PropertyName="Name"/> 
       </CollectionViewSource.GroupDescriptions> 
      </CollectionViewSource> 
     </Grid.Resources> 
     <ListView ItemsSource="{Binding Source={StaticResource ViewPersons}}"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Name}"/> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </Grid> 
:

<local:PersonView x:Key="Persons"/> 

하지만 MainWindow를에서 만든 객체를 사용하려면, 여기에 작업 코드

public class Person 
    { 
     public string Name { get; set; } 
    } 

    public class PersonView 
    { 
     public ObservableCollection<Person> Persons { get; set; } 

     public PersonView() 
     { 
      Persons = new ObservableCollection<Person>(); 
      Persons.Add(new Person() { Name = "Luis" }); 
      Persons.Add(new Person() { Name = "Gusth" }); 
     } 
    } 

이 코드가 작동,하지만 난 CollectionViewSource의 t을 바인딩 할 :

클래스는 내가 사용 MainWindow를에서 만든 오 개체 :

public partial class MainWindow : Window 
    { 
     public PersonView BindThis { get; set; } 

     public MainWindow() 
     { 
      InitializeComponent(); 

      BindThis = new PersonView(); 
     } 
    } 

나는 이것을 시도하지만, 일을 해달라고 :

<CollectionViewSource x:Key="ViewPersons" Source="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=BindThis}"> 
       <CollectionViewSource.GroupDescriptions> 
        <PropertyGroupDescription PropertyName="Name"/> 
       </CollectionViewSource.GroupDescriptions> 
      </CollectionViewSource> 

답변

0

SRY들, 나는이 문제를 해결하기 위해 노력 오랜 시간을 보냈다. 하지만 지금은 제대로 작동합니다. 이 바인딩 된 것들은 여전히 ​​많은 것을 혼란스럽게합니다. 누군가가 도움이되기를 바랍니다.

수정 :

public class PersonView : ObservableCollection<Person> 
{ 
    public PersonView() 
    { 
     Add(new Person() { Name = "Luis" }); 
     Add(new Person() { Name = "Gusth" }); 
    } 
} 
관련 문제