1

I am looking for a bit of assistance on the following. My view model contains the following:

  • A list of all jobs
  • A list of all grouped jobs (e.g jobs in a particular category)
  • A list of all jobs in the currently selected group.

This is expressed in the view as 3 separate datagrids.

  • One to display all the jobs
  • One to display all grouped jobs
  • One to display all jobs in the currently selected group.

The current bindings I have allow for the list of jobs in the current group to changed based on what group I have selected in the group job view.

What I am looking for assistance on is binding the row background color of the main job list to change based on whether or not the job is in the currently selected job groups list.

So if I change the current group of jobs, all jobs that are in that group will be highlighted. Any assistance would be appreciated.

4

1 回答 1

2

我通过将DataGrid样式与MultiBinding触发器和转换器相结合来解决这个问题。

示例 XAML 代码:

<Window.Resources>
    <this:RowConverter x:Key="RowConverter" />
</Window.Resources>

<Grid>
    <DataGrid Name="dtGroups" HorizontalAlignment="Left" />

    <DataGrid Name="dtJobs" HorizontalAlignment="Right">
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Value="True">
                        <DataTrigger.Binding>
                            <MultiBinding Converter="{StaticResource RowConverter}">
                                <Binding Path="SelectedItem.Id" ElementName="dtGroups" />
                                <Binding Path="GroupId" />
                            </MultiBinding>
                        </DataTrigger.Binding>

                        <Setter Property="Background" Value="Green" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
</Grid>

dtGroupsDataGrid由以下类型的对象填充的:

public class JobGroup
{
    public int Id { get; set; }
    public string Name { get; set; }
}

dtJobsDataGrid由以下类型的对象填充的:

public class Job
{
    public string Name { get; set; }
    public int GroupId { get; set; }
}

接下来,我检查GroupIdin Jobclass 是否与dtGroups. 这发生在转换器中:

class RowConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values.Length != 2 || values[0] == null || values[1] == null) return false;

        if (values[0].ToString() == values[1].ToString()) return true;

        return false;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
于 2013-05-22T20:20:53.587 回答