2

XAML を使用して奇数行に別の色を設定しようとしています。

問題のデータグリッドには 3 種類のデータがあり、それぞれ異なる色を付けたいのですが、単に AlternatingRowBackground を変更するだけではうまくいきません。

私は次のようなものを使用する予定です

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
         <Condition Binding="{Binding Type}" Value="0"/>                         
         <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="False"/> 
         <Condition Binding="{Binding IsOddRow, RelativeSource={RelativeSource Self}}" Value="False"/>
    </MultiDataTrigger.Conditions>      
    <Setter Property="Background" Value="#FFDFE6ED"/>                   
</MultiDataTrigger>

のようなプロパティはないようですIsOddRow。代わりにどのプロパティを確認する必要がありますか?

4

3 に答える 3

4

に設定AlternationCountしてから、祖先の添付プロパティDataGridにバインドできます。値が「1」の場合、行番号が奇数です。DataGridRowsItemsControl.AlternationIndex

<DataGrid ItemsSource="{Binding ...}"
          AlternationCount="2">
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Type}" Value="0"/>
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self},
                                                     Path=IsSelected}"
                                   Value="False"/>
                        <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},
                                                     Path=(ItemsControl.AlternationIndex)}"
                                   Value="1"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="#FFDFE6ED"/>
                </MultiDataTrigger>
                <!-- ... -->
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>
    <!-- ... -->
</DataGrid>

添付プロパティにバインドするときは、添付プロパティをかっこで囲む必要があることに注意してください。Path=(ItemsControl.AlternationIndex)動作しますが、動作しPath=ItemsControl.AlternationIndexません。


更新添付された動作を介し
てプロパティを作成することもできます。IsOddRowあなたが購読する行動でLoadingRow。イベント ハンドラで、ロードされた行のインデックスを取得し、それが奇数かどうかを確認します。IsOddRow結果は、バインド可能な という添付プロパティに格納されます。

動作を開始するbehaviors:DataGridBehavior.ObserveOddRow="True"には、DataGrid

<DataGrid ItemsSource="{Binding ...}"
          behaviors:DataGridBehavior.ObserveOddRow="True">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Type}" Value="0"/>
                        <Condition Binding="{Binding Path=IsSelected,
                                                     RelativeSource={RelativeSource Self}}" Value="False"/>
                        <Condition Binding="{Binding Path=(behaviors:DataGridBehavior.IsOddRow),
                                                     RelativeSource={RelativeSource Self}}" Value="False"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="#FFDFE6ED"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

DataGridBehavior

public class DataGridBehavior
{
    #region ObserveOddRow

    public static readonly DependencyProperty ObserveOddRowProperty =
        DependencyProperty.RegisterAttached("ObserveOddRow",
                                            typeof(bool),
                                            typeof(DataGridBehavior),
                                            new UIPropertyMetadata(false, OnObserveOddRowChanged));
    [AttachedPropertyBrowsableForType(typeof(DataGrid))]
    public static bool GetObserveOddRow(DataGrid dataGrid)
    {
        return (bool)dataGrid.GetValue(ObserveOddRowProperty);
    }
    public static void SetObserveOddRow(DataGrid dataGrid, bool value)
    {
        dataGrid.SetValue(ObserveOddRowProperty, value);
    }

    private static void OnObserveOddRowChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        DataGrid dataGrid = target as DataGrid;
        dataGrid.LoadingRow += (object sender, DataGridRowEventArgs e2) =>
        {
            DataGridRow dataGridRow = e2.Row;
            bool isOddRow = dataGridRow.GetIndex() % 2 != 0;
            SetIsOddRow(dataGridRow, isOddRow);
        };
    }

    #endregion // ObserveOddRow

    #region IsOddRow

    public static DependencyProperty IsOddRowProperty =
        DependencyProperty.RegisterAttached("IsOddRow",
                                            typeof(bool),
                                            typeof(DataGridBehavior),
                                            new PropertyMetadata(false));
    [AttachedPropertyBrowsableForType(typeof(DataGridRow))]
    public static bool GetIsOddRow(DataGridRow dataGridCell)
    {
        return (bool)dataGridCell.GetValue(IsOddRowProperty);
    }
    public static void SetIsOddRow(DataGridRow dataGridCell, bool value)
    {
        dataGridCell.SetValue(IsOddRowProperty, value);
    }

    #endregion // IsOddRow
}
于 2012-06-10T12:24:08.393 に答える
2

使用しているグリッド/行のタイプがわからないため、正確なプロパティ名を指定することはできませんが、行のインデックス(行番号)にバインドし、値コンバーター(trueを返す)を使用して、行が奇数または偶数です。

于 2012-06-10T12:18:50.627 に答える
0

ほとんどすべての回答が使用されてAlternationCount="2"いますが、少し制限が多すぎると思います。私の側ではAlternationCount="{ Binding MainData.ProjColl.Count}"、最後まで行に番号を付けるために次のようなものを使用します( 0 から始まることに注意してください!)。

この場合、@Danny Varod で言及されているように、値コンバーターが必要です。

コンバーターを使用して、行の色を交互に変更します (ほぼ質問に答えます)。

public class IsEvenConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        bool res = false;
        int? val = value as int?;
        if (null != val)
            res = (0 == (val % 2));
        return res;
    }     
    ...
}

そして、呼び出し元の XAML

<UserControl ...
<UserControl.Resources>
    ...
    <vm_nmspc:IsEvenConverter x:Key="IsEven"/>
    <Style TargetType="DataGridRow">
        <Setter Property="Width" Value="Auto"/>
        <Setter Property="Background" Value="LightGray"/>

        <!--Converter will be used below-->

        <Style.Triggers>
            ...
            <Setter Property="Background" Value="LightGray"/
            <DataTrigger Binding="{Binding  RelativeSource={RelativeSource Self},
                                            Path=(ItemsControl.AlternationIndex),
                                            Converter={StaticResource ResourceKey=IsEven}}" Value="true">
                <Setter Property="Background" Value="Lavender"/>
            </DataTrigger>
            <Trigger Property="IsMouseOver" Value="True" >
                <Setter Property="Background" Value="LightGreen"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

<Grid>
    <DataGrid ItemsSource="{Binding MainData.ProjColl}" AutoGenerateColumns="False" 
    AlternationCount="{ Binding MainData.ProjColl.Count}" >
    ...
    <DataGridTextColumn Header="Project Name" .... 

    </DataGrid>
</Grid>
</UserControl>

およびいくつかの個別のスクリーンショット

ここに画像の説明を入力 ここに画像の説明を入力

同じコードの別の部分: WPF DataGrid で行番号を表示する簡単な方法

于 2017-02-22T15:29:12.227 に答える