2016-04-09 37 views
2

ソリューション:Ilanの回答を見てください!列挙型を参照するDataTrigger DependencyProperty

私は現在いくつかのCustomControlsで作業しており、これもその1つです。 DirectionPropertyに応じて、私はDataTriggerでlinearGradientBrushの方向を変更したいと思います。私は本当にそれを働かせることができず、あなたの助けを願っています。

DataTriggerが実際に値または方向を取得できないようです。事前のおかげで SanHolo

EDIT:そのようなことをやって、私はエラーを取得:

System.Windows.Dataエラー:4:参照してバインディングのソースを見つけることができません 'RelativeSource FindAncestor、AncestorType =' CustomControlLibrary.ColoredProgress '、AncestorLevel =' 1 ''。 BindingExpression:Path = Direction; DataItem = null;ターゲット要素は 'ColoredProgress'(Name = '');ターゲットプロパティは、私はあなたのコードを実行していないが、私はあなたの問題はあなたの値が結合点であると仮定 'NoTarget'(タイプ 'オブジェクト')

C#

using System.Windows; 
using System.Windows.Controls; 

namespace CustomControlLibrary 
{ 
    public class ColoredProgress : Control 
    { 
     public enum colorDirection { Increase, Decrease } 

     private static DependencyProperty ProgressProperty = 
      DependencyProperty.Register("Progress", typeof(double), typeof(ColoredProgress), new PropertyMetadata(0.00)); 

     private static DependencyProperty DirectionProperty = 
      DependencyProperty.Register("Direction", typeof(colorDirection), typeof(ColoredProgress), new PropertyMetadata(colorDirection.Increase)); 

     public double Progress 
     { 
      get { return (double)GetValue(ProgressProperty); } 
      set { SetValue(ProgressProperty, converter(value)); } 
     } 

     public colorDirection Direction 
     { 
      get { return (colorDirection)GetValue(DirectionProperty); } 
      set { SetValue(DirectionProperty, value); } 
     } 

     public ColoredProgress() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(ColoredProgress), new FrameworkPropertyMetadata(typeof(ColoredProgress))); 
      this.Loaded += ColoredProgress_Loaded; 
     } 

     private void ColoredProgress_Loaded(object sender, RoutedEventArgs e) 
     { 
      double height = (double)GetValue(ColoredProgress.ActualHeightProperty); 
      SetValue(ProgressProperty, height - (height * Progress)); 
     } 

     //takes a double between 0-1 (percent of the ProgressBar) and converts it to the value needed in the design 
     private double converter(double percentage) 
     { 
      double height = (double)GetValue(ColoredProgress.ActualHeightProperty); 
      return height - (height * percentage); 
     } 
    } 
} 

XAML

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:CustomControlLibrary"> 

    <Style TargetType="{x:Type local:ColoredProgress}"> 

     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:ColoredProgress}"> 
        <Border Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          RenderTransformOrigin="0.5, 0.5" 
          DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ColoredProgress}}}"> 

         <Grid x:Name="PART_Bar"> 
          <Grid Background="Transparent" Panel.ZIndex="1"> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="Auto"/> 
           </Grid.RowDefinitions> 
           <Rectangle Fill="{TemplateBinding Background}" Height="{Binding Path=Progress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 
          </Grid> 

          <Grid Panel.ZIndex="0"> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="*" x:Name="increase"/> 
            <RowDefinition Height="0" x:Name="decrease"/> 
           </Grid.RowDefinitions> 
           <Rectangle Grid.Row="0"> 
            <Rectangle.Fill> 
             <LinearGradientBrush StartPoint="0.5,1" EndPoint="0.5,0"> 
              <GradientStop Color="Yellow" Offset="0.0" /> 
              <GradientStop Color="Red" Offset="1.0" /> 
             </LinearGradientBrush> 
            </Rectangle.Fill> 
           </Rectangle> 
           <Rectangle Grid.Row="1"> 
            <Rectangle.Fill> 
             <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> 
              <GradientStop Color="Yellow" Offset="0.0" /> 
              <GradientStop Color="Red" Offset="1.0" /> 
             </LinearGradientBrush> 
            </Rectangle.Fill> 
           </Rectangle> 
          </Grid> 
         </Grid> 
        </Border> 

        <ControlTemplate.Triggers> 
         <DataTrigger Binding="{Binding Path=Direction, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ColoredProgress}}}" Value="colorDirection.Decrease"> 
          <Setter TargetName="increase" Property="Height" Value="0"/> 
          <Setter TargetName="decrease" Property="Height" Value="*"/> 
         </DataTrigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

答えて

1

通常のトリガーを使用してください:私はあなたがすることができます、あなたのコントロールの依存関係プロパティとして方向を定義したので、データトリガは、値を確認するためのDataContextに行く理解したよう

   <ControlTemplate TargetType="{x:Type local:ColoredProgress}"> 
       ... 

       <ControlTemplate.Triggers> 
        <Trigger Property="Direction" Value="Decrease"> 
         <Setter TargetName="increase" Property="Height" Value="0"/> 
         <Setter TargetName="decrease" Property="Height" Value="*"/> 
        </Trigger> 
        <Trigger Property="Direction" Value="Increase"> 
         <Setter TargetName="increase" Property="Height" Value="*"/> 
         <Setter TargetName="decrease" Property="Height" Value="0"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 

を値を直接取得します。必要以上の価値を提供できるデータ・コンテキストがないため、データ・コンテキストを指すことはできません。そのため、バインディング式のエラーが発生します。 詳しい説明が必要な場合はお知らせください。

よろしくお願いいたします。

+0

Niceはそれを自分でテストして動作します。クール。 – Nebelkraehe

+0

@ルーベンありがとうございます。 – Ilan

+0

@Ilanご返信ありがとうございます!私のために働く。あなたの答えは完全に理解できました、もう一度ありがとう! – SanHolo

0

ですDataTriggerが期待するEnum値に正しく設定されていません。私はそれが動作するはずと仮定し

<ControlTemplate.Triggers> 
    <DataTrigger Binding="{Binding Path=Direction, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ColoredProgress}}}" 
       Value="{x:Static local:colorDirection.Decrease}"> 
     <Setter TargetName="increase" Property="Height" Value="0"/> 
     <Setter TargetName="decrease" Property="Height" Value="*"/> 
    </DataTrigger> 
</ControlTemplate.Triggers> 

を(結合新しい注)が、私は、コードの残りの部分をチェックしていなかったので、ここであなたの進捗状況を更新して自由に感じる:

これを試してみてください。

+0

エラーが発生しました。名前colorDirectionがCustomControlLibraryにありません。どのように私はそれを参照するのですか? – SanHolo

+0

'colorDirection'はネストされた列挙型なので、あなたは含まれているクラス修飾子を使う必要があります(私は思う?)' Value = "{x:Static local:ColoredProgress.colorDirection.Decrease}" ' –

+0

Sryはうまくいかなかったどちらか。 – SanHolo