2012-06-02 52 views
4

我是新來的WPF。在我的示例應用程序中,我使用ListView來顯示屬性的內容。我不知道如何將ListView中的SelectedItem綁定到屬性,然後綁定到TextBlock。ListView綁定中的SelectedItem

Window.xaml

<Window x:Class="Exec.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Main window" Height="446" Width="475" > 

    <Grid> 
     <ListView Name="ListViewPersonDetails" Margin="15,12,29,196" ItemsSource="{Binding Persons}" SelectedItem="{Binding CurrentSelectedPerson}"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="First name" DisplayMemberBinding="{Binding FirstName}"/> 
        <GridViewColumn Header="Last name" DisplayMemberBinding="{Binding LastName}"/> 
        <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}"/> 
       </GridView> 
      </ListView.View> 
     </ListView> 

     <TextBlock Height="23" Name="textFirstNameBlock" FontSize="12" Margin="97,240,155,144"> 
       <Run Text="Name: " /> 
       <Run Text="{Binding CurrentSelectedPerson.FirstName}" FontWeight="Bold" /> 
     </TextBlock> 

     <TextBlock Height="23" Name="textLastNameBlock" FontSize="12" Margin="97,263,155,121"> 
       <Run Text="Branch: " /> 
       <Run Text="{Binding CurrentSelectedPerson.LastName}" FontWeight="Bold" /> 
     </TextBlock> 

     <TextBlock Height="23" Name="textAddressBlock" FontSize="12" Margin="0,281,155,103" HorizontalAlignment="Right" Width="138"> 
       <Run Text="City: " /> 
       <Run Text="{Binding CurrentSelectedPerson.Address}" FontWeight="Bold" /> 
     </TextBlock> 

    </Grid> 
</Window> 

MainWindow.xaml.cs

Tman manager = new Tman(); 

     private List<Person> persons; 
     public List<Person> Persons 
     { 
      get 
      { 
       return this.persons; 
      } 

      set 
      { 
       if (value != null) 
       { 
        this.persons = value; 
       } 

      } 
     } 

     private Person currentSelectedPerson; 
     public Person CurrentSelectedPerson 
     { 
      get 
      { 
       return currentSelectedPerson; 
      } 
      set 
      { 
       this.currentSelectedPerson = value; 
      } 
     } 


     private void Window_Loaded(object sender, RoutedEventArgs e){ 
      ListViewPersonDetails.ItemsSource= manager.GetPersons(); 

     } 

Person.cs

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

     public string LastName 
     { 
      get; 
      set; 
     } 

     public string Address 
     { 
      get; 
      set; 
     } 

    } 

感謝您的幫助。

+0

NotifyPropertyChanged需要發送屬性的名稱,您是否因某種原因試圖發送不同的字符串?不知道這是唯一的問題,這個列表是否填充了? –

+0

你現在得到什麼樣的行爲?你是否設置了窗口的DataContext?它需要設置爲那些綁定工作。此外,您正在設置ItemsSource兩次,一次在XAML中,一次在代碼中。你只需要設置一次。 –

+0

@KDiTraglia對不起,我忘了刪除NotifyPropertyChanged。知道它的工作原理,但我不知道如何將CurrentSelectedPerson屬性項綁定到TextBlocks .. – Matt

回答

8
private void Window_Loaded(object sender, RoutedEventArgs e){ 
    ListViewPersonDetails.ItemsSource= manager.GetPersons(); 
} 

這可能是你的問題,你不應該像這樣設置itemssource。只需更換爲這條線......

this.DataContext = this; 

這就是將您的屬性綁定到UI,以便所有這些綁定語句有任何意義。

此外,您還需要更新您的文本塊上的綁定,以實際匹配Person類中的屬性名稱。

<TextBlock Height="23" Name="textFirstNameBlock" FontSize="12" Margin="97,240,155,144"> 
    <Run Text="Name: " /> 
    <Run Text="{Binding CurrentSelectedPerson.FirstName}" FontWeight="Bold" /> 
</TextBlock> 

<TextBlock Height="23" Name="textLastNameBlock" FontSize="12" Margin="97,263,155,121"> 
    <Run Text="Branch: " /> 
    <Run Text="{Binding CurrentSelectedPerson.LastName}" FontWeight="Bold" /> 
</TextBlock> 

<TextBlock Height="23" Name="textAddressBlock" FontSize="12" Margin="0,281,155,103" HorizontalAlignment="Right" Width="138"> 
    <Run Text="City: " /> 
    <Run Text="{Binding CurrentSelectedPerson.Address}" FontWeight="Bold" /> 
</TextBlock> 

編輯:

經測試,在VS,你需要更多的1件小的事情,以實現對CurrentSelectedPerson財產INotifyPropertyChanged的...

private Person currentSelectedPerson; 
public Person CurrentSelectedPerson 
{ 
    get { return currentSelectedPerson; } 
    set 
    { 
     currentSelectedPerson = value; 
     if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CurrentSelectedPerson")); 
    } 
} 

作爲替代這個作品也有點更簡單...

<TextBlock Height="23" Name="textFirstNameBlock" FontSize="12" Margin="97,240,155,144"> 
    <Run Text="Name: " /> 
    <Run Text="{Binding ElementName=ListViewPersonDetails, Path=SelectedItem.FirstName}" FontWeight="Bold" /> 
</TextBlock> 

(對其他文本框重複相似的邏輯)

+0

謝謝,這有幫助。在CurrentSelectedPerson屬性中是SelectedItem。你知道我可以如何將CurrentSelectedPerson項目綁定到TextBlocks? – Matt

+1

它應該像列出的一樣工作,只需更正名稱以實際匹配屬性(currentSelectedPerson.FirstName而不是FstNamePerson)即可。將編輯帖子。 –

+0

我不知道爲什麼,但它不起作用。我編輯了我的第一篇文章。無論如何,感謝您的幫助。 – Matt