2011-01-02 8 views
2

セルの値に基づいてセルの背景色が変更されるSilverlightデータグリッドのトリガを作成するにはどうすればよいですか? 私は以前WPFプロジェクトに取り組んでいましたが、これはxamlのDataTriggersで非常に簡単だったことを思い出しています。しかし、この機能はSilverlightで利用できるようには見えず、どこから始めたらいいか分かりません。SilverlightでWPFのようなデータトリガを作成するには?

ありがとうございます。

答えて

2

まず、Silverlightのトリガーの置き換えはVisualStateManagerです。 VSMはトリガよりもはるかに強力です。なぜなら、状態が変わったときにStoryBoardを実行できるからです。

あなたの状況でアニメーションが必要ない場合は、IValueConverterを使用して解決する方法があります。 DataTemplateで罫線を作成し、背景ブラシを変更するために使用するDataItemのプロパティに背景ブラシをバインドします。

<Border Background={Binding InterestingProperty,Converter={StaticResource BrushConverter}} /> 

アニメーションを必要とするならば、あなたはVisualStateManager上に読みたいとしている:

public class BrushConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     value.ToString() == "Red" ? new SolidColorBrush(Color.Red) : SolidColorBrush(Color.Blue); 
    } 

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

次に、あなたのXAMLは次のようになります。あなたがやるべきことは、依存関係プロパティを持つTemplatedまたはUserControlを作成し、そのプロパティが変更されたときにコントロールがどの状態にあるかを判断し、Visual State Managerを呼び出すことです。構文は

VisualStateManager.GoToVisualState(yourControlInstance,"TheState",boolUseTransitions); 
+0

1でを使用するための例です。あなたは値がnullであることに応える必要があります。また、そのGoToStateはGoToVisualStateではありません。 – AnthonyWJones

0

のようなものですこれが真と偽ブラシ

public class BoolToBrushConverter:DependencyObject,IValueConverter 
    { 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if(value is bool && (bool)value) 
     { 
     return TrueBrush; 
     } 

    return FalseBrush; 
} 

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



public Brush FalseBrush 
{ 
    get { return (Brush)GetValue(FalseBrushProperty); } 
    set { SetValue(FalseBrushProperty, value); } 
} 

// Using a DependencyProperty as the backing store for FalseBrush. This enables animation, styling, binding, etc... 
public static readonly DependencyProperty FalseBrushProperty = 
    DependencyProperty.Register("FalseBrush", typeof(Brush), typeof(BoolToBrushConverter), new PropertyMetadata(null)); 



public Brush TrueBrush 
{ 
    get { return (Brush)GetValue(TrueBrushProperty); } 
    set { SetValue(TrueBrushProperty, value); } 
} 

// Using a DependencyProperty as the backing store for TrueBrush. This enables animation, styling, binding, etc... 

public static readonly DependencyProperty TrueBrushProperty = 
    DependencyProperty.Register("TrueBrush", typeof(Brush), typeof(BoolToBrushConverter), new PropertyMetadata(null));} 

とXAML

十分閉じる
<UserControl.Resources> 
    <converter:BoolToBrushConverter x:Key="enabledToBrushConverter" 
       TrueBrush="White" FalseBrush="Gray" /> 
</UserControl.Resources> 

<TextBlock Foreground="{Binding Element.IsEnabled, 
    Converter={StaticResource enabledToBrushConverter}, ElementName= your_Element}" /> 
関連する問題