2014-11-22 19 views
0

我做了一個控制模板,它是目標類型,如果按鈕。 拖動它的事件觸發器屬於IsEnable和IsnotEnable屬性。當控制模板啓用時,我將不透明度設置爲100%,當不透明度降低到40%時。如何在不破壞控件模板的情況下綁定屬性?

在我的GUI窗口

我這樣定義一個新的按鈕:

<Button x:Name="JoinB" 
     IsEnabled="{Binding Path=GroupStatus,Converter={StaticResource EnableConverter}}" 
     Template="{DynamicResource JoinButtonStyle}" /> 

EnableConverter是一個簡單的轉換器,返回true或false。 轉換器正在工作。我的按鈕沒有啓用,但不透明度不會改變。 如果我定義我的按鈕,像這樣(沒有轉換器):

<Button x:Name="JoinB" IsEnabled="false" 
     Template="{DynamicResource JoinButtonStyle}" /> 

透明度確實發生了改變。

你知道我在做什麼錯嗎?

JoinButtonStyle代碼:

<ControlTemplate x:Key="JoinButtonStyle" TargetType="{x:Type Button}"> 
    <ControlTemplate.Resources> 
     <Storyboard x:Key="OnMouseEnter1"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.BlurRadius)" Storyboard.TargetName="rectangle"> 
       <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="8"/> 
      </DoubleAnimationUsingKeyFrames> 
      <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.Color)" Storyboard.TargetName="rectangle"> 
       <EasingColorKeyFrame KeyTime="0:0:0.2" Value="#FF00BC02"/> 
      </ColorAnimationUsingKeyFrames> 
     </Storyboard> 
     <Storyboard x:Key="OnMouseLeave1"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.BlurRadius)" Storyboard.TargetName="rectangle"> 
       <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="2"/> 
      </DoubleAnimationUsingKeyFrames> 
     </Storyboard> 
     <Storyboard x:Key="OnPreviewMouseLeftButtonDown1"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="rectangle"> 
       <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0.7"/> 
      </DoubleAnimationUsingKeyFrames> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="label"> 
       <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0.7"/> 
      </DoubleAnimationUsingKeyFrames> 
     </Storyboard> 
     <Storyboard x:Key="OnPreviewMouseLeftButtonUp1"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="rectangle"> 
       <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
      </DoubleAnimationUsingKeyFrames> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="label"> 
       <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
      </DoubleAnimationUsingKeyFrames> 
     </Storyboard> 
     <Storyboard x:Key="onNotEnabled"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid"> 
       <EasingDoubleKeyFrame KeyTime="0" Value="0.4"/> 
      </DoubleAnimationUsingKeyFrames> 
     </Storyboard> 
     <Storyboard x:Key="onEnabled"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid"> 
       <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
      </DoubleAnimationUsingKeyFrames> 
     </Storyboard> 
    </ControlTemplate.Resources> 
    <Grid x:Name="grid" VerticalAlignment="Center" HorizontalAlignment="Center" Background="#00000000"> 
     <Rectangle x:Name="rectangle" HorizontalAlignment="center" VerticalAlignment="center" Height="30" Width="90" RadiusX="15" RadiusY="15" StrokeThickness="1" Stroke="#FF58A6FD"> 
      <Rectangle.Effect> 
       <DropShadowEffect BlurRadius="2" ShadowDepth="0" Color="#FF58A6FD"/> 
      </Rectangle.Effect> 
     </Rectangle> 
     <Label x:Name="label" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF58A6FD" Content="Join"/> 
    </Grid> 
    <ControlTemplate.Triggers> 
     <EventTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonUp"> 
      <BeginStoryboard x:Name="OnPreviewMouseLeftButtonUp1_BeginStoryboard" Storyboard="{StaticResource OnPreviewMouseLeftButtonUp1}"/> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonDown"> 
      <BeginStoryboard x:Name="OnPreviewMouseLeftButtonDown1_BeginStoryboard" Storyboard="{StaticResource OnPreviewMouseLeftButtonDown1}"/> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="Mouse.MouseLeave"> 
      <BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="Mouse.MouseEnter"> 
      <BeginStoryboard x:Name="OnMouseEnter1_BeginStoryboard" Storyboard="{StaticResource OnMouseEnter1}"/> 
     </EventTrigger> 
     <Trigger Property="IsEnabled" Value="False"> 
      <Trigger.EnterActions> 
       <BeginStoryboard x:Name="OnMouseEnter1_BeginStoryboard1" Storyboard="{StaticResource onNotEnabled}"/> 
      </Trigger.EnterActions> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="True"> 
      <Trigger.EnterActions> 
       <BeginStoryboard x:Name="onEnabled_BeginStoryboard" Storyboard="{StaticResource onEnabled}"/> 
      </Trigger.EnterActions> 
     </Trigger> 
    </ControlTemplate.Triggers> 

</ControlTemplate> 

轉換器代碼:

public class EnableToGroupStatusConverter:IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if ((ClientManager.DateGroupInfo.GroupStatusType)value == ClientManager.DateGroupInfo.GroupStatusType.CLOSED) 
     { 
      return false; 
     } 
     else 
     { 
      return true; 
     } 

    } 

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

    #endregion 
} 

}

+0

你能提供你的模板代碼? – Grx70 2014-11-22 12:01:12

+0

我寫了一個轉換器和一個控件模板,並發現不透明度已正確更新。請發佈'JoinButtonStyle'的代碼以及'EnableConverter'; – kennyzx 2014-11-22 12:07:29

+0

將我的代碼添加到主要問題 – 2014-11-22 13:07:42

回答

0

不是有兩個不同Trigger的啓用/禁用狀態,你現在做的

<Trigger Property="IsEnabled" Value="False"> 
    <Trigger.EnterActions> 
     <BeginStoryboard Storyboard="{StaticResource onNotEnabled}"/> 
    </Trigger.EnterActions> 
</Trigger> 
<Trigger Property="IsEnabled" Value="True"> 
    <Trigger.EnterActions> 
     <BeginStoryboard Storyboard="{StaticResource onEnabled}"/> 
    </Trigger.EnterActions> 
</Trigger> 

將它們組合成一個TriggerEnterActions/ExitActions

<Trigger Property="IsEnabled" Value="False"> 
    <Trigger.EnterActions> 
     <BeginStoryboard Storyboard="{StaticResource onNotEnabled}"/> 
    </Trigger.EnterActions> 
    <Trigger.ExitActions> 
     <BeginStoryboard Storyboard="{StaticResource onEnabled}"/> 
    </Trigger.ExitActions> 
</Trigger> 
+0

返回true或false謝謝!它的工作現在。有什麼不同? – 2014-11-22 23:28:07

相關問題