对您的 XAML 进行少量编辑似乎可以解决问题。您需要按特定源名称过滤 Button.Click 事件,如下所示。此外,从 Slider.Value 中删除 Binding 似乎有帮助。
我不太确定如何最好地维护绑定并仍然能够使其正常工作。
<Slider Name="MySlider"
Maximum="5"
Minimum="-5"/>
<Button Name="myButton" Content="Reset">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click" SourceName="myButton">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0"
Storyboard.TargetName="MySlider"
Storyboard.TargetProperty="Value"
To="0" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
如果目标是将 Slider.Value 数据绑定到某物,例如...
<Slider Value="{Binding FloatProperty}"/>
...然后我怀疑(尽管我不是 100% 确定)您尝试做的事情可能是不可能的。
首先,让我解释一下为什么您会看到滑块被“冻结”。这不是一个错误 - 我只需要仔细阅读文档以了解发生了什么。
当 FillBehavior 设置为 Stop 时,当 TimeLine 完成时,Animation 将恢复该值(恢复到 Animation 开始之前的值)。
另一方面,当 FillBehavior 为 HoldEnd(这是您在未明确指定 FillBehavior 时获得的默认行为)时,DoubleAnimation 将继续保持结束值并防止来自其他来源(如滑块手动移动)。因此,每次移动滑块时,Animation 都会将其重置为 0,因为 HoldEnd。这就是为什么您会看到滑块在 0 时表现得好像“冻结”了一样。
我调查了在 Button.IsPressed = True 上使用 DataTrigger 作为替代方案。这通常在 Style 或 ControlTemplate 中完成 - 它看起来像这样:
<Slider Maximum="5"
Minimum="-5"
Value="{Binding FloatProperty}">
<Slider.Template>
<ControlTemplate TargetType="Slider">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Slider x:Name="Slider"
Grid.Row="0"
Maximum="{TemplateBinding Maximum}"
Minimum="{TemplateBinding Minimum}"
Value="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Value,
Mode=TwoWay}" />
<Button x:Name="ResetButton"
Grid.Row="1"
Content="Reset" />
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=ResetButton, Path=IsPressed}" Value="True">
<Setter TargetName="Slider" Property="Value" Value="0" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Slider.Template>
</Slider>
不幸的是,Dependency Property Value Precedence rules 认为 TemplatedParent 模板属性的优先级高于触发器和样式设置器。因此,单击“重置”按钮会暂时将滑块值更改为 0,但会立即重置为相应绑定支持的原始值。
在用尽了 Animation 和 DataTriggers 之后,我得出结论认为这可能是不可能的。也就是说,鉴于 WPF 中存在的大量功能,我很可能是错的......
编辑:2015 年 12 月 5 日
我想我有一个解决方案可以满足您的关键要求 - 即允许设计师修改 UI,包括“重置”滑块值的含义。也就是说,此解决方案需要代码隐藏 - 但这样的代码隐藏不会阻止 XAML 充分表达您的意图。
这个想法是写一个行为,并使用该行为将按钮与滑块相关联,并指示此按钮应触发“重置”操作。
行为的基本轮廓如下所示:
/// <remarks>
/// Requires System.Windows.Interactivity.dll to be
/// added to the References section of the Project
/// </remarks>
public class SliderResetBehavior: Behavior<Button>
{
protected override void OnAttached()
{
// AssociatedObject refers to a Button instance
// to which this Behavior is attached
AssociatedObject.Click += OnClick;
}
/// <summary>
/// Upon receiving a Click event, reset the Slider
/// </summary>
private void OnClick(object sender, RoutedEventArgs e)
{
if(Slider != null)
{
Slider.Value = ResetValue;
}
}
#region Dependency Property - ResetValue
/// <summary>
/// Stores a double that will act as the definition of 'reset'
/// value for the Slider associated with this Behavior.
/// </summary>
public double ResetValue
{
get { return (double)GetValue(ResetValueProperty); }
set { SetValue(ResetValueProperty, value); }
}
// Using a DependencyProperty as the backing store for ResetValue.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty ResetValueProperty =
DependencyProperty.Register(
"ResetValue",
typeof(double),
typeof(SliderResetBehavior),
new PropertyMetadata(0.0));
#endregion
/// <summary>
/// Maintains a System.Windows.Controls.Slider object upon
/// which this Behavior will act upon.
/// </summary>
#region Dependency Property - Slider
public Slider Slider
{
get { return (Slider)GetValue(SliderProperty); }
set { SetValue(SliderProperty, value); }
}
// Using a DependencyProperty as the backing store for Slider.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty SliderProperty =
DependencyProperty.Register(
"Slider",
typeof(Slider),
typeof(SliderResetBehavior),
new PropertyMetadata(default(Slider)));
#endregion
}
XAML 现在可以非常简单,如下所示:
<StackPanel>
<Slider x:Name="Slider" Minimum="-5" Maximum="5" Value="{Binding SliderValue}"/>
<!--
Requires xmlns entries like this:
xmlns:local="clr-namespace:<Namespace containing SliderResetBehavior>"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
-->
<Button x:Name="button" Content="Reset">
<i:Interaction.Behaviors>
<local:SliderResetBehavior ResetValue="0" Slider="{Binding ElementName=Slider}"/>
</i:Interaction.Behaviors>
</Button>
</StackPanel>
上面显示的此 XAML 保留了设计人员仅使用松散 XAML 完全可自定义的关键属性,并且还允许 Slider.Value 的数据绑定。