在 ComboBox DataTemplate 中获取 TextBlock 的值

问题描述 投票:0回答:5
c# wpf combobox datatemplate textblock
5个回答
0
投票

检查这个样本。文本块(组合框下方)显示组合框中当前选定的 xml 元素的名称属性的值。将弹出一个消息框,其中包含在可视化树中查找的相同结果。初始选择更改时查找失败。看起来组合框项目是在设置所选项目后创建的。

XAML:

<Window x:Class="CBTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">

    <Window.Resources>
        <XmlDataProvider x:Key="UsersData" XPath="Users">
            <x:XData>
                <Users xmlns="">
                    <User name="Sally" />
                    <User name="Lucy" />
                    <User name="Linus" />
                    <User name="Charlie" />
                </Users>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>

    <StackPanel>

        <ComboBox 
            Name="_comboBox"
            ItemsSource="{Binding Source={StaticResource UsersData},XPath=*}"
            SelectedIndex="0"
            SelectionChanged="OnComboBoxSelectionChanged">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding XPath=@name}" Name="nameTextBlock" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

        <!-- Below shows how to get the value of selected item directly from the data. -->
        <TextBlock 
            DataContext="{Binding Path=SelectedItem, ElementName=_comboBox}"
            Text="{Binding XPath=@name}" />

    </StackPanel>

</Window>

后面的代码,展示了如何通过遍历可视化树直接获取文本:

private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox comboBox = sender as ComboBox;
    ComboBoxItem comboBoxItem = comboBox.ItemContainerGenerator.ContainerFromItem(comboBox.SelectedItem) as ComboBoxItem;
    if (comboBoxItem == null)
    {
        return;
    }
    TextBlock textBlock = FindVisualChildByName<TextBlock>(comboBoxItem, "nameTextBlock");
    MessageBox.Show(textBlock.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;
}

0
投票

抱歉来晚了一点 :) 但以下也有效(具有讽刺意味的是和你在同一个修复中!!)

TextBlock tb1 = (TextBlock)cbo_team.SelectedItem;
MessageBox.Show(tb1.Text);

0
投票
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBox ContactListBox = sender as ListBox;
    ListBoxItem listBoxItem = ContactListBox .ItemContainerGenerator.ContainerFromItem(ContactListBox.SelectedItem) as ListBoxItem;
    if (listBoxItem == null)
    {
        return;
    }
    TextBlock txtBlock = FindVisualChildByName<TextBlock>(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;
}

0
投票

其他人已经建议使用 SelectionChanged 事件。没有测试下面的代码,但你可以试一试。

private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
    TextBlock tvContent = (sender as ComboBox).SelectedItem as TextBlock;

    string content = tvContent.Text;
}

0
投票
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var comboBox = (ComboBox)sender;
    ComboBoxItem comboAsTextblock = (ComboBoxItem)comboBox.SelectedItem;
    string comboBoxItemText = comboAsTextblock.Content.ToString();
    // comboBoxItemText is what you want :)
}
© www.soinside.com 2019 - 2024. All rights reserved.