2016-12-09 2 views
0

Есть ли способ изменить свойство Autoreverse доски объявлений. Я хотел бы, чтобы привязать его к свойству элемента управления, но не могу найти способ ...StoryBoard Autoreverse Binding

Моего кода до сих пор:

public class MyProgressBar : ProgressBar 
{ 
    public bool IndeterminateAutoReverse 
    { 
     get { return (bool)GetValue(IndeterminateAutoReverseProperty); } 
     set { SetValue(IndeterminateAutoReverseProperty, value); } 
    } 

    // DependencyProperty as the backing store for IndeterminateAutoReverse 
    public static readonly DependencyProperty IndeterminateAutoReverseProperty = DependencyProperty.Register(
     "IndeterminateAutoReverse", 
     typeof(bool), 
     typeof(MyProgressBar), 
     new PropertyMetadata(true) 
    ); 
} 

И стиль в теме \ Generic.xaml:

<Style TargetType="{x:Type local:MyProgressBar}"> 
     <Setter Property="Foreground" Value="#FF06B025" /> 
     <Setter Property="Background" Value="#FFE6E6E6" /> 
     <Setter Property="BorderBrush" Value="#FFBCBCBC" /> 
     <Setter Property="BorderThickness" Value="1" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:MyProgressBar}"> 
        <Grid x:Name="TemplateRoot"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Indeterminate"> 
            <Storyboard x:Name="TheStoryBoard" RepeatBehavior="Forever" AutoReverse="{Binding IndeterminateAutoReverse, RelativeSource={RelativeSource TemplatedParent}}"> 
             <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" 
                     Storyboard.TargetName="Animation"> 
              <EasingPointKeyFrame KeyTime="0" Value="-0.5,0.5" /> 
              <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5" /> 
              <EasingPointKeyFrame KeyTime="0:0:2" Value="1.5,0.5" /> 
             </PointAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border BorderBrush="{TemplateBinding BorderBrush}" 
           BorderThickness="{TemplateBinding BorderThickness}" 
           Background="{TemplateBinding Background}" /> 
         <Rectangle x:Name="PART_Track" /> 
         <Grid x:Name="PART_Indicator" 
           ClipToBounds="true" 
           HorizontalAlignment="Left"> 
          <Rectangle x:Name="Indicator" 
             Fill="{TemplateBinding Foreground}" /> 
          <Rectangle x:Name="Animation" 
             Fill="{TemplateBinding Foreground}" 
             RenderTransformOrigin="0.5,0.5"> 
           <Rectangle.RenderTransform> 
            <TransformGroup> 
             <ScaleTransform ScaleX="0.25" /> 
            </TransformGroup> 
           </Rectangle.RenderTransform> 
          </Rectangle> 
         </Grid> 
        </Grid> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsIndeterminate" Value="true"> 
          <Setter Property="Visibility" TargetName="Indicator" Value="Collapsed" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

с этим AutoReverse всегда ложно ...

Я также попытался изменить его жгутов ControlTemplate Триггеры так:

<ControlTemplate.Triggers> 
    <Trigger Property="IsIndeterminate" Value="true"> 
     <Setter Property="Visibility" argetName="Indicator" Value="Collapsed" /> 
    </Trigger> 
    <Trigger Property="IndeterminateAutoReverse" Value="true"> 
     <Setter Property="AutoReverse" TargetName="TheStoryBoard" Value="True" /> 
    </Trigger> 
</ControlTemplate.Triggers> 

Но он не компилируется ...

Любая идея?

+0

Пожалуйста, поделитесь ваши ошибки компиляции – robertos

+0

ребенка с именем "TheStoryBoard" не найден в VisualTree. – Sugz

ответ

0

