2017-05-30 24 views
0

私はタイトルのようなオプションを探しています、私は最初の行が緑で、他は赤であるという例を見てきました、これが結論だった:私は、日時を編集することで、それを変更しようとした予約(ホテルのための)予約予約緑、古い予約赤のデータグリッド

<DataGrid> 
<DataGrid.ItemContainerStyle> 
    <Style TargetType="DataGridRow"> 
     <Setter Property="Background" Value="LightBlue"/> 
     <Style.Triggers> 
      <DataTrigger 
       Binding="{Binding RelativeSource={RelativeSource Mode=PreviousData}}" 
       Value="{x:Null}"> 
       <Setter Property="Background" Value="Green"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.ItemContainerStyle> 

が、私は何の専門家です。私は、誰かが私を助けることができるこの

`<DataGrid.ItemContainerStyle> 
     <Style TargetType="DataGridRow"> 
       <Setter Property="Background" Value="Red" /> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=PreviousData}}" Value="{x:Static sys:DateTime.Now}"> 
          <Setter Property="Background" Value="Green"/> 
         </DataTrigger> 
        </Style.Triggers> 
      </Style> 

`

がありますか? Thxで事前に!

+0

は、おそらく実際のロジックを実行するために、このために、コンバータのいくつかの並べ替えを使用します。データはコンバータ出力でトリガされます。 XAMLですべてのことをやってもうまく終わらない – BradleyDotNET

答えて

1

一つのアプローチは、コンバータを使用することです:

XAMLで
public class StringToColorConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 

     if (value.ToString() == "old") 
     { 
      var color = (Color)ColorConverter.ConvertFromString("Red"); 
      return color; 
     } 
     else if (value.ToString() == "Upcoming") 
     { 
      var color = (Color)ColorConverter.ConvertFromString("Green"); 
      return color; 
     } 


     return (Color)ColorConverter.ConvertFromString("Black"); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

そして:

<Window x:Class="TestWpf.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:TestWpf" 
     xmlns:conv="clr-namespace:TestWpf.StringToColorConverter" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <conv:StringToColorConverter x:Key="colorConverter" /> 
    </Window.Resources> 
    <Grid> 
     <DataGrid> 
      <DataGrid.RowStyle> 
       <Style TargetType="DataGridRow"> 
        <Setter Property="Background"> 
         <Setter.Value> 
          <Binding Path="Reservation" Converter="{StaticResource StringToColorConverter}"/> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </DataGrid.RowStyle> 
     </DataGrid> 
    </Grid> 
</Window> 
0

以下のXAMLは、バインドされているオブジェクトがYourItemSourceであり、プロパティがReservationであることを前提としています。 Reservationプロパティの値がOldの場合、行はRedになります。 Reservationの値が次のの場合は、Greenになります。

<DataGrid ItemsSource="{Binding YourItemsSource}"> 
    <DataGrid.RowStyle> 
     <Style TargetType="DataGridRow"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Reservation}" Value="Old"> 
        <Setter Property="Background" Value="Red"></Setter> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding Reservation}" Value="Upcoming"> 
        <Setter Property="Background" Value="Green"></Setter> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.RowStyle> 
</DataGrid> 

オブジェクトとプロパティの名前と値でインラインになるように名前を調整します。また、値に基づいて、あなたが望む色を示します。