2016-05-13 19 views
0

私は刻まれた値がOperativeCountです。この数字が10より大きい場合、DataGridColumnの色を変更したいと思います。 Value=">10"今のところ明らかにDataTrigger数値より大きい場合

<DataGrid.Resources> 
    <Style x:Key="DGCellStyle" TargetType="DataGridCell"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding OperativeCount}" Value=">10"> 
       <Setter Property="FontWeight" Value="Bold"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.Resources> 

が動作していないが、基本的にそれは私がやりたいものです。

+0

あなたはその答えの1つを正しいものとしてチェックしてください。あるいは、何が間違っているのか、それとも欠けているのかを教えてください。これは、私たちにとっても、この質問を見る他のSOユーザにとっても、有益なフィードバックです。 –

答えて

1

そのためには、値コンバータが必要です。

using System; 
using System.Globalization; 
using System.Windows.Data; 
using System.Windows.Markup; 

namespace EdPlunkett 
{ 
    public class GreaterThan : MarkupExtension, IValueConverter 
    { 
     // The only public constructor is one that requires a double argument. 
     // Because of that, the XAML editor will put a blue squiggly on it if 
     // the argument is missing in the XAML. 
     public GreaterThan(double opnd) 
     { 
      Operand = opnd; 
     } 

     /// <summary> 
     /// Converter returns true if value is greater than this. 
     /// 
     /// Don't let this be public, because it's required to be initialized 
     /// via the constructor. 
     /// </summary> 
     protected double Operand { get; set; } 

     // When the XAML is parsed, each markup extension is instantiated 
     // and the parser asks it to provide its value. Here, the value is 
     // us. 
     public override object ProvideValue(IServiceProvider serviceProvider) 
     { 
      return this; 
     } 

     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return System.Convert.ToDouble(value) > Operand; 
     } 

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

そして、ここでは、あなたがそれを使用する方法は次のとおりです:ここでは、あなたが私だったらということ書くかもしれない方法です

<Window 
    .... 
    xmlns:edp="clr-namespace:EdPlunkett" 
    .... 
    > 

    <DataGrid.Resources> 
     <Style x:Key="DGCellStyle" TargetType="DataGridCell"> 
      <Style.Triggers> 
       <DataTrigger 
        Binding="{Binding OperativeCount, Converter={edp:GreaterThan 10}}" 
        Value="True"> 
        <Setter Property="FontWeight" Value="Bold" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.Resources> 

値コンバータは、非常に多くの場合、リソースとしてインスタンス化されており、それらのパラメータは通常に渡されますConverterParameterプロパティを使用しますが、MarkupExtensionにすると、XAMLパーサーは引数の型を強制するだけでなく、引数が必要です。これにより、コンバータの邪魔は非常に簡単になります。さらにIntellisenseを入手してください。

0

このコンポーネントを再利用する必要がないか、または「一般的」にする必要がない場合は、よりシンプルで最も重視されるソリューションは次のとおりです。

このコードのコンバーターを作成:アプリケーションの存続期間中OperativeCountプロパティの変更は、それが変更に通知を上げる必要がある場合、

<DataGrid.Resources> 

    <local:CountToFontWeightConverter x:Key="CountToFontWeightConverter"/> 

    <Style TargetType="{x:Type DataGridCell}" x:Key="DGCellStyle"> 
    <Setter Property="FontWeight" 
      Value="{Binding OperativeCount, 
       Converter={StaticResource CountToFontWeightConverter}}"/> 
    </Style> 

</DataGrid.Resources> 

明らかに:

using System; 
using System.Globalization; 
using System.Windows; 
using System.Windows.Data; 

namespace WpfApplication1 
{ 
    public class CountToFontWeightConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (value == null) 
       return DependencyProperty.UnsetValue; 

      var count = (int)value; 

      if (count > 10) 
       return FontWeights.Bold; 
      else 
       return FontWeights.Normal; 
     } 

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

そして、このようにそれを使用しますINotifyPropertyChanged実装を介して、またはReactiveライブラリを介して、

コンバータの内部でハードコーディングするのではなく、コンバータのパラメータとして制限をに渡すことで、この解決策を少し一般化することができます。これにより、制限が異なる複数の場所で使用できるようになります。

2

Blend SDK for WPFを使用すると、コードがなくても非常に迅速に処理できます。 DataTrigger (Blend SDK for WPF)をチェックしてください。動作としてChangePropertyActionを使用します。

<ei:DataTrigger Binding="{Binding OperativeCount}" Comparison="GreaterThan" Value="10"> 
    <ei:ChangePropertyAction PropertyName="FontWeight" > 
    <ei:ChangePropertyAction.Value> 
     <FontWeight>Bold</FontWeight> 
    </ei:ChangePropertyAction.Value> 
    </ei:ChangePropertyAction> 
</ei:DataTrigger> 

多くのことを気にしないでください。ブレンドはそれを気にしないでください。

+0

ソフトウェアを見つけるのは非常に難しかったので、ここで最も関連性の高いリンクは次のとおりです。 https://www.microsoft.com/en-us/download/details.aspx?id=5915 および https://www.microsoft.com/en-us/download/details.aspx?id=10801 。最初のURLにリンクされている「概要」からいくつかの新しい情報(不明な点があります)を見つけてください。 –

+0

Expression Studioの古いバージョンを指しています。 Expression Studioは廃止され、Expression BlendはVisual Studioに含まれています。ちょうどVisual Studioのコミュニティ版を入手してください。 –

関連する問題