2013-04-25 2 views
0

그래서 마침내 WinForms에서 WPF로 이동하기로 결정했습니다. 흥미로운 여행을하고 있습니다. 나는 ObservableCollectionListBox에 묶는 간단한 어플리케이션을 가지고있다.WPF의 컬렉션에 바인딩

namespace MyTestApp 
{ 
    public class Animal 
    { 
     public string animalName; 
     public string species; 

     public Animal() 
     { 
     } 

     public string AnimalName { get { return animalName; } set { animalName = value; } } 
     public string Species { get { return species; } set { species = value; } } 
    } 
} 

AnimalList 기업 :

가 나는 Animal 기업이

namespace MyTestApp 
{ 
    public class AnimalList : ObservableCollection<Animal> 
    { 
     public AnimalList() : base() 
     { 
     } 
    } 
} 

을 그리고 마지막으로 여기 내 메인 창입니다 :

<Window x:Class="MyTestApp.Window3" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:MyTestApp" 
    Title="Window3" Height="478" Width="563"> 

<Window.Resources> 
    <local:AnimalList x:Key="animalList"> 
     <local:Animal AnimalName="Dog" Species="Dog"/> 
     <local:Animal AnimalName="Wolf" Species="Dog"/> 
     <local:Animal AnimalName="Cat" Species="Cat"/> 
    </local:AnimalList>  
</Window.Resources> 

<Grid> 
    <StackPanel Orientation="Vertical" Margin="10,0,0,0"> 
     <TextBlock FontWeight="ExtraBold">List of Animals</TextBlock> 
     <ListBox ItemsSource="{Binding Source={StaticResource animalList}, Path=AnimalName}"></ListBox> 
    </StackPanel> 
</Grid> 

이제 응용 프로그램을 실행하면 목록 상자에 "Dog", "Wolf"및 "Cat"대신 "D", "o"및 "g"가 채워집니다.

enter image description here

어딘가에 어리석은 짓을하고 있다는 강한 느낌이 들지만 (AnimalList 생성자일까요?)하지만 그게 뭔지 알 수는 없습니다. 어떤 도움을 주셔서 감사합니다.

답변

1

DisplayMemberPath (바인딩의 Path 속성과 반대)를 설정해야합니다. 당신이 동물 개체 목록에 결합되어 있기 때문에

<Grid> 
    <StackPanel Orientation="Vertical" Margin="10,0,0,0"> 
     <TextBlock FontWeight="ExtraBold">List of Animals</TextBlock> 
     <ListBox ItemsSource="{Binding Source={StaticResource animalList}}" DisplayMemberPath="AnimalName"></ListBox> 
    </StackPanel> 
</Grid> 

DisplayMemberPath는 목록 항목으로 표시 할 동물 클래스의 속성의 이름을 지정합니다. 속성이 객체 자체 인 경우

, 당신은 당신은 animalname에 목록 상자를 바인딩하고

<ListBox ItemsSource="{Binding Source={StaticResource animalList}}" DisplayMemberPath="PropertyInAnimalClass.PropertyInTheChildObject.PropertyToDisplay" /> 
+0

빙고. 그것은 트릭을했다. – PoweredByOrange

0

.. 당신이 즉, 표시하려는 속성의 전체 경로를 지정하는 점 표기법을 사용할 수 있습니다. 대신 목록 상자를 컬렉션에 바인딩해야합니다.

<ListBox ItemsSource="{Binding Source={StaticResource animalList}}"></ListBox> 

바인딩에서 path=AnimalName을 삭제했습니다.

ListBox가 Animal을 표시하는 방법을 모르므로 ToString -method를 호출하므로 클래스 이름이 표시됩니다. 당신의 DataContext가 Animal의 인스턴스 인 ItemTemplate을 내부

<ListBox ItemsSource="{Binding Source={StaticResource animalList}}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel>  
       <TextBlock Text="{Binding AnimalName}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

그리고 당신은 그 인스턴스의 속성에 바인딩 할 수 있습니다 :

은 당신과 같이 그에게 ItemTemplate을을 제공함으로써이 문제를 해결할 수 있습니다. 예제에서는 AnimalName을 바인딩했지만 기본적으로 일반적인 XAML 컨트롤을 사용하고 바인딩 된 개체의 여러 속성에 바인딩 할 템플릿을 만듭니다.