2012-06-19 21 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你给我看一些更详细的例子吗? –