使用带有变量或文本框的C#代码更改故事板的属性 [英] Change Properties of a Storyboard by C# Code with a Variable or a Textbox

查看:104
本文介绍了使用带有变量或文本框的C#代码更改故事板的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。

我是一个绝对的初学者,使用Expression Blend和C#。但我想学习它。我认为对你们大多数人来说我的问题不是真正的问题,但我对每一个答案感到高兴。

Herr是我的问题:

我创造了一个故事板女巫让我们成长长方形。我想用文本块设置高度的端点。

这是XAML代码:

< Window
xmlns =" http://schemas.microsoft。 com / winfx / 2006 / xaml / presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
x:Class =" UltraProgramm.ActualFilling"
x:Name =" Window"
Title =" ActualFilling"
Width =" 400"高度= QUOT; 480"的xmlns:d =" HTTP://schemas.microsoft.com/expression/blend/2008"的xmlns:MC = QUOT; HTTP://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable =" d">

< Window.Resources>
< Storyboard x:Key =" StoryboardFilling">
< DoubleAnimationUsingKeyFrames BeginTime =" 00: 00:00" Storyboard.TargetName = QUOT; rectRed" Storyboard.TargetProperty = QUOT;(FrameworkElement.Height)" RepeatBehavior =" 1x">
< SplineDoubleKeyFrame x:Name =" Spline" KeyTime = QUOT; 00:00:10"值= QUOT; 350" KeySpline =" 0.909,0.044,1,1" />
< / DoubleAnimationUsingKeyFrames>
< / Storyboard>
< /Window.Resources>
< Window.Triggers>
< EventTrigger RoutedEvent =" FrameworkElement.Loaded" />
< /Window.Triggers>

<网格x:名称=" LayoutRoot"高度="450">
< DockPanel Horizo​​ntalAlignment =" Stretch"余量= QUOT; 100,50,100,50" VerticalAlignment = QUOT;拉伸"宽度= QUOT;自动"高度= QUOT;自动"背景= QUOT;#FF000000" LastChildFill =" False">
< Rectangle Fill ="#FFFA0000"行程="#FF000000" X:名称= QUOT; rectRed"高度= QUOT; 10" DockPanel.Dock =" Bottom" />
< / DockPanel>
< Button Horizo​​ntalAlignment =" Left"余量= QUOT; 18,0,0,151" X:名称= QUOT;为btnTest" VerticalAlignment = QUOT;底座"宽度= QUOT; 50"高度= QUOT; 41"含量="Füllen" Click =" btnTest_Click" />
< TextBox Horizo​​ntalAlignment =" Left"余量= QUOT; 18.757,194,0,0" VerticalAlignment = QUOT;陀螺" RenderTransformOrigin = QUOT; 0.472,0.467"文本= QUOT;文本框" TextWrapping = QUOT;包覆与QUOT; d:LayoutOverrides = QUOT;身高" x:Name =" txtLiter" />
< / Grid>
< / Window>

在SplineDoubleKeyFrame中我想用文本框或变量设置值。我有一个名字,但我不能在我的后面代码中调用它。我怎样才能以最简单的方式解决这个问题。

感谢所有需要一些时间的人。

Jörg

Hello.

I'm an absolutly beginner in working with Expression Blend and C#. But I want to learn it. I think for most of you is my problem not a real problem, but I'm happy about every answer.

Herr is my problem:

I've created a storyboard witch let's grow an rectangle. And i want to set the endpoint of the height with an textblock.

Here is the XAML Code:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="UltraProgramm.ActualFilling"
    x:Name="Window"
    Title="ActualFilling"
    Width="400" Height="480" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

    <Window.Resources>
        <Storyboard x:Key="StoryboardFilling">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectRed" Storyboard.TargetProperty="(FrameworkElement.Height)" RepeatBehavior="1x">
                <SplineDoubleKeyFrame x:Name="Spline" KeyTime="00:00:10" Value="350" KeySpline="0.909,0.044,1,1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
    </Window.Triggers>

    <Grid x:Name="LayoutRoot" Height="450">
        <DockPanel HorizontalAlignment="Stretch" Margin="100,50,100,50" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Background="#FF000000" LastChildFill="False">
            <Rectangle Fill="#FFFA0000" Stroke="#FF000000" x:Name="rectRed" Height="10" DockPanel.Dock="Bottom"/>
        </DockPanel>
        <Button HorizontalAlignment="Left" Margin="18,0,0,151" x:Name="btnTest" VerticalAlignment="Bottom" Width="50" Height="41" Content="Füllen" Click="btnTest_Click"/>
        <TextBox HorizontalAlignment="Left" Margin="18.757,194,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.472,0.467" Text="TextBox" TextWrapping="Wrap" d:LayoutOverrides="Height" x:Name="txtLiter"/>
    </Grid>
</Window>

In the SplineDoubleKeyFrame I want to set the Value with an Textbox or a Variable. I've got it a name but i can't call it in my behind-code. How can I solve this problem in the easiest way.

Thanks for all who take some time for me.

Jörg

推荐答案



一种解决方案是使用从String转换为Double的自定义值cnverted将value属性绑定到TextBox的Test属性。我在这里加了一个。在WPF中,您甚至需要处理按钮单击事件,并且只需使用偶数触发器来启动故事板。

