简体   繁体   English

组合框选择的项目在datagrid内部不起作用

[英]combobox selected item not working inside datagrid

I have a datagrid in which I put one column as TemplateColumn. 我有一个数据网格,其中我将一列作为TemplateColumn。

Here is my Datagrid: 这是我的Datagrid:

<DataGrid AutoGenerateColumns="False"
          x:Name="MainDataGrid"
          ItemsSource="{Binding OrderItems}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Width="230" Header="Product Name">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ProductName,Mode=OneWay}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox 
                        Text="{Binding ProductName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                        <vw:CustomDatagrid />
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

In the CellEditingTemplate I put one Combobox in that I put one UserControl . 在CellEditingTemplate中,我放置了一个组合框,其中放置了一个UserControl。

Here is my UserControl: 这是我的UserControl:

<UserControl x:Class="RH_Maize.View.CustomDatagrid"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" Height="499" Width="560">
<Grid x:Name="LayoutRoot"  Width="560">
    <DataGrid ItemsSource="{Binding FilterdItems}"
              SelectedItem="{Binding SelectedFilterItem,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
              x:Name="CustomDataGrid"
             >
        <DataGrid.Columns>
            <DataGridTextColumn MinWidth="150"
                                Header="Category"
                                Binding="{Binding CategoryName,Mode=OneWay}"/>
            <DataGridTextColumn MinWidth="180"
                                Header="Item"
                                Binding="{Binding ProductName,Mode=OneWay}" />
            <DataGridTextColumn MinWidth="130"
                                Header="Rate"
                                Binding="{Binding Rate,Mode=OneWay}" />
            <DataGridHyperlinkColumn MinWidth="100" Header="Details"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

My requirement is : 我的要求是:

When the Combobox text changes I populate the CustomDatagrid with Items ,user can select an item from the CustomDatagrid, the selectedItem(ProductName) is displayed on the Combobox. 当组合框文本更改时,我用Items填充CustomDatagrid,用户可以从CustomDatagrid中选择一个项目,则selectedItem(ProductName)将显示在组合框上。

My viewModel: 我的viewModel:

public class ProductCartViewModel : ViewModelBase
{
    public ProductCartViewModel()
    {
        PopulateCustomGrid(string.Empty);
    }

    private string _productName;
    public string ProductName   
    {
        get { return _productName; }
        set
        {
            if (value != _productName)
            {
                _productName = value;
                PopulateCustomGrid(_productName);
                RaisePropertyChanged(() => ProductName);
            }
        }
    }

    private void PopulateCustomGrid(string productNameMatch)
    {
        List<ProductFilterModel> lstProduct = new List<ProductFilterModel>();
        using(var context=new MaizeEntities())
        {
            var items = from p in context.TblProducts
                        where p.ProductName.Contains(productNameMatch)
                        select p;


           foreach(var item in items)
           {
               ProductFilterModel product = new ProductFilterModel();
               product.CategoryName = item.TblProductCategory.CategoryName;
               product.ProductId = item.ProductId;
               product.ProductCode = item.ProductCode;
               product.ProductName = item.ProductName;
               product.Rate = item.PurchaseRate;

               lstProduct.Add(product);
           }
           FilterdItems = new ObservableCollection<ProductFilterModel>(lstProduct);
        }
    }
}

My problem: 我的问题:

When the user select an item from the CustomDataGrid the combobox.Text got the "RH_Maize.View.CustomDatagrid" text instead of ProductName .Whats wrong with my code ? 当用户从CustomDataGrid中选择一个项目时,combobox.Text获得了“ RH_Maize.View.CustomDatagrid”文本,而不是ProductName。我的代码怎么了?

I would suggest you to go for popup approach which look like a textbox with a button at end to open popup 我建议您使用弹出方法,该方法看起来像一个文本框,该文本框的末尾有一个用于打开弹出窗口的按钮

So the template for the column will have a textbox where user can type and a popup where you can host child controls (datagrid in your case) 因此,该列的模板将具有一个用户可以在其中键入文本框和一个可以在其中承载子控件的弹出窗口(在您的情况下为datagrid)

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="auto" />
    </Grid.ColumnDefinitions>
    <TextBox x:Name="text" />
    <ToggleButton Grid.Column="1"
                  Content="..."
                  x:Name="toggle" />
    <Popup PlacementTarget="{Binding ElementName=text}"
           IsOpen="{Binding IsChecked, ElementName=toggle}">
        <Border Background="White">
            <TextBlock Text="your child control here, eg datagrid" />
        </Border>
    </Popup>
</Grid>

popup will be controlled by the button at the end you can also make it open by clicking the text box too 弹出窗口将由最后的按钮控制,您也可以通过单击文本框将其打开

inside popup you can place your data grid etc. 在弹出窗口中,您可以放置​​数据网格等。

DataGrid in popup 弹出中的DataGrid

    <Popup PlacementTarget="{Binding ElementName=text}"
           IsOpen="{Binding IsChecked, ElementName=toggle}">
        <Border Background="White">
            <vw:CustomDatagrid SearchKey="{Binding Text, ElementName=text}}"/>
        </Border>
    </Popup>

binding a assumed property SearchKey in your custom control CustomDatagrid to Text property of TextBox, this binding will push the value of text box to control where you can perform filter search or other logic to populate your data in grid. 将自定义控件CustomDatagrid的假定属性SearchKey绑定到TextBox的Text属性,此绑定将推送文本框的值以控制您可以在何处执行过滤器搜索或其他逻辑,以在网格中填充数据。

Clarification on introduction of property SearchKey 澄清引入属性SearchKey

When the Combobox text changes I populate the CustomDatagrid with Items ,user can select an item from the CustomDatagrid, the selectedItem(ProductName) is displayed on the Combobox.

to me it mean that you need to pass the user typed text to the CustomDatagrid and populate the grid, and when user select an item from data grid you want to display it in combo box. 对我来说,这意味着您需要将用户键入的文本传递给CustomDatagrid并填充网格,并且当用户从数据网格中选择一个项目时,您想要在组合框中显示它。 so I this assumed property SearchKey, which will help you to pass the text data in a MVVM way. 因此,我假设此属性为SearchKey,它将帮助您以MVVM方式传递文本数据。

You can choose to have any name for the property, you'd we able to populate the grid as long as you receive the text typed by user 您可以选择使用该属性的任何名称,只要您收到用户键入的文本,我们就可以填充表格

you may choose to pass in the view model also in order to set properties, execute some methods etc. 您也可以选择传入视图模型以设置属性,执行某些方法等。

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

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