C# 获取ComboBox数据模板中TextBlock的值
我有以下XAML:C# 获取ComboBox数据模板中TextBlock的值,c#,wpf,combobox,datatemplate,textblock,C#,Wpf,Combobox,Datatemplate,Textblock,我有以下XAML: <ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="6" Grid.Column="2" Name="cbo_team" VerticalAlignment="Top" Width="148" DataContext="{Binding ElementName=cbo_component, Path=SelectedItem}" Selected
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="6" Grid.Column="2"
Name="cbo_team" VerticalAlignment="Top" Width="148"
DataContext="{Binding ElementName=cbo_component, Path=SelectedItem}"
SelectedIndex="0">
<ComboBox.ItemsSource>
<Binding XPath="Teams/Team/@id"
Converter="{StaticResource xmlConverter}">
<Binding.ConverterParameter>
<local:XmlConverterParameter
XPathTemplate="/Products/Teams/Team[{0}]"
XPathCondition="@id='{0}'" />
</Binding.ConverterParameter>
</Binding>
</ComboBox.ItemsSource>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
在C#中,我试图获取
组合框中当前选定项的文本块的值。我该怎么做?基本相同,但唯一的答案没有帮助。查看此示例。textblock(组合框下方)显示组合框中当前选定xml元素的name属性值。将弹出一个消息框,其中显示的结果与视觉树中的查找结果相同。更改初始选择时,查找失败。看起来comboboxitems是在设置选定项后创建的
XAML:
代码隐藏,显示如何通过遍历可视树直接获取文本:
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;
}
私有无效OnComboxSelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
ComboBox ComboBox=发件人作为ComboBox;
ComboBoxItem ComboBoxItem=comboBox.ItemContainerGenerator.ContainerFromItem(comboBox.SelectedItem)作为ComboBoxItem;
如果(comboBoxItem==null)
{
返回;
}
TextBlock TextBlock=FindVisualChildByName(comboBoxItem,“nameTextBlock”);
MessageBox.Show(textBlock.Text);
}
私有静态T FindVisualChildByName(DependencyObject父对象,字符串名称),其中T:DependencyObject
{
for(int i=0;i
很抱歉聚会迟到了一点:)但是下面的方法也很有效(讽刺的是,它和你的方法是一样的!!)
private void ListBox\u SelectionChanged(对象发送者,selectionchangedventargs e)
{
ListBox ContactListBox=发送者作为ListBox;
ListBoxItem ListBoxItem=ContactListBox.ItemContainerGenerator.ContainerFromItem(ContactListBox.SelectedItem)作为ListBoxItem;
如果(listBoxItem==null)
{
返回;
}
TextBlock txtBlock=FindVisualChildByName(listBoxItem,“ListTextBlock”);
MessageBox.Show(txtBlock.Text);
}
私有静态T FindVisualChildByName(DependencyObject父对象,字符串名称),其中T:DependencyObject
{
for(int i=0;i
其他人已经建议使用SelectionChanged事件。没有测试下面的代码,但您可以尝试一下
private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
TextBlock tvContent = (sender as ComboBox).SelectedItem as TextBlock;
string content = tvContent.Text;
}
在组合框中有一个显示所选项目内容的辅助字段似乎是一种不必要的解决方法,只是为了能够在C#中读取该辅助字段。不过,我会记住这一点,因为这是一种丑陋的攻击。我刚刚在示例中添加了textblock,以向您展示如何从所选项目中获取name属性。不需要文本块。你把它和组合框项目的数据模板中使用的文本块混淆了吗?哦,明白了。当我检查C#中的SelectedItem
属性时,我得到了一个XML结构。我可以检查那里的Attributes
属性,并在C#中提取name
属性的值,但这似乎是多余的,因为name
属性已经在XAML中提取出来了-TextBlock
中显示了它。似乎我应该能够从组合框
中提取一个简单的字符串值--文本块
中显示的名称
属性,而不必担心该值的XML结构。问题是您没有对所选组合框项的文本块的引用。您需要使用代码隐藏在可视化树中搜索它。我编辑了我的答案,也展示了这种(不那么优雅的)方法。要使用微软的技术,你必须是一个策划者。我的智商非常低,所以是时候转向开源了。
TextBlock tb1 = (TextBlock)cbo_team.SelectedItem;
MessageBox.Show(tb1.Text);
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;
}
private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
TextBlock tvContent = (sender as ComboBox).SelectedItem as TextBlock;
string content = tvContent.Text;
}