将列表传递给多值转换器
Passing a List To multi value Converter
我有这样的 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
的项目源中。 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
评论更新
如果您的 VM 是此用户控件的数据上下文,则通过 DataContext 访问 Listofcolor。
<Binding Path="DataContext.Listofcolor" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" />
<Binding Path="View.DataContext.Listofcolor"/>
这就是我缺少的 link
我有这样的 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
的项目源中。 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
评论更新
如果您的 VM 是此用户控件的数据上下文,则通过 DataContext 访问 Listofcolor。
<Binding Path="DataContext.Listofcolor" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" />
<Binding Path="View.DataContext.Listofcolor"/>
这就是我缺少的 link