如何从ListBox中选定的TextBlock元素中提取值?

时间:2011-06-02 11:01:18

标签: c# silverlight xaml windows-phone-7 silverlight-4.0

我正在使用ListBox来显示Dictionary<>中包含的所有值对象:

<ListBox Height="519" x:Name="ContactsListBox" Width="460" Margin="0,0,0,0" SelectionChanged="ContactsListBox_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Key}" Margin="5" Foreground="{StaticResource PhoneAccentBrush}"/>
                    <TextBlock x:Name ="LastNameData" Text="{Binding Value}" Margin="20, 0" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

内容由以下代码填写:

Dictionary<long, Contact> contacts = new Dictionary<long, Contact>();
this.ContactsListBox.ItemsSource = contacts;

现在,我想知道当前选择ListBox中哪个特定的“联系人”,或者通过知道它的Key,或者只是从“LastNameData”TextBlock中提取值。

我尝试过这样的事情,但显而易见它不起作用:

private void ContactsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBox lb = this.ContactsListBox.SelectedItem as ListBox;
    this.Test_SomeOtherTextBlock.Text = lb.ToString();
}

我真的很感谢你的帮助!

5 个答案:

答案 0 :(得分:3)

你甚至可以做下面的事情:

<ListBox Height="519" x:Name="ContactsListBox" Width="460" Margin="0,0,0,0" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Key}" Margin="5"/>
                        <TextBlock x:Name ="LastNameData" Text="{Binding Value}" Margin="20, 0" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Grid DataContext="{Binding ElementName=ContactsListBox, Path=SelectedItem}" Margin="0,470,0,0">
            <TextBlock Text="{Binding Value}"/>
        </Grid>

所以你不需要代码......

BR,

TJ

答案 1 :(得分:0)

您的代码很好,使用ListBox.SelectedItem是正确的方法。

我认为问题在于您应该将其投放为ListBoxItem,而不是ListBox。并且还要使用DataContext来获得它的价值,所以像这样的行(未经测试,我不确定在最后一行中访问DataContext值):

private void ContactsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBoxItem lbi = this.ContactsListBox.SelectedItem as ListBoxItem;
    var dataContext = lbi.DataContext;
    this.Test_SomeOtherTextBlock.Text = dataContext.Value.ToString();
}

答案 2 :(得分:0)

有几个问题:

在Xaml中,您可能不希望显示类名,而是显示合理的字符串,例如:

<TextBlock x:Name ="LastNameData" Text="{Binding Value.LastName}" Margin="20, 0" />

在选择处理中,所选项目是KeyValuePair&lt; ...&gt;。如果你在调试器中查看返回的类型,你可以自己轻松找到它。 (对于程序员来说应该是一种反射,因此上面的问题永远不会出现:))

private void ContactsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { 
    KeyValuePair<long, Contact> kv = (KeyValuePair<long, Contact>)this.ContactsListBox.SelectedItem;
    Contact c = (Contact)kv.Value;
    Debug.WriteLine(c.LastName);
}

答案 3 :(得分:0)

试试这个对我有用,会帮助你...

private void ListBox_SelectionChanged(object sender,SelectionChangedEventArgs e)

{

ListBox ContactListBox =发件人为ListBox;

ListBoxItem listBoxItem = ContactListBox .ItemContainerGenerator.ContainerFromItem(ContactListBox.SelectedItem)as ListBoxItem;

if(listBoxItem == null)

{

            return;

}

TextBlock txtBlock = FindVisualChildByName(listBoxItem,“ListTextBlock”);

MessageBox.Show(txtBlock.Text);

}

    private static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            string controlName = child.GetValue(NameProperty) as string;
            if (controlName == name)
            {
                return child as T;
            }
            T result = FindVisualChildByName<T>(child, name);
            if (result != null)
                return result;
        }
        return null;
    }

答案 4 :(得分:-1)


private void ContactsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBox lb = this.ContactsListBox.SelectedItem as ListBox;
    this.Test_SomeOtherTextBlock.Text = lb.ToString();
}
会是这样的......

private void ContactsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBox lb = this.ContactsListBox.SelectedItem as ListBox;
    DataTemplate template=lb.ContentTemplate;
    //Now here you have to extract the content of the data template 
      and then you need to extract the TextBlock from that content.

}

这只是对功能的概述。很抱歉无法发布完整的代码。