一、XAML內聯計算
①定義一個類實現IValueConverter接口
②在窗口資源中導入定義的類
<Window.Resources>    <local:ArithmeticConverter x:Key="converter"></local:ArithmeticConverter></Window.Resources>③使用定義的類實現內聯計算,如Storyboard的To屬性的設置:To="{Binding ElementName=window,Path=Width,Converter={StaticResource converter},ConverterParameter=-30}"二、實例代碼演示①ArithmeticConverter.cs實現IValueConverter接口using System;using System.Text.RegularExPRessions;using System.Windows;using System.Windows.Data;namespace Animation{    public class ArithmeticConverter : IValueConverter    {        private const string ArithmeticParseExpression = "([+//-*/]{1,1})//s{0,}(//-?[//d//.]+)";        private Regex arithmeticRegex = new Regex(ArithmeticParseExpression);                       public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)        {            if (value is double && parameter != null)            {                string param = parameter.ToString();                if (param.Length > 0)                {                    Match match = arithmeticRegex.Match(param);                    if (match != null && match.Groups.Count == 3)                    {                        string Operation = match.Groups[1].Value.Trim();                        string numericValue = match.Groups[2].Value;                        double number = 0;                        if (double.TryParse(numericValue, out number)) // this should always succeed or our regex is broken                        {                            double valueAsDouble = (double)value;                            double returnValue = 0;                            switch (operation)                            {                                case "+":                                    returnValue = valueAsDouble + number;                                    break;                                case "-":                                    returnValue = valueAsDouble - number;                                    break;                                case "*":                                    returnValue = valueAsDouble * number;                                    break;                                case "/":                                    returnValue = valueAsDouble / number;                                    break;                            }                            return returnValue;                        }                    }                }            }            return null;        }        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)        {            throw new Exception("The method or operation is not implemented.");        }    }}②內聯計算的使用<Window x:Class="Animation.XamlAnimation"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="XamlAnimation" Height="300" Width="300" Name="window"    xmlns:local="clr-namespace:Animation"    >    <Window.Resources>        <local:ArithmeticConverter x:Key="converter"></local:ArithmeticConverter>    </Window.Resources>    <Button Padding="10" Name="cmdGrow" Height="40" Width="160"          HorizontalAlignment="Center" VerticalAlignment="Center">        <Button.Triggers>            <EventTrigger RoutedEvent="Button.Click">                <EventTrigger.Actions>                    <BeginStoryboard>                        <Storyboard>                            <DoubleAnimation Storyboard.TargetProperty="Width"                To="{Binding ElementName=window,Path=Width,Converter={StaticResource converter},ConverterParameter=-30}"                               Duration="0:0:5"></DoubleAnimation>                            <DoubleAnimation Storyboard.TargetProperty="Height"                To="{Binding ElementName=window,Path=Height,Converter={StaticResource converter},ConverterParameter=-50}"                               Duration="0:0:5"></DoubleAnimation>                        </Storyboard>                    </BeginStoryboard>                </EventTrigger.Actions>            </EventTrigger>        </Button.Triggers>        <Button.Content>            Click and Make Me Grow        </Button.Content>    </Button></Window>
新聞熱點
疑難解答