2016-08-26 27 views

答えて

1

これを行う便利な方法は、GridViewItemの周りにBorderを置き、ValueConverterを使用して現在のアイテムに基づいて背景色を選択することです。

まず、あなたの値変換を定義します。

public class ItemToColorConverter: IValueConverter 
{ 
    //this converts the item from your data source to the color brush 
    //of the background of the row 
    public object Convert(object value, Type targetType, 
     object parameter, string language) 
    { 
     //cast the value parameter to the type of item in your data source 
     var yourValue = (YourType)value; 
     if (yourValue > 10) //some condition you want to use to choose the color 
     { 
      //highlight 
      return new SolidColorBrush(Colors.Green); 
     } 
     else 
     { 
      //leave no background 
      return new SolidColorBrush(Colors.Transparent); 
     } 
    } 

    //you don't have to implement conversion back as this is just one-way binding 
    public object ConvertBack(object value, Type targetType, 
     object parameter, string language) 
    { 
     throw new NotImplementedException(); 
    } 
} 

は今、あなたはApp.xamlで、コンバータのアプリケーションリソースインスタンスを作成する必要があります。

<Application ...> 
    <Application.Resources> 
     <converters:ItemToColorConverter x:Key="ItemToColorConverter" /> 
    </Application.Resources> 
</Application> 

は、今すぐあなたのGridView項目DataTemplateにこのコンバーターを使用します。

<GridView ItemsSource="{Binding YourDataSource"}> 
    <GridView.ItemTemplate> 
     <DataTemplate> 
     <Border Background="{Binding Converter={StaticResource ItemToColorConverter}"> 
      <!-- ... your content --> 
     </Border> 
     </DataTemplate> 
    </GridView.ItemTemplate> 
</GridView> 
+0

私は10以上のマーク(条件)を持っているそれらの行だけ背景色を変更したい..しかしsoln上のすべての行を緑色.. –

+0

返事ありがとうございます。でもまだ私は私のsolnを取得していません。私はasp.netのRowDataBoundのようなものを探しています –

+0

あなたは必要に応じてそれを変更することができます。背景を透明に保ちたい場合は、SolidColorBrushコンストラクタでColors.Transparentを使用してください:-) –

関連する問題