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

问题描述

你好!

我有一个带有组合框的gridview。
$
现在我想要在ComboBox中选择已经在C#程序中发生并评估选择。



正常例如:
Hello!
I have a gridview with a combo box.
Now I want if in the ComboBox a selection has taken place in the C # program back and evaluate the selection.

Normal would be for example:

推荐答案

根据你的描述,你想从DataGridTemplateColumn获取组合框文本,对吗?

According to your description, you want to get combobox text from DataGridTemplateColumn, am I right?

如果是,您可以查看以下示例:

If yes, you can take a look the following sample:

 <DataGrid
            Name="datagrid1"
            AutoGenerateColumns="False"
            CanUserAddRows="False"
            ItemsSource="{Binding phones}">
            <DataGrid.Columns>
                <DataGridTextColumn
                    Width="200"
                    Binding="{Binding phonen, Converter={StaticResource phoneconverter}}"
                    Header="Phone number" />
                <DataGridTemplateColumn
                    MinWidth="50"
                    CanUserSort="True"
                    Header="Status"
                    SortMemberPath="GrSpalte07CBStatus">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox
                                x:Name="taskCombo"
                                Width="100"
                                VerticalAlignment="Top"
                                FontFamily="Tahoma"
                                FontSize="11"
                                FontWeight="Bold"
                                Foreground="Black"
                                ItemsSource="{Binding DataContext.items, RelativeSource={RelativeSource AncestorType=local:Window26}}"
                                SelectedItem="{Binding selecteditem, UpdateSourceTrigger=PropertyChanged}">
                                <!--<ComboBoxItem Content="Open" TabIndex="1" />
                                <ComboBoxItem Content="Fixed" TabIndex="2" />
                                <ComboBoxItem Content="Forward" TabIndex="3" />
                                <ComboBoxItem Content="Delete" TabIndex="4" />-->
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button
            Name="btn1"
            Width="300"
            Height="30"
            Click="btn1_Click"
            Content="test" />




public class phoneviewmodel
    {
        public ObservableCollection<PhoneModel> phones { get; set; }
        private List<string> _items;
        public List<string> items
        {
            get { return _items; }
            set
            {
                _items = value;
               
            }
        }
        public phoneviewmodel()
        {
         items = new List<string>()
            {
                 "Item 1",
    "Item 2",
    "Item 3",
    "Item 4",
    "Item 5"
            };
            phones = new ObservableCollection<PhoneModel>()
            {
                new PhoneModel(){phonen="1231231234",selecteditem=items[0]},
                new PhoneModel(){phonen="1231231234" ,selecteditem=items[0]}
            };
           
        }

    }
    public class PhoneModel:ViewModelBase
    {
        public string phonen { get; set; }
        private string _selecteditem;
        public string selecteditem
        {
            get { return _selecteditem; }
            set
            {
                _selecteditem = value;
                RaisePropertyChanged("selecteditem");
            }
        }
       
       
    }


 public partial class Window26 : Window
    {
       
        public Window26()
        {
            InitializeComponent();
            this.DataContext = new phoneviewmodel(); ;
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            foreach (PhoneModel model in datagrid1.Items)
            {
                if(model!=null)
                {
                    var selecteditem = model.selecteditem;//here you have selected item
                    Console.WriteLine(selecteditem);
                }
               
            }
        }
    }

ViewModelBase是一个继承INotifyPropertyChanged接口的类。请设置Combobox selecteditem  UpdateSourceTrigger = PropertyChanged

The ViewModelBase is one class inherit INotifyPropertyChanged interface. Please set Combobox selecteditem UpdateSourceTrigger=PropertyChanged

最好的问候,

Cherry

Cherry


这篇关于如何在Gridview中查询ComboBox的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 13:37