【问题标题】:A dataGrid column depending on another column依赖于另一列的 dataGrid 列
【发布时间】:2015-10-20 01:41:22
【问题描述】:

我正在开发 WPF - MVVM 应用程序。

我必须在空白处添加产品InvoicedataGrid

每个产品都有一个参考号refsup 和一个description

当我在InvoicedataGrid 中添加一行时,我选择ComboboxColumn 中的参考号,并且描述出现在下一列中。

我该怎么做? 试图保持 MVVM 模式

查看

<DataGrid x:Name="dataGridInvoice"
          Margin="5"
          Grid.Row="1"
          ItemsSource="{Binding Collection}"
          AutoGenerateColumns="False"
          SelectedItem="{Binding Selected, Mode=TwoWay}"
          SelectionMode="Extended"
          SelectionUnit="FullRow"
          AddingNewItem="dataGridInvoice_AddingNewItem">
    <DataGrid.Columns>
        <DataGridTextColumn Header="SuppNb"
                            Binding="{Binding suppInvNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                            Width="*" />
        <DataGridTextColumn Header="Supplier"
                            Binding="{Binding supplier, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                            Width="*" />
        <DataGridComboBoxColumn Header="Ref Supplier"
                                ItemsSource="{Binding Products, Mode=OneWay, Source={StaticResource supplier}}"
                                DisplayMemberPath="refsup"
                                SelectedValueBinding="{Binding refSupp}"
                                SelectedValuePath="refsup"
                                Width="*" />
        <DataGridTextColumn Header="Description"
                            Binding="{Binding description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                            Width="*" />
    </DataGrid.Columns>
</DataGrid>

视图模型

public class InvoiceViewModel : ViewModelBase
{
    public Context ctx = new Context();
    public InvoiceViewModel()
    {
        Get(false);
    }

    private ObservableCollection<Invoice> collection;
    public ObservableCollection<Invoice> Collection
    {
        get
        {
            return collection;
        }
        set
        {
            collection = value;
            OnPropertyChanged("Collection");
        }
    }

    private Invoice _selected;
    public Invoice Selected
    {
        get
        {
            return _selected;
        }
        set
        {
            _selected = value;
            OnPropertyChanged("Selected");
        }
    }

    private void Get(bool loadDataFirst)
    {
        if (loadDataFirst) ctx.Invoices.Load();
        Collection = ctx.Invoices.Local;
    }

    private void Save()
    {
        ctx.SaveChanges();
    }

    private void Delete()
    {
        var id = Selected;
        var invoice = (from i in ctx.Invoices
                    where i.idInvoice == id.idInvoice
                    select i).SingleOrDefault();
        Collection.Remove(invoice);
    }

    private Invoice _currentItem;
    public Invoice CurrentItem
    {
        get
        {
            return _currentItem;
        }
        set
        {
            _currentItem = value;
            OnPropertyChanged("CurrentItem");
        }
    }

    #region "Command"

    private ICommand saveCommand;
    private ICommand removeCommand;

    public ICommand SaveCommand
    {
        get
        {
            return saveCommand ?? (saveCommand = new RelayCommand(p => this.Save(), p => this.CanSave()));
        }
    }

    private bool CanSave()
    {
        return true;
    }

    public ICommand DeleteCommand
    {
        get
        {
            return removeCommand ?? (removeCommand = new RelayCommand(p => this.Delete(), p => this.CanDelete()));
        }
    }

    public bool CanDelete()
    {
        if (Selected != null)
            return true;
        else
            return false;
    }

    #endregion
}

型号

public partial class product
{
    public int idproduct { get; set; }
    public string @ref { get; set; }
    public int supplier { get; set; }
    public string refsup { get; set; }
    public string description { get; set; }
    public int MOQ { get; set; }
    public int unit { get; set; }
    public decimal priceMOQ { get; set; }

    public virtual foodSupplier foodSupplier { get; set; }
    public virtual unit unit1 { get; set; }
}
public partial class Invoice : ViewModelBase
{
    public int idInvoice { get; set; }
    public string suppInvNumber { get; set; }
    public Nullable<int> supplier { get; set; }
    public string refSupp { get; set; }
    public string description { get; set; }    

    public virtual foodSupplier foodSupplier { get; set; }
    public virtual shop shop1 { get; set; }
}

【问题讨论】:

    标签: c# wpf mvvm binding datagrid


    【解决方案1】:

    您可以创建一个DataGridTemplateColumn,它定义了CellTemplateCellEditingTemplate。 CellTemplate 和 CellEditingTemplate 的 DataTemplate 中定义的控件的可见性可以根据选择的值进行切换。

    但是,DataGridTemplateColumnor 任何其他支持的 dataGrid 列都不位于 DataGrid 的可视化树中,因此默认情况下它不继承 DataGrid 的 DataContext。因为它们不在同一个 VisualTree 中,所以任何使用 RelativeSource 获取 DataContext 的尝试都不会奏效,因为 DataGrid 无法遍历到 DataGrid。

    因此,有多种方法可以实现这一目标;其中我将在下面展示一个:

    1. 创建 FrameworkElement 将继承 DataContext,即使它们不在可视化树或逻辑树中。因此,我们可以利用它来使用它。

    2. 创建一个绑定到 ProxyElement 的 ContentControl

    3. 创建一个转换控件可见性的转换器(在 CellTemplate 或 CellEditingTemplate 中定义)。

    4. 创建的代理元素可以绑定到可见性数据库。

      <Grid>
         <Grid.Resources>
           <FrameworkElement x:Key="ProxyElement" DataContext="{Binding}"></FrameworkElement>
         </Grid.Resources>
         <ContentControl Visibility="Collapsed" Content="{StaticResource ProxyElement}"></ContentControl>
         <DataGrid>
             <DataGrid.Columns>
              <DataGridTemplateColumn>
                   <DataGridTemplateColumn.CellTemplate>
                              <DataTemplate>
                                  <TextBox Visibility="{Binding Source={StaticResource ProxyElement}, Path=DataContext.refsub, Converter={StaticResource ConvertorToConvertrefsubToVisibility}}" />
                              </DataTemplate>
                   </DataGridTemplateColumn.CellTemplate>               
               </DataGridTemplateColumn>
              </DataGrid.Columns>
         </DataGrid>
      </Grid>
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      相关资源
      最近更新 更多