简体   繁体   English

WPF MVVM 如何将 ComboBox SelectedItem 绑定到特定的 DataGrid 选定行列值

[英]WPF MVVM How to bind ComboBox SelectedItem to specific DataGrid selected row column value

I have a ViewModel (parent) that is the parent of two usercontrols (let's say UCdatagrid and UCEditControls), the usercontrols have no viewmodels and are using the parent viewmodels DataContext.我有一个 ViewModel(父级),它是两个用户控件(比如说 UCdatagrid 和 UCEditControls)的父级,用户控件没有视图模型并且正在使用父视图模型 DataContext。

What I want to do is perform CRUD operations on the UCDatagrid with the UCEditControls which consist of Textboxes and Comboboxes.我想要做的是使用由文本框和组合框组成的 UCEditControls 在 UCDatagrid 上执行 CRUD 操作。 The Comboboxes are prepopulated with data.组合框预先填充了数据。 W W

So far I have everything working except the combobox link.到目前为止,除了 combobox 链接之外,我已经完成了所有工作。 The textbox (Code) is filled correctly on datagrid row selection fine.文本框(代码)在数据网格行选择上正确填充。 When the user clicks a datagrid row I wish to take the corresponding Type column value and have that show as the selected item in the Type combobox so updates can be made.当用户单击数据网格行时,我希望获取相应的类型列值并将其显示为类型 combobox 中的选定项,以便可以进行更新。

I'm using Caliburn Micro.我正在使用 Caliburn Micro。

ParentViewModel.cs父视图模型.cs

public class ConductorProductViewModel : Screen
{
    private readonly IDataConnection _connect;

    private ProductModel _selectedProduct;
    public ProductModel SelectedProduct
    {
        get { return _selectedProduct; }
        set
        {
            _selectedProduct = value;
            
            NotifyOfPropertyChange(() => SelectedProduct);          
        }
    }

    private TypeModel _selectedType;
    public TypeModel SelectedType
    {
        get { return _selectedType; }
        set { _selectedType = value; NotifyOfPropertyChange(() => SelectedType); }
    }

    private BindableCollection<ProductModel> _productList;
    public BindableCollection<ProductModel> ProductList
    {
        get { return _productList; }
        set { _productList = value; NotifyOfPropertyChange(() => ProductList); }
    }

    private BindableCollection<TypeModel> _types;
    public BindableCollection<TypeModel> Types
    {
        get { return _types; }
        set { _types = value; NotifyOfPropertyChange(() => Types); }
    }

    public ConductorProductViewModel(IDataConnection connect)
    {
        _connect = connect;

        GetProducts();
        LoadComboBoxLists();
    }

    private void LoadComboBoxLists()
    {
        Types = new BindableCollection<TypeModel>(_connect.Types_GetAll());
    }

    private async void GetProducts()
    {
        var allProducts = await _connect.Products_GetAll();
        ProductList = new BindableCollection<ProductModel>(allProducts);
    }
 }

DataGrid.Xaml数据网格.Xaml

<UserControl x:Class="...>    
    <Grid>
        <DataGrid ItemsSource="{Binding ProductList}" SelectedItem="{Binding SelectedProduct}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Code" Binding="{Binding Code}"/>
                <DataGridTextColumn Header="Type" Binding="{Binding Type}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

EditControls.Xaml EditControls.Xaml

<UserControl x:Class="...>    
    <Grid>
        <TextBox Text="{Binding SelectedProduct.Code}" />

        <ComboBox Name="TypeCB" ItemsSource="{Binding Types}"
                  SelectedItem={Binding SelectedType}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Type}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

     </Grid>
</UserControl>

Image of what im trying to achieve https://i.stack.imgur.com/OGPzP.png我试图实现的图像 https://i.stack.imgur.com/OGPzP.png

Found help with the solution from this post这篇文章中找到解决方案的帮助

In the SelectedProduct property I added在我添加的 SelectedProduct 属性中

public ProductModel SelectedProduct
{
    get { return _selectedProduct; }
    set
    {
        _selectedProduct = value;

        var indexOfType = Types.Select((TypeModel, index) => new { TypeModel, index }).First(x => x.TypeModel.Type == value.Type).index;
        TypeIndex = indexOfType;

        NotifyOfPropertyChange(() => SelectedProduct);          
    }
}

I then bound the SelectedIndex on the combobox to a new property called TypeIndex.然后我将 combobox 上的 SelectedIndex 绑定到一个名为 TypeIndex 的新属性。

Now whenever a datagrid row is selected it will find the datagrids "Type" column value and match to the combobox list of Types and get the index number.现在,无论何时选择数据网格行,它都会找到数据网格的“类型”列值并匹配 combobox 类型列表并获取索引号。 This index value is passed to TypeIndex.这个索引值被传递给 TypeIndex。

暂无
暂无

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

相关问题 如何将DataGridHeader的组合框中的Selected Item的值绑定到一个特定的列而不绑定到wpf中的所有Datagrid列? - How to bind the value of Selected Item in a combobox in DataGridHeader to one specific column not to all Datagrid columns in wpf? 具有Combobox的WPF MVVM Datagrid更新selectedItem Change上的其他列 - WPF MVVM Datagrid having Combobox update other column on selectedItem Change WPF:绑定到DataGrid中的行的SelectedItem - WPF: Bind to SelectedItem of row in DataGrid WPF - MVVM - ComboBox SelectedItem - WPF - MVVM - ComboBox SelectedItem WPF DataGrid组合框selectedItem - WPF DataGrid Combobox selectedItem 如何将带有值转换器的WPF组合框所选项目绑定到标签? 标签和组合框都是datagrid列 - How to bind WPF combobox selected item with a value converter to a label? Both label and combobox are datagrid columns 如何将带有值转换器的WPF组合框所选项目绑定到DataGridTextColumn? DataGridTextColumn和combobox都是datagrid列 - How to bind WPF combobox selected item with a Value Converter to a DataGridTextColumn? Both DataGridTextColumn and combobox are datagrid columns wpf / mvvm-如何制作一个组合框,以设置mvvm中所选项目的特定值 - wpf/mvvm - How to make a ComboBox that sets a specific value from selected item in mvvm 在选定的行上隐藏特定的上下文菜单DataGrid WPF MVMVM - Hide specific context menu in datagrid upon selected row wpf mvvm Wpf:如何从嵌套的 DataGrid 绑定 SelectedItem - Wpf: How to bind SelectedItem from nested DataGrid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM