2017-02-23 20 views
0

神の愛のためにこれを並べ替えることはできません。私はこの答えを見ました:How to Bind data to DataGridComboBoxColumn in DataGrid using MVVMと多くのことが問題になっていますが、私は間違って何をしているのか分かりません。ここに私の考えがあります。私はViewsコレクションにバインドされているDataGridを持っています。各行はクラスViewWrapperのオブジェクトで表されます。ViewWrapperにはというViewWrapperというプロパティがあります。したがって、基本的にビューラッパーは内部に別のビューラッパーを格納できます。ここでViewTemplatesという変数に格納されているのオブジェクトをViewWrapperのリストにフックしたいと思います。これはViewModelで利用可能です。 View Templateプロパティを設定できる可能性のあるすべてのビューラッパーを保持します。今度は、ComboBoxのSelectedItemをDataGrid行のViewWrapper.ViewTemplateにバインドします。私はこのことを機能させることができません。MVVMパターン(Galasoft)を使用したWPFでのComboboxカラムのバインド

は、ここに私の設定です:

XAML:

<UserControl x:Class="GrimshawRibbon.Revit.Views.ViewManager.ViewManagerView" 
      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" 
      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
      xmlns:local="clr-namespace:GrimshawRibbon.Revit.Views.ViewManager" 
      xmlns:ex="clr-namespace:GrimshawRibbon.Revit.Wpf.Extensions" 
      xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
      xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="500"> 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" /> 
       <ResourceDictionary Source="pack://application:,,,/GrimshawRibbon;component/Revit/Wpf/Style/GrimshawTheme.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </UserControl.Resources> 
    <Grid> 
     <ex:DataGridEx x:Name="dgViews" 
           Style="{StaticResource AzureDataGrid}" 
           Margin="10" 
           AutoGenerateColumns="False" 
           ItemsSource="{Binding Views, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
           CanUserAddRows="False" 
           IsReadOnly="False" 
           SelectionMode="Extended" 
           SelectionUnit="FullRow" 
           SelectedItemsList="{Binding SelectedViews, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="CellEditEnding"> 
        <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding CellEditEndingCommand}"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/> 
       <DataGridTextColumn Header="ViewType" Binding="{Binding ViewType}" Width="100" IsReadOnly="True"/> 
       <DataGridTextColumn Header="ViewGroup" Binding="{Binding ViewGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="120" IsReadOnly="False"/> 
       <DataGridTextColumn Header="ViewSubGroup" Binding="{Binding ViewSubGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="120" IsReadOnly="False"/> 
       <DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}" 
             EditingElementStyle="{DynamicResource MetroDataGridCheckBox}" 
             Header="OnSheet" 
             Binding="{Binding OnSheet, Mode=TwoWay}" 
             IsReadOnly="True" 
             Width="80"> 
       </DataGridCheckBoxColumn> 
       <DataGridComboBoxColumn Header="ViewTemplate" Width="150" SelectedItemBinding="{Binding WhatDoISetThisTo?}"> 
        <DataGridComboBoxColumn.ElementStyle> 
         <Style TargetType="ComboBox"> 
          <Setter Property="ItemsSource" Value="{Binding ViewTemplates}"/> 
          <Setter Property="IsReadOnly" Value="True"/> 
         </Style> 
        </DataGridComboBoxColumn.ElementStyle> 
        <DataGridComboBoxColumn.EditingElementStyle> 
         <Style TargetType="ComboBox"> 
          <Setter Property="ItemsSource" Value="{Binding ViewTemplates}"/> 
         </Style> 
        </DataGridComboBoxColumn.EditingElementStyle> 
       </DataGridComboBoxColumn> 
      </DataGrid.Columns> 
     </ex:DataGridEx> 
    </Grid> 
</UserControl> 

のViewModel:

