【问题标题】:how to bind to property without destroying the control template Features?如何在不破坏控件模板功能的情况下绑定到属性?
【发布时间】:2014-11-22 11:37:32
【问题描述】:

我制作了一个控件模板,它是按钮的目标类型。 它的两个事件触发器属于 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
}

}

【问题讨论】:

  • 能否提供您的模板代码?
  • 我写了一个转换器和一个控制模板,发现不透明度更新正确。请将JoinButtonStyleEnableConverter的代码贴出来;
  • 将我的代码添加到主要问题
  • 你绑定的时候有没有在Convert方法中设置断点来查看它是否被调用以及使用什么参数?
  • 是的,我用断点检查过,结果很好。根据枚举返回真或假

标签: c# wpf controltemplate


【解决方案1】:

而不是像您当前那样为启用/禁用状态设置两个不同的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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多