如何从ComboBox中的文本块获取价值 [英] How to get value from textblock in ComboBox

查看:132
本文介绍了如何从ComboBox中的文本块获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有组合框,其中有itemtemplate包含两个文本块,即.对于ID&名称

I  have combobox which has itemtemplate which contain two textblock ie. for id & name,

值来自数据库.现在我有一个问题是,如何获取ID或Name的值.

value for ID and Name is come from database. Now I have a Question is that, How i get values of ID or Name.

<ComboBox Name="cmb1" Width="70" ItemsSource="{Binding}" 

    HorizontalAlignment="Left" Margin="95.5,118.5,0,0" Height="25" 

    VerticalAlignment="Top" SelectionChanged="cmb1_SelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate x:Name="cmbDTem">
                 <StackPanel Orientation="Horizontal">
                    <TextBlock Name="txtID" Text="{Binding CategoryID}" Width="10"/>
                     <TextBlock Name="txtName" Text="{Binding CategoryName}" Width="100"/>
                  </StackPanel>
             </DataTemplate>
        </ComboBox.ItemTemplate>
 </ComboBox>

在Initialize()

In the Initialize()

cmb1.DataContext = ds.Tables[0].DefaultView; 

在组合框选择更改中,我要显示选择的值

In Combobox selection changes i want to display the value which selected

 private void cmb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
           ?????????
        } 

 

推荐答案

您可以通过当前选定的项目访问它. ComboBox应该具有一个名为"SelectedItem"或"SelectedValue"的属性.它应该包含当前选择的表行.然后,您可以将该SelectedItem强制转换为表行数据类型,然后访问该强制转换项目的CategoryName或CategoryID属性.看起来像这样:

You can access that through the currently selected item. The ComboBox should have a property called "SelectedItem" or "SelectedValue". This should contain the currently selected table row. You can then cast that SelectedItem as your table row data type, then access the CategoryName or CategoryID property on that casted item. Would look something like this:

int categoryID = (cmb1.SelectedItem as MyTableRowClass).CategoryID;


这篇关于如何从ComboBox中的文本块获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