public class ViewManagerViewModel : ViewModelBaseEx 
    { 
     public ViewManagerModel Model; 
     public ObservableCollection<ViewWrapper> Views { get; private set; } 
     public ObservableCollection<ViewWrapper> ViewTemplates { get; private set; } 
     public IList SelectedViews { get; set; } 
     public RelayCommand<DataGridCellEditEndingEventArgs> CellEditEndingCommand { get; set; } 

     public ViewManagerViewModel(Document doc) 
     { 
      Model = new ViewManagerModel(doc); 
      Views = Model.CollectViews(); 
      ViewTemplates = Model.CollectViewTemplates(); 
      CellEditEndingCommand = new RelayCommand<DataGridCellEditEndingEventArgs>(args => OnCellEditEnding(args)); 
     } 

     /// <summary> 
     /// Logic for handling cell editing events. 
     /// </summary> 
     /// <param name="e">DataGrid Row object.</param> 
     private void OnCellEditEnding(DataGridCellEditEndingEventArgs e) 
     { 
      var wrapper = e.Row.Item as ViewWrapper; 
      switch (e.Column.SortMemberPath) 
      { 
       case "Name": 
        Model.ChangeName(wrapper); 
        break; 
       case "ViewGroup": 
        Model.SetParameter(wrapper, "View Group", wrapper.ViewGroup); 
        break; 
       case "ViewSubGroup": 
        Model.SetParameter(wrapper, "View Sub Group", wrapper.ViewSubGroup); 
        break; 
       default: 
        break; 
      } 
     } 

     public override void Apply() 
     { 
      var vw = new List<ViewWrapper>(); 
      foreach (ViewWrapper v in SelectedViews) 
      { 
       vw.Add(v); 
      } 

      Model.Delete(vw); 

      // update collection so that rows get deleted in datagrid 
      foreach (ViewWrapper i in vw) 
      { 
       Views.Remove(i); 
      } 
     } 
    } 

ViewWrapperクラス:

public abstract class ElementWrapper : ObservableObject 
    { 
     public virtual object Self { get; set; } 
     public virtual ElementId ID { get; set; } 
     public virtual string Name { get; set; } 
    } 



    public class ViewWrapper : ElementWrapper 
    { 
     public string ViewType { get; set; } 
     public bool IsSelected { get; set; } 
     public bool OnSheet { get; set; } 
     public string ViewGroup { get; set; } 
     public string ViewSubGroup { get; set; } 
     public ViewWrapper ViewTemplate { get; set; } 

    } 

答えて

1

SelectedItemBindingプロパティがバインドする必要があります現在の行を表すViewWrapperオブジェクトのViewTemplateプロパティに設定します。

ViewTemplatesコレクションはビューモデルで定義されているため、バインドできるようにRelativeSourceバインディングを使用する必要があります。

これを試してみてください:

<DataGridComboBoxColumn Header="ViewTemplate" Width="150" SelectedItemBinding="{Binding ViewTemplate}"> 
    <DataGridComboBoxColumn.ElementStyle> 
     <Style TargetType="ComboBox"> 
      <Setter Property="ItemsSource" Value="{Binding DataContext.ViewTemplates, RelativeSource={RelativeSource AncestorType=DataGrid}}"/> 
      <Setter Property="IsReadOnly" Value="True"/> 
     </Style> 
    </DataGridComboBoxColumn.ElementStyle> 
    <DataGridComboBoxColumn.EditingElementStyle> 
     <Style TargetType="ComboBox"> 
      <Setter Property="ItemsSource" Value="{Binding DataContext.ViewTemplates, RelativeSource={RelativeSource AncestorType=DataGrid}}"/> 
     </Style> 
    </DataGridComboBoxColumn.EditingElementStyle> 
</DataGridComboBoxColumn> 
+0

これは部分的にしかそこに私を取得します。私は今、私のドロップダウンですべての 'ViewTemplates'を見ることができますが、値のどれも選択されていません。いくつかの行項目には既にViewTemplateの値が割り当てられており、その場合はコンボボックスの選択項目がバインドされているときにドロップダウンから適切な値を設定する必要があります。右? – konrad

+0

ViewTemplateプロパティの設定方法によって異なります。ビューモデルのViewTemplatesコレクションに存在するViewWrapperのインスタンスに設定する必要があります。 – mm8

+0

まあ、彼らは同じクラスです。そのラッパークラスに 'Equals'メソッドを上書きした場合はどうなりますか?はい、彼らは両方とも別々にインスタンス化されるので、私は彼らが同じオブジェクトではないと思う方法を見ることができるので、彼らは言うごとに同じインスタンスであることを伝えますか? – konrad

関連する問題