DataGrid内部的组合框传递值

时间:2018-08-13 06:10:49

标签: c# wpf combobox datagrid

我在DataGrid中有Combobox。我只需要选择的值,它就会传递给我: System.Windows.ComboBoxItem:Zylinder x 我只想要: Zylinder x

我有点迷茫(DataGrid对我来说是如此令人困惑,也因为无法通过名称或类似方式在datagrid内部找到组件)我遇到了checkBox的类似问题,并且我通过Checked&Unchencked解决了这个问题。 >

XAML:

            <DataGrid x:Name="dg_material" Margin="10,10,10,0" HorizontalAlignment="Left" Height="142"  Width="700" CanUserAddRows="True" AddingNewItem="DataGrid_AddingNewItem" CanUserDeleteRows="True" AutoGenerateColumns="False" PreviewKeyDown="dg_material_PreviewKeyDown">

                    <DataGridTextColumn Header="SN" Binding="{Binding SN}" />
                    <DataGridTemplateColumn Header="Zylinder" x:Name="zylinder_name">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="cb_zyl01" SelectedItem="{Binding Zylinder}" SelectionChanged="cb_zyl01_SelectionChanged">
                                    <ComboBoxItem Name="z00">--</ComboBoxItem>
                                    <ComboBoxItem Name="z01">Zylinder 1</ComboBoxItem>
                                    <ComboBoxItem Name="z02">Zylinder 2</ComboBoxItem>
                                    <ComboBoxItem Name="z03">Zylinder 3</ComboBoxItem>
                                    <ComboBoxItem Name="z04">Zylinder 4</ComboBoxItem>
                                    <ComboBoxItem Name="z05">Zylinder 5</ComboBoxItem>
                                    <ComboBoxItem Name="z06">Zylinder 6</ComboBoxItem>
                                    <ComboBoxItem Name="z07">Zylinder 7</ComboBoxItem>
                                    <ComboBoxItem Name="z08">Zylinder 8</ComboBoxItem>
                                    <ComboBoxItem Name="z09">Zylinder 9</ComboBoxItem>
                                    <ComboBoxItem Name="z10">Zylinder 10</ComboBoxItem>
                                    <ComboBoxItem Name="z11">Zylinder 11</ComboBoxItem>
                                    <ComboBoxItem Name="z12">Zylinder 12</ComboBoxItem>
                                    <ComboBoxItem Name="z13">Zylinder 13</ComboBoxItem>
                                    <ComboBoxItem Name="z14">Zylinder 14</ComboBoxItem>
                                    <ComboBoxItem Name="z15">Zylinder 15</ComboBoxItem>
                                    <ComboBoxItem Name="z16">Zylinder 16</ComboBoxItem>
                                    <ComboBoxItem Name="z17">Zylinder 17</ComboBoxItem>
                                    <ComboBoxItem Name="z18">Zylinder 18</ComboBoxItem>
                                    <ComboBoxItem Name="z19">Zylinder 19</ComboBoxItem>
                                    <ComboBoxItem Name="z20">Zylinder 20</ComboBoxItem>
                                </ComboBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

        </StackPanel>

    </Grid>
</ScrollViewer>

后面的代码:

public partial class MaterialAdd : Window
{
    public MaterialAdd()
    {
        InitializeComponent();
        dg_material.ItemsSource = materialList; 

    }  
    List<Material> predej = new List<Material>();
    public List<Material> materialList = new List<Material>();
    bool Boxbool = false;

    private void DataGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
    {

        e.NewItem = new Material
            {
                Bezeichnung = Auto.Text,
                Menge = "",
                Box = Boxbool,
                SN = "",
                Zylinder = combostring
        };                
    }

    AutoCompleteBox Auto;


    private void dg_material_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        var uiElement = e.OriginalSource as UIElement;
        if (e.Key == Key.Enter && uiElement != null)
        {
            e.Handled = true;
            uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        }
    }

    string text = "";

    private void ac_mat1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if((sender as AutoCompleteBox).SelectedItem != null)
        {
            text = Auto.SelectedItem.ToString();
        }


    }

    public void predejtext()
    {
        string dg = null;
        foreach (Material x in dg_material.ItemsSource)
        {

            dg += x.Bezeichnung.ToString() + ";";                
            dg += x.Menge.ToString() + ";";
            dg += x.Box + ";";
            dg += x.SN.ToString() + ";";
            dg += x.Zylinder.ToString() + ";";
            GlobalZL.passstr = dg;
        }
    }

    private void BoxControl_Checked(object sender, RoutedEventArgs e)
    {
        Boxbool = true;
    }

    private void BoxControl_Unchecked(object sender, RoutedEventArgs e)
    {
        Boxbool = false;
    }

    private void ac_mat1_Initialized(object sender, EventArgs e)
    {
        Auto = (sender as AutoCompleteBox);
        Auto.ItemsSource = GlobalZL.list_zbozi;
    }

    string combostring = "";

    private void cb_zyl01_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {          
        if ((sender as ComboBox).SelectedItem != null)
        {
            combostring = (sender as ComboBox).SelectedValue.ToString();
            combostring = combostring.Remove(0, 37).Trim();                
        }
    }       
}

1 个答案:

答案 0 :(得分:0)

通过更改控件名称尝试以下代码

protected void btnClick_Click(object sender, EventArgs e)
{
    var tmpValue = ((DropDownList)gvRowItem.Cells[/*cellId*/].FindControl("/*dropDownListId*/")).SelectedValue;
}