2016-04-09 42 views
2

Çözüm: Lütfen Ilan cevabına bakın!DataTrigger bir enum başvuruyor DependencyProperty

Şu anda bazı CustomControls üzerinde çalışıyorum ve bunlardan biri. DirectionProperty'a bağlı olarak, linearGradientBrush'ın yönünü DataTrigger ile değiştirmek istiyorum. Çalışmasını gerçekten sağlayamadım ve yardımın için umut ediyorum. Bu DataTrigger benziyor

gerçekten değer veya Yön almanız mümkün değildir. Şimdiden teşekkürler SanHolo

DÜZENLEME: bunu böyle yapmak i bir hata alıyorum:

System.Windows.Data hatası: 4: referansla bağlama için kaynak bulunamıyor 'RelativeSource FindAncestor, AncestorType =' CustomControlLibrary.ColoredProgress ', AncestorLevel =' 1 ''. BindingExpression: Yol = Yön; Dataıtem = null; hedef eleman 'ColoredProgress' (Name = ''); özelliği hedef 'notarget' (tip 'Object')

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

Ben senin kod çalıştırmasına değil
<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> 

cevap

1

kullanın lütfen düzenli tetikleyiciler:

   <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> 

Ben sizin kontrolünüz size bir bağımlılık özelliği olarak bir Yön tanımladığınız beri veri tetik, değer denetlemek için DataContext gitmek anlamak gibi can değeri doğrudan al. Üzerinizde daha fazla veri bağlamını gösteremezsiniz çünkü veri bağlamında size ihtiyacınız olan değeri sağlayabilecek herhangi bir özellikten yoksundur. Bu yüzden bağlama ifadesi hatası alıyorsunuz. Daha fazla açıklamaya ihtiyacınız varsa bildirin.

Saygılarımızla.

+0

Güzel, kendim test ettim ve çalışıyor. Güzel. – Nebelkraehe

+0

@Ruben teşekkür ederim. – Ilan

+0

@Ilan Hızlı cevabınız için teşekkür ederiz! Benim için de çalışıyor. Cevabınız tamamen anlaşılabilir, tekrar teşekkürler! – SanHolo

0

, ama senin sorunun değeri bağlayıcı olduğunu varsayalım DataTrigger, beklediğiniz Enum değerine doğru şekilde ayarlanmamıştır. Bunun çalışması gerekir varsayalım

<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> 

(bağlama yeni Değerini Not), ama kodun geri kalanını kontrol etmedi, işte içinde ilerleme güncellemek için çekinmeyin:

bu deneyin .

+0

Bir hata alıyorum: ColorDirection adı CustomControlLibrary'da yok. Buna nasıl başvururum? 'ColorDirection' iç içe bir enum olduğundan – SanHolo

+0

, siz (sanırım?) Içeren sınıf eleme kullanmanız gerekecektir' Değer = "{x: Statik yerel: ColoredProgress.colorDirection.Decrease}" ' –

+0

Sry işe yaramadı ya. – SanHolo