开发工具与关键技术:Microsoft Visual Studio 2017、WPF
这里就举个例子,在按钮的样式模板里面加上一个进度条,然后点击按钮时,ViewModel里面声明的一个int类型的值就开始变化,我就会用下面的方法把这个值传递到这个按钮的样式模板里面的进度条那来做这个进度条的进度值。
首先在工程中创建一个静态类:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
namespace PC.Resources.PublicClass
{
public static class ButtonProgressAssist
{
private const double DefaultMaximum = 100.0;
private const double DefaultMinimum = 0.0;
#region AttachedProperty : MinimumProperty
public static readonly DependencyProperty MinimumProperty
= DependencyProperty.RegisterAttached("Minimum", typeof(double), typeof(ButtonProgressAssist), new FrameworkPropertyMetadata(DefaultMinimum));
public static double GetMinimum(ButtonBase element) => (double)element.GetValue(MinimumProperty);
public static void SetMinimum(ButtonBase element, double value) => element.SetValue(MinimumProperty, value);
#endregion
#region AttachedProperty : MaximumProperty
public static readonly DependencyProperty MaximumProperty
= DependencyProperty.RegisterAttached("Maximum", typeof(double), typeof(ButtonProgressAssist), new FrameworkPropertyMetadata(DefaultMaximum));
public static double GetMaximum(ButtonBase element) => (double)element.GetValue(MaximumProperty);
public static void SetMaximum(ButtonBase element, double value) => element.SetValue(MaximumProperty, value);
#endregion
#region AttachedProperty : ValueProperty
public static readonly DependencyProperty ValueProperty
= DependencyProperty.RegisterAttached("Value", typeof(double), typeof(ButtonProgressAssist), new FrameworkPropertyMetadata(default(double)));
public static double GetValue(ButtonBase element) => (double)element.GetValue(ValueProperty);
public static void SetValue(ButtonBase element, double value) => element.SetValue(ValueProperty, value);
#endregion
#region AttachedProperty : ValueProperty
//public static readonly DependencyProperty TextProperty
// = DependencyProperty.RegisterAttached("Text", typeof(string), typeof(ButtonProgressAssist), new FrameworkPropertyMetadata(default(string)));
//public static string GetText(ButtonBase element) => (string)element.GetValue(TextProperty);
//public static void SetText(ButtonBase element, string value) => element.SetValue(TextProperty, value);
#endregion
#region AttachedProperty : IsIndeterminate
public static readonly DependencyProperty IsIndeterminateProperty
= DependencyProperty.RegisterAttached("IsIndeterminate", typeof(bool), typeof(ButtonProgressAssist), new FrameworkPropertyMetadata(default(bool)));
public static bool GetIsIndeterminate(ButtonBase element) => (bool)element.GetValue(IsIndeterminateProperty);
public static void SetIsIndeterminate(ButtonBase element, bool isIndeterminate) => element.SetValue(IsIndeterminateProperty, isIndeterminate);
#endregion
}
}
然后在xaml中引用那个静态类的命名空间:
xaml 按钮控件代码:
<Button Height="30" Width="60" Panel.ZIndex="{Binding DisplayPercent ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource MaterialDesignRaisedButton}" local:ButtonProgressAssist.Value="{Binding Percent,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" local:ButtonProgressAssist.Opacity="{Binding DownloadVisity,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
按钮样式代码(注意:在你写样式的那个xaml中也要引用什么那个静态类的命名空间):
<Style x:Key="MaterialDesignRaisedButton" TargetType="{x:Type ButtonBase}">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Grid Background="Transparent">
<Grid>
<ProgressBar x:Name="ProgressBar"
Style="{DynamicResource ButProgressBarStyle}"
FontSize="15"
Minimum="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ButtonProgressAssist.Minimum)}"
Maximum="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ButtonProgressAssist.Maximum)}"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ButtonProgressAssist.Value)}"
IsIndeterminate="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ButtonProgressAssist.IsIndeterminate)}"
Height="{TemplateBinding Height}"
Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ButtonBase}}, Path=ActualWidth}"
HorizontalAlignment="Left"
VerticalAlignment="Center" Background="#FF4077EA"/>
</Grid>
<TextBlock x:Name="txt" Text="下载" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="txt"
Duration="0:0:0.5"
Storyboard.TargetProperty="Opacity"
To="0"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" Value="0.23"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这样就能把数据从ViewModel转递到到ResourceDictionary.Style中了。