在不同DataContext中具有两个组合框的MultiBinding

时间:2018-10-24 20:52:53

标签: c# wpf xaml data-binding combobox

我有以下数据网格,其中包含两个组合框,每个组合框都包含在数据模板中。

<DataGrid x:Name="FilterSelection" HorizontalAlignment="Stretch">

    <DataGrid.Resources>
        <DataTemplate x:Key="EntityComboboxTemp">
            <ComboBox x:Name="EntityCombobox"
                ItemsSource="{Binding DataContext.FilterVehicleEntities, RelativeSource={RelativeSource AncestorType=local:ExportView}}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction 
                            Command="{Binding DataContext.SelectedEntityCommand, RelativeSource={RelativeSource AncestorType=local:ExportView}}"
                            CommandParameter="{Binding ElementName=EntityCombobox, Path=SelectedItem}">
                        </i:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>
        </DataTemplate>


        <DataTemplate x:Key="AttributeComboboxTemp">
            <ComboBox x:Name="AttributeCombobox"
                ItemsSource="{Binding DataContext.FilterAttributes, RelativeSource={RelativeSource AncestorType=local:ExportView}}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction 
                            Command="{Binding DataContext.SelectedAttributeCommand, RelativeSource={RelativeSource AncestorType=local:ExportView}}">
                            <i:InvokeCommandAction.CommandParameter>
                                <MultiBinding Converter="{conv:FilterMultipleConverter}">
                                    <Binding ElementName="EntityCombobox" Path="SelectedItem"/>
                                    <Binding ElementName="AttributeCombobox" Path="SelectedItem"/>
                                </MultiBinding>
                            </i:InvokeCommandAction.CommandParameter>
                        </i:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>
        </DataTemplate>
    </DataGrid.Resources>


    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Entity" CellTemplate="{StaticResource EntityComboboxTemp}" />
        <DataGridTemplateColumn Header="Entity Attribute" CellTemplate="{StaticResource AttributeComboboxTemp}"/>
    </DataGrid.Columns>
</DataGrid>

问题在于第二个组合框的多重绑定,即:

Binding ElementName="EntityCombobox" Path="SelectedItem"/>

应将第一个组合框的选定项作为命令参数绑定到第二个组合框。但是我总是会收到EntityCombobox未知的数据绑定错误。我如何为该绑定设置DataContext,这甚至可能吗?

2 个答案:

答案 0 :(得分:1)

我的建议是在您看来不要做太多事情。我建议在DataGrid的ViewModel中执行此逻辑。

首先,我将创建一个视图模型来保存您的过滤器选择。

示例:(请参阅注释,将选择更改的逻辑放在何处)

public class FilterViewModel
{
    private string _vehicleEntity;
    public string VehicleEntity
    {
        get { return _vehicleEntity; }
        set
        {
            _vehicleEntity = value;
            //OnPropertyChanged() if you want
        }
    }

    private string _attribute;
    public string Attribute
    {
        get { return _attribute; }
        set
        {
            _attribute = value;
            //Add logic here to determine what to do with both Attribute and VehicleEntity
            //OnPropertyChanged() if you want
        }
    }
}

然后设置整个View的ViewModel,以容纳FilterModel的集合以及Vehicle选项和Attribute选项的列表。

示例:

public class MainWindowViewModel
{
    public ObservableCollection<FilterViewModel> Rows { get; }
    public ObservableCollection<string> FilterVehicleEntities { get; }
    public ObservableCollection<string> FilterAttributes { get; set; }

    public MainWindowViewModel()
    {
        Rows = new ObservableCollection<FilterViewModel>();

        FilterVehicleEntities = new ObservableCollection<string>()
        {
            "Vehicle 1",
            "Vehicle 2",
            "Vehicle 3",
        };

        FilterAttributes = new ObservableCollection<string>()
        {
            "Attribute 1",
            "Attribute 2",
            "Attribute 3",
        };
    }
}

然后通过直接将选择内容绑定到属性,使您的视图变得不那么复杂。属性更新后,您将立即收到有关选择更改的通知(如FilterViewModel的注释中所标记)。

示例(完整的xaml):

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow"
        Width="600"
        Height="500">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <DataGrid
            AutoGenerateColumns="False"
            ItemsSource="{Binding Rows}">
            <DataGrid.Columns>
                <DataGridComboBoxColumn
                    Header="Entity"
                    SelectedItemBinding="{Binding VehicleEntity, UpdateSourceTrigger=PropertyChanged}">
                    <!-- property changed so we get the change right after we select-->
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style
                            TargetType="ComboBox">
                            <Setter
                                Property="ItemsSource"
                                Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.FilterVehicleEntities}" />
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style
                            TargetType="ComboBox">
                            <Setter
                                Property="ItemsSource"
                                Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.FilterVehicleEntities}" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>

                <DataGridComboBoxColumn
                    Header="Entity Attribute"
                    SelectedItemBinding="{Binding Attribute, UpdateSourceTrigger=PropertyChanged}">
                    <!-- property changed so we get the change right after we select-->
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style
                            TargetType="ComboBox">
                            <Setter
                                Property="ItemsSource"
                                Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.FilterAttributes}" />
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style
                            TargetType="ComboBox">
                            <Setter
                                Property="ItemsSource"
                                Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.FilterAttributes}" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

答案 1 :(得分:0)

ComboBox中的DataTemplate不能引用使用ComboBox绑定在其他地方定义的ElementName,因为它们不在同一个namescope

您应该做的是将SelectedItem的{​​{1}}属性绑定到源属性,然后将另一个EntityCombobox的{​​{1}}属性绑定到同一属性。源属性。为此,定义source属性的类必须实现CommandParameter接口并引发更改通知,并且两个ComboBox也必须共享相同的INotifyPropertyChanged