2012-06-19 46 views
0

我曾經使用listbox.itemsource作爲我的e.Result。如何在可觀察集合中獲取值?

<ListBox Height="476" HorizontalAlignment="Left" Margin="11,17,0,0" Name="ListBox1" VerticalAlignment="Top" Width="434" Foreground="#FFF5F5F1" > 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
<StackPanel Orientation="Vertical"> 
          <TextBlock Height="40" HorizontalAlignment="Left" Margin="8,24,10,0" Name="txtBlockCustName" Text="{Binding CustName, Mode=OneWay}" VerticalAlignment="Top" FontSize="26" /> 

          <TextBlock Height="40" HorizontalAlignment="Left" Margin="8,24,0,0" Name="txtBlockCustEmail" Text="{Binding CustEmail, Mode=OneWay}" VerticalAlignment="Top" FontSize="26" /> 
    </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

我該如何獲得數據綁定值?

void proxy_FindProfileCompleted(object sender, FindProfileCompletedEventArgs e) 
     { 
      ListBox1.ItemsSource = e.Result; 
      ObservableCollection<Customer> Customers = this.ListBox1.ItemsSource as ObservableCollection<Customer>; 
     } 

我想從可觀察集合中獲取客戶名稱和客戶電子郵件。

+0

我很困惑。這些值是不是出現在列表框中,或者你是否想要獲得別的東西? – Josh

+0

@Josh,我想獲取列表框內的值,因爲它是textblock數據綁定,所以我不能使用正常的字符串name = textblock.text .. –

回答

1

我不明白,如果這是你所需要的,但不妨一試:

void proxy_FindProfileCompleted(object sender, FindProfileCompletedEventArgs e) 
{    
    ListBox1.ItemsSource = e.Result; 
    ObservableCollection<Customer> Customers = 
     this.ListBox1.ItemsSource as ObservableCollection<Customer>; 
    foreach(Customer cust in Customers) 
    { 
     // You can get cust.CustName 
     // and you can get cust.CustEmail 
    } 
} 
+0

thnx爲你的答案。它用於從observablecollection獲取一個值。如果我想只取得cust.custname的全部價值,該怎麼辦? –

0

你可以通過查看類CollectionViewSource開始。這會跟蹤當前項目(您可能必須在列表框綁定上登錄IsSynchronizedWithCurrentItem=true)。你可以綁定到這個,並傳遞它。

如果這沒有回答你的問題,我試圖想出一個更詳細的例子。

+0

Thnx.Can你給我看一些更詳細的例子嗎? –