К сожалению, привязка внутри анимации ControlTemplate не поддерживается (источник: https://msdn.microsoft.com/en-us/library/ms742868.aspx#Anchor_9).

Вы можете достичь того, чего хотите, используя два разных состояния. Вот рабочий пример:

[TemplateVisualState(Name = "IndeterminateAutoReverse", GroupName = "CommonStates")] 
public class MyProgressBar : ProgressBar 
{ 
    static MyProgressBar() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(MyProgressBar), new FrameworkPropertyMetadata(typeof(MyProgressBar))); 
    } 

    public MyProgressBar() 
    { 
     var isIndeterminateProperty = DependencyPropertyDescriptor.FromProperty(IsIndeterminateProperty, typeof(MyProgressBar)); 
     isIndeterminateProperty?.AddValueChanged(this, (s,e) => SetIndeterminateState()); 
    } 

    public bool IndeterminateAutoReverse 
    { 
     get { return (bool)GetValue(IndeterminateAutoReverseProperty); } 
     set { SetValue(IndeterminateAutoReverseProperty, value); } 
    } 

    public static readonly DependencyProperty IndeterminateAutoReverseProperty = 
     DependencyProperty.Register(
      "IndeterminateAutoReverse", 
      typeof(bool), 
      typeof(MyProgressBar), 
      new PropertyMetadata(true, (o, e) => (o as MyProgressBar)?.SetIndeterminateState())); 

    private void SetIndeterminateState() 
    { 
     if (!IsIndeterminate) 
     { 
      return; 
     } 

     VisualStateManager.GoToState(
      this, 
      IndeterminateAutoReverse ? "IndeterminateAutoReverse" : "Indeterminate", 
      true); 
    } 
} 

Generic.xaml

<Style TargetType="{x:Type local:MyProgressBar}"> 
    <Setter Property="Foreground" Value="#FF06B025" /> 
    <Setter Property="Background" Value="#FFE6E6E6" /> 
    <Setter Property="BorderBrush" Value="#FFBCBCBC" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:MyProgressBar}"> 
       <Grid x:Name="TemplateRoot"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Indeterminate"> 
           <Storyboard RepeatBehavior="Forever"> 
            <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" 
                    Storyboard.TargetName="Animation"> 
             <EasingPointKeyFrame KeyTime="0" Value="-0.5,0.5" /> 
             <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5" /> 
             <EasingPointKeyFrame KeyTime="0:0:2" Value="1.5,0.5" /> 
            </PointAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="IndeterminateAutoReverse"> 
           <Storyboard AutoReverse="True" RepeatBehavior="Forever"> 
            <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" 
                    Storyboard.TargetName="Animation"> 
             <EasingPointKeyFrame KeyTime="0" Value="-0.5,0.5" /> 
             <EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5" /> 
             <EasingPointKeyFrame KeyTime="0:0:2" Value="1.5,0.5" /> 
            </PointAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          Background="{TemplateBinding Background}" /> 
        <Rectangle x:Name="PART_Track" /> 
        <Grid x:Name="PART_Indicator" 
          ClipToBounds="true" 
          HorizontalAlignment="Left"> 
         <Rectangle x:Name="Indicator" 
            Fill="{TemplateBinding Foreground}" /> 
         <Rectangle x:Name="Animation" 
            Fill="{TemplateBinding Foreground}" 
            RenderTransformOrigin="0.5,0.5"> 
          <Rectangle.RenderTransform> 
           <TransformGroup> 
            <ScaleTransform ScaleX="0.25" /> 
           </TransformGroup> 
          </Rectangle.RenderTransform> 
         </Rectangle> 
        </Grid> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsIndeterminate" Value="true"> 
         <Setter Property="Visibility" TargetName="Indicator" Value="Collapsed" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

MainWindow.xaml

<Grid> 
    <local:MyProgressBar IsIndeterminate="True" IndeterminateAutoReverse="{Binding IsChecked, ElementName=TheCheckBox}"/> 
    <CheckBox x:Name="TheCheckBox" /> 
</Grid> 
+0

В итоге я делаю что-то подобное, но немного более упрощенное. Учитывая две раскадровки, где то же самое, за исключением AutoReverse, я сохранил только один VisualState в ControlTemplate и изменил AutoReverse, обратившись к «VisualStateGroup.CurrentState.StoryBoard» в PropertyChangedCallback свойства IndeterminateAutoReverse. Спасибо, в любом случае :) – Sugz

Смежные вопросы