将列表传递给多值转换器

时间:2019-05-03 08:05:21

标签: c# wpf mvvm devexpress

我有这样的DataGrid列

<dxg:GridControl DockPanel.Dock="Right" Name="gridControl" ItemsSource="{Binding FilterWiseListOfWorkOrder,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" >

        <dxg:GridColumn Header="Name" >
                            <dxg:GridColumn.DisplayMemberBinding>
                                <MultiBinding Converter="{StaticResource CellBackRoundColorOtTypeConvertor}" >
                                    <Binding Path="RowData.Row" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" />
                                    <Binding Path="Listofcolor" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" />
                                </MultiBinding>
                            </dxg:GridColumn.DisplayMemberBinding>
                        </dxg:GridColumn>
     </dxg:GridControl>

此处<Binding Path="RowData.Row" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" />行数据已传递给转换器

但是<Binding Path="Listofcolor" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" />无法通过颜色列表。这不在Datagrid的itemsource中。 Listofcolor在Vm中作为单独的列表

转换器

public class CellBackRoundColorOtTypeConvertor : MarkupExtension, IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue)
             // Here values[1] == DependencyProperty.UnsetValue is true
            //Some Conversions
        }
    }

VM

 public List<Ots> FilterWiseListOfWorkOrder
            {
                get { return filterWiseListOfWorkOrder; }
                set
                {
                    filterWiseListOfWorkOrder = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("FilterWiseListOfWorkOrder"));
                }
            }


// This will fill in Ctor of Vm 
     public List<string> Listofcolor
            {
                get { return listofcolor; }
                set { listofcolor = value; }
            }

问:如何将这个Listofcolor传递给转换器?

尝试过DataContext.Listofcolor并尝试使用ElementName

评论更新

SS

2 个答案:

答案 0 :(得分:0)

如果您的VM是此用户控件的datacontext,则通过DataContext访问Listofcolor。

 <Binding Path="DataContext.Listofcolor" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" />

答案 1 :(得分:0)

<Binding Path="View.DataContext.Listofcolor"/>  这就是我缺少的link