2

现在,我有类似的东西(2 列的 Dropbox 包含彼此独立的值):

<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

我可以想到两种方法来做到这一点,由于您没有提供确切的案例,我将提供一个简单的场景并以此为基础构建我的答案,

假设您有DataGrid两个可编辑列(AB),在编辑模式下,您可以从列表中选择Acombobox,然后B将被过滤以仅显示其值以 Acombobox开头的项目例如,如果A="aa"B应该是{"aaaa","aabb"},要实现你首先需要一个代表项目的模型DataGrid

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

在您的代码隐藏/ ViewModel中定义这些属性(DataGridcomboboxesItemSource 集合):

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();
        }
    }

定义一个完整的B集合,您B combobox的 itemsource 将从该集合中填充

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);

        }
    }

您当然需要实现INotifypropertyChanged接口,以便更新B Combobox Itemsource,

现在关于Xaml,由于Xceed中的一些限制,您需要指定Combobox'sItemSourceSelectedItem使用RelativeSourceandAncestor绑定,

<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>

结果是这样的

第一的 第二

另一种方法是使用 a并在每次更改AMultivalueConverter时更新B集合,如下所示: 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 ComboboxItemSource

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 回答