2

今のところ、私はそのようなものを持っています(互いに独立した値を含むドロップボックスを持つ2つの列):

<xcdg:DataGridControl.Columns>
    <xcdg:Column Title="A"
                 FieldName="A"
                 CellContentTemplate="{StaticResource ADT}"
                                         GroupValueTemplate="{StaticResource ADT}"
                                         Converter="{StaticResource AConverter}"
                 CellEditor="{StaticResource AEditor}"/>
    <xcdg:Column Title="B"
                 FieldName="B"
                 CellContentTemplate="{StaticResource BDT}"
                                         GroupValueTemplate="{StaticResource BDT}"
                                         Converter="{StaticResource BConverter}"
                 CellEditor="{StaticResource BEditor}"/>
</xcdg:DataGridControl.Columns>

また、B 列は、最初の列で選択した値に応じた値を含むドロップボックスにしたいと考えています。

それを達成する方法がわかりません。私は Binding.RelativeSource について読みましたが、私が使用すべきものではないと思います。

ありがとう

4

1 に答える 1

2

それを行うには2つの方法が考えられます.正確なケースを提供しなかったので、簡単なシナリオを提供し、それに基づいて回答を作成します.

DataGrid2 つの編集可能な列 ( AおよびB ) があるとします。編集モードでは、リストからA値を選択できます。combobox次に、Bがフィルター処理されて、値が A値でcombobox始まる項目のみが表示されます。たとえば、A="aa" の場合、B{"aaaa","aabb"}にする必要があります。これを実装するには、最初にアイテムを表すモデルが必要ですDataGrid

 public class GridItem
    {
        public String A { get; set; }
        public String B { get; set; }
    }

コードビハインド/ ViewModelで、これらのプロパティ ( DataGrid、およびcomboboxesItemSource コレクション)を定義します。

private ObservableCollection<GridItem> _gridItemsCollection = new ObservableCollection<GridItem>()
    {
        new GridItem()
        {
            A="aa",
            B="bbbb"
        }
    };
public ObservableCollection<GridItem> GridItemsCollection
    {
        get
        {
            return _gridItemsCollection;
        }

        set
        {
            if (_gridItemsCollection == value)
            {
                return;
            }

            _gridItemsCollection = value;
            OnPropertyChanged();
        }
    }
         //for the first Combobox
private ObservableCollection<String> _aCollection = new ObservableCollection<String>()
    {
        "aa",            
        "bb"           
    };
public ObservableCollection<String> ACollection
    {
        get
        {
            return _aCollection;
        }

        set
        {
            if (_aCollection == value)
            {
                return;
            }

            _aCollection = value;
            OnPropertyChanged();
        }
    }
    //for the second Combobox
private ObservableCollection<String> _bCollection ;
public ObservableCollection<String> BCollection
    {
        get
        {
            return _bCollection;
        }

        set
        {
            if (_bCollection == value)
            {
                return;
            }

            _bCollection = value;
            OnPropertyChanged();
        }
    }

の itemsource が生成される完全なBコレクションを定義しますB combobox

ObservableCollection<String> MainBCollection = new ObservableCollection<String>()
    {
        "aaaa",            
        "aabb",            
        "bbaa",            
        "bbbb"           
    };

最後に、 B の母集団は、このプロパティを使用しcomboboxて選択された項目に基づいて決定されます。A combobox

private String _selectedAItem;
    public String SelectedAItem
    {
        get
        {
            return _selectedAItem;
        }

        set
        {
            if (_selectedAItem == value)
            {
                return;
            }

            _selectedAItem = value;
            OnPropertyChanged();
            var returnedCollection = new ObservableCollection<String>();
            foreach (var val in MainBCollection)
            {
                if (val.StartsWith(_selectedAItem))
                {
                    returnedCollection.Add(value);
                }
            }
            BCollection = new ObservableCollection<string>(returnedCollection);

        }
    }

もちろん、 B Itemsource が更新されるINotifypropertyChangedように、インターフェイスを実装する必要があります。 Combobox

Xamlに関しては、 XceedComboboxのいくつかの制限により、を指定し、およびバインディングItemSourceSelectedItem使用する必要があります。RelativeSourceAncestor

<Grid >           
    <xcdg:DataGridControl ItemsSource="{Binding GridItemsCollection}" AutoCreateColumns="False" SelectionMode="Single" >
        <xcdg:DataGridControl.Columns>
            <xcdg:Column Title="A"
                     FieldName="A"                        
                     >
                <xcdg:Column.CellContentTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </xcdg:Column.CellContentTemplate>
                <xcdg:Column.CellEditor>
                    <xcdg:CellEditor>
                        <xcdg:CellEditor.EditTemplate>
                            <DataTemplate>
                                <ComboBox Name="AComboBox" SelectedItem="{Binding SelectedAItem, RelativeSource={RelativeSource FindAncestor, 
                                                        AncestorType={x:Type Window}}}"   SelectedValue="{xcdg:CellEditorBinding}"
                          ItemsSource="{Binding RelativeSource=
                                                        {RelativeSource FindAncestor, 
                                                        AncestorType={x:Type wpfApplication3:MainWindow}}, 
                                                        Path=ACollection}">

                                </ComboBox>
                            </DataTemplate>
                        </xcdg:CellEditor.EditTemplate>
                    </xcdg:CellEditor>
                </xcdg:Column.CellEditor>
            </xcdg:Column>
            <xcdg:Column Title="B"
                     FieldName="B"
                     >
                <xcdg:Column.CellContentTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </xcdg:Column.CellContentTemplate>
                <xcdg:Column.CellEditor>
                    <xcdg:CellEditor>
                        <xcdg:CellEditor.EditTemplate>
                            <DataTemplate>
                                <ComboBox Name="AComboBox" SelectedValue="{xcdg:CellEditorBinding}"  ItemsSource="{Binding RelativeSource=
                                                        {RelativeSource FindAncestor, 
                                                        AncestorType={x:Type Window}}, 
                                                        Path=BCollection}">


                                </ComboBox>
                            </DataTemplate>
                        </xcdg:CellEditor.EditTemplate>
                    </xcdg:CellEditor>
                </xcdg:Column.CellEditor>
            </xcdg:Column>
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>

</Grid>

そして結果はそのようなものです

最初 2番

それを行うもう 1 つの方法は、a を使用して、 Aが変更されるたびにBコレクションMultivalueConverterを更新することです。 SelectedValue

<xcdg:CellEditor.EditTemplate>
                            <DataTemplate>
                                <ComboBox Name="AComboBox" SelectedValue="{xcdg:CellEditorBinding}">
                                    <ComboBox.ItemsSource>
                                        <MultiBinding Converter="{StaticResource BCollectionConverter}">
                                            <Binding Path="BCollection" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/>
                                            <Binding Path="SelectedValue" ElementName="AComboBox" />
                                        </MultiBinding>                                                                                         
                                    </ComboBox.ItemsSource>
                                </ComboBox>
                            </DataTemplate>
                        </xcdg:CellEditor.EditTemplate>

そして、コンバーターを実装してB Comboboxを更新しItemSource

public class BCollectionConverter:IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == null)
            return null;
        var bCollection = (values[0] as ObservableCollection<String>);
        var aSelectedItem = (values[1] as String);

        if (aSelectedItem == null)
            return null;
        var returnedCollection = new ObservableCollection<String>();
        foreach (var value in bCollection)
        {
            if (value.StartsWith(aSelectedItem))
            {
                returnedCollection.Add(value);
            }
        }
        return returnedCollection;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

私は最後のものを試していませんでした。試してみてください。それが役に立てば幸いです。

于 2015-08-27T21:11:17.107 に答える