2012-03-09 55 views
2

對不起,這是一個非常簡單的問題。但是目前我實際上並不知道要谷歌什麼。如何將IEnumerable <string>綁定到ListBox?

如果我有這樣的對象:

public class Employee 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

在我的虛擬機我公司員工的(public List<Employee> Employees)的列表。這很容易在我的XAML中綁定這個到ListBox:

<ListBox ItemsSource={Binding Employees}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding FirstName}"/> 
       <TextBlock Text="{Binding LastName}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

但是,如果我的員工的列表將包含字符串,而不是Employee對象?我怎樣才能將這些字符串值綁定到數據模板中的TextBlock

+0

你必須設置綁定模式,在這種情況下'mode =「two-way」' – David 2012-03-09 07:47:04

+0

爲什麼我需要設置文本塊的雙向綁定?單向綁定的作用是「Property => UI-Element方式」。另一種方式需要雙向綁定(UI-E => Prop)。並且您不能更改文本塊中的數據。 – 2012-03-09 10:12:20

回答

4

只需寫入{Binding}指定綁定的位置。

+0

謝謝!以前從未使用過。 – 2012-03-09 07:47:42

+0

你也可以使用'{Binding Path =。}'這是同樣的事情,但如果你想要一個雙向綁定,很有用,因爲雙向綁定需要設置路徑。 – 2016-03-17 21:13:14

相關問題