这是XAML

< Window
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008 " xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006 " <登记/> X:类= QUOT; WpfApplication65.Window1"
X:名称= QUOT;窗口"
名称= QUOT;窗口1"
宽度=" 640"高度= QUOT; 480"
mc:Ignorable =" d"
xmlns:local =" clr-namespace:WpfApplication65">

Hi,

One solution is to bing the value property to the TextBox's Test property using a custom value cnverted that coverts from String to Double. I've included one here. In WPF you done even need to handle the buttons click event and can just use an even trigger to start the Storyboard.

Here's is the XAML

<Window
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 x:Class="WpfApplication65.Window1"
 x:Name="Window"
 Title="Window1"
 Width="640" Height="480"
 mc:Ignorable="d"
 xmlns:local="clr-namespace:WpfApplication65">

< Window.Resources>
< local:StringToDoubleConverter x:Key =" StringToDoubleConverter" />
< Storyboard x:Key =" StoryboardFilling">
< DoubleAnimationUsingKeyFrames BeginTime =" 00:00:00" ; Storyboard.TargetName = QUOT; rectRed" Storyboard.TargetProperty = QUOT;(FrameworkElement.Height)" RepeatBehavior =" 1x">
< SplineDoubleKeyFrame x:Name =" Spline" KeyTime = QUOT; 00:00:10" Value =" {Binding Path = Text,Converter = {StaticResource StringToDoubleConverter},ElementName = txtLiter,Mode = Default}" KeySpline =" 0.909,0.044,1,1" />
< / DoubleAnimationUsingKeyFrames>
< / Storyboard>
< /Window.Resources>
< Window.Triggers>
< EventTrigger RoutedEvent =" ButtonBase.Click" SourceName =" btnTest">
< BeginStoryboard Storyboard =" {StaticResource StoryboardFilling}" />
< / EventTrigger>
< /Window.Triggers>

   <Window.Resources>
    <local:StringToDoubleConverter x:Key="StringToDoubleConverter"/>
        <Storyboard x:Key="StoryboardFilling">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectRed" Storyboard.TargetProperty="(FrameworkElement.Height)" RepeatBehavior="1x">
                <SplineDoubleKeyFrame x:Name="Spline" KeyTime="00:00:10" Value="{Binding Path=Text, Converter={StaticResource StringToDoubleConverter}, ElementName=txtLiter, Mode=Default}" KeySpline="0.909,0.044,1,1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="btnTest">
         <BeginStoryboard Storyboard="{StaticResource StoryboardFilling}"/>
        </EventTrigger>
    </Window.Triggers>

< Grid x:Name =" LayoutRoot"高度="450">< DockPanel Horizo​​ntalAlignment =" Stretch"余量= QUOT; 100,50,100,50" VerticalAlignment = QUOT;拉伸"宽度= QUOT;自动"高度= QUOT;自动"背景= QUOT;#FF000000" LastChildFill =" False">
< Rectangle Fill ="#FFFA0000"行程="#FF000000" X:名称= QUOT; rectRed"高度= QUOT; 10" DockPanel.Dock =" Bottom" />
< / DockPanel>
< Button Horizo​​ntalAlignment =" Left"余量= QUOT; 18,0,0,151" X:名称= QUOT;为btnTest" VerticalAlignment = QUOT;底座"宽度= QUOT; 50"高度= QUOT; 41" Content ="Füllen" />< TextBox Horizo​​ntalAlignment =" Left"余量= QUOT; 18.757,194,0,0" VerticalAlignment = QUOT;陀螺" RenderTransformOrigin = QUOT; 0.472,0.467"文本= QUOT;文本框" TextWrapping = QUOT;包覆与QUOT; d:LayoutOverrides = QUOT;身高" x:Name =" txtLiter" />
< / Grid>
< / Window>

这里的代码是

    <Grid x:Name="LayoutRoot" Height="450">
        <DockPanel HorizontalAlignment="Stretch" Margin="100,50,100,50" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Background="#FF000000" LastChildFill="False">
            <Rectangle Fill="#FFFA0000" Stroke="#FF000000" x:Name="rectRed" Height="10" DockPanel.Dock="Bottom"/>
        </DockPanel>
        <Button HorizontalAlignment="Left" Margin="18,0,0,151" x:Name="btnTest" VerticalAlignment="Bottom" Width="50" Height="41" Content="Füllen"/>
        <TextBox HorizontalAlignment="Left" Margin="18.757,194,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.472,0.467" Text="TextBox" TextWrapping="Wrap" d:LayoutOverrides="Height" x:Name="txtLiter"/>
    </Grid>
</Window>

and here is the code behind

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication65
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		public Window1()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
		}
	}
	
	public sealed class StringToDoubleConverter : IValueConverter
	{
		// Methods
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			string valueAsString = value as String;
			double doubleValue = 0.0;
			if(!String.IsNullOrEmpty(valueAsString))
			{
				try
				{
					doubleValue = Double.Parse(valueAsString, System.Globalization.CultureInfo.CurrentCulture);
				}
				catch(FormatException)
				{
				}
			}
			return doubleValue;
		}
	
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return ((double)value).ToString();
		}
	}
}



谢谢!


Thanks!


这篇关于使用带有变量或文本框的C#代码更改故事板的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