简体   繁体   English

WPF Datagrid组合框XAML和代码隐藏

[英]WPF Datagrid Combobox XAML and Codebehind

Since I am a beginner in WPF, I have a question which might be basic in nature. 因为我是WPF的初学者,所以我有一个本质上可能是基本的问题。

I have a datagrid which has a combobox. 我有一个带有组合框的数据网格。

The xaml which I have written is: 我写的xaml是:

<DataGridComboBoxColumn Header="ControlOption" Width="100"
       SelectedItemBinding="{Binding Path=DataGridComboxBox_Control}">
    <DataGridComboBoxColumn.ItemsSource>
        <col:ArrayList>
            <sys:String>Database1</sys:String>
            <sys:String>DataBase2</sys:String>
        </col:ArrayList>
    </DataGridComboBoxColumn.ItemsSource>
</DataGridComboBoxColumn>

I have 3 values which should be there in the drop down of the comboBox. 我有3个值,应该在comboBox的下拉列表中。 1. WorkStream1 2. WorkStream2 3. WorkStream3 1. WorkStream1 2. WorkStream2 3. WorkStream3

Now how do I write the codebehind for the ComboxBox. 现在,我该如何编写ComboxBox的代码隐藏。 Please do give me pointers in this. 请在这方面给我指点。 :) :)

Very much appreciated. 非常感谢。

Ashutosh 阿舒托什

Sorry, haven't quite understood you. 抱歉,还不太了解您。 Do you want to replace the itemssource of combobox from code behind? 是否要从后面的代码替换combobox的itemssource? In this case you can to bind combobox's ItemsSource to property. 在这种情况下,您可以将组合框的ItemsSource绑定到属性。 Like that: 像那样:

<Controls:DataGridComboBoxColumn Header="Gender" ItemsSource="{Binding Path=Genders}" />

and in code behind will be smth like 在后面的代码中会像

    public ObservableCollection<string> Genders
    {
        get {
            return _genders;
        }
        set { _genders = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Genders"));
        }
    }

If you meant smth else, explain please. 如果您还有其他意思,请解释。 May be I've understood you in a wrong way 可能是我对您的理解有误

use datagrid_PreviewKeyDown function to capture keypressed in grid. 使用datagrid_PreviewKeyDown函数捕获在网格中按下的键。 if the cell is focused use datagrid.BeginEdit() to convert cell from text block to combo box. 如果单元格是焦点,请使用datagrid.BeginEdit()将单元格从文本块转换为组合框。 i think this will help to solve the issue. 我认为这将有助于解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM