WPF - 它得是更容易比我做它 [英] WPF -- it's gotta be easier than I'm making it

查看:234
本文介绍了WPF - 它得是更容易比我做它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的时候darndest弄清这一点:说我有两个按钮和三个的TextBlocks。我想任何一个按钮来触发所有的TextBlocks一个简单的故事板。目前,我试图定义一个包含故事板,然后触发来自任何按钮点击一个通用的文本块风格。这是我来最接近的,但在启动应用程序崩溃......我是什么做的没有错位置:

 < Window.Resources><风格的TargetType =TextBlock的>
< setter属性=前景VALUE =蓝/>
< Style.Resources>
<情节提要X:键=TextBlockOpacityStoryboard.TargetProperty =透明度>
&所述; DoubleAnimation是来自=0要=1/>
< /故事板>
< /Style.Resources>
< /样式和GT;

 < Window.Triggers>
<的EventTrigger RoutedEvent =ButtonBase.ClickSOURCENAME =按钮>
< BeginStoryboard故事板={StaticResource的TextBlockOpacity}/>
< /&的EventTrigger GT;
< /Window.Triggers>
<电网X:NAME =LayoutRoot>
<按钮X:NAME =按钮的Horizo​​ntalAlignment =左保证金=51,54,0,0VerticalAlignment =评出的WIDTH =96HEIGHT =45CONTENT =按钮/>< TextBlock的X:名称=textBlock1保证金=228,54,172,0VerticalAlignment =评出的高度=45字号=26.667文本=TextBlock的TextWrapping =自动换行/>
< TextBlock的X:名称=textBlock2保证金=228,103,172,0VerticalAlignment =评出的高度=45字号=26.667文本=你好TextWrapping =自动换行/>
< /网格和GT;


解决方案

根据kek444的XAML的唯一的解决办法,我present不依赖于按钮的DataContext的,可以有多个触发器略有改进版

 <窗​​口
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    X:类=WpfApplication1.MainWindow
    X:NAME =窗口
    标题=主窗口
    WIDTH =640HEIGHT =480>
    < Window.Resources>
        <的UIElement X:键=OpacityCounter透明度=0/>
        <风格的TargetType =TextBlock的>
    < setter属性=透明度VALUE ={绑定源= {StaticResource的OpacityCounter},路径=透明度}/>
    < /样式和GT;
    <情节提要X:键=OnClick1>
    < D​​oubleAnimationUsingKeyFrames的BeginTime =00:00:00Storyboard.Target ={StaticResource的OpacityCounter}Storyboard.TargetProperty =(UIElement.Opacity)>
    &所述; SplineDoubleKeyFrame KeyTime =00:00:00值=0/>
    &所述; SplineDoubleKeyFrame KeyTime =00:00:00.5000000值=1/>
    < / DoubleAnimationUsingKeyFrames>
    < /故事板>
    < /Window.Resources>
    < Window.Triggers>
    <的EventTrigger RoutedEvent =ButtonBase.ClickSOURCENAME =Button1的>
    < BeginStoryboard故事板={StaticResource的OnClick1}/>
    < /&的EventTrigger GT;
    <的EventTrigger RoutedEvent =ButtonBase.ClickSOURCENAME =按钮2>
    < BeginStoryboard故事板={StaticResource的OnClick1}/>
    < /&的EventTrigger GT;
    < /Window.Triggers>    <电网X:NAME =LayoutRoot>
    <&StackPanel的GT;
    < StackPanel的方向=横向>
    <按钮X:NAME =Button1的WIDTH =131HEIGHT =37CONTENT =按钮1保证金=0,0,0,22/>
    <按钮X:NAME =按钮2WIDTH =131HEIGHT =37CONTENT =按钮2保证金=0,0,0,22/>
    < / StackPanel的>
    < TextBlock的X:名称=文本块HEIGHT =27文本=1的TextBlockTextWrapping =自动换行/>
    < TextBlock的X:名称=textBlock1HEIGHT =27文本=2的TextBlockTextWrapping =自动换行/>
    < TextBlock的X:名称=textBlock2HEIGHT =27文本=TextBlock的3TextWrapping =自动换行/>
    < TextBlock的X:名称=textBlock3HEIGHT =27文本=TextBlock的4TextWrapping =自动换行/>
    < / StackPanel的>
    < /网格和GT;
< /窗GT;

要使用一个ListBox作为触发机制(前提是你有一个名为ListBox1的在某处列表框,添加以下Window.Triggers:

 <的EventTrigger RoutedEvent =Selector.SelectionChangedSOURCENAME =ListBox1的>
    < BeginStoryboard故事板={StaticResource的OnClick1}/>
< /&的EventTrigger GT;

或引发特定ListBoxItem中,你需要(其中ITEM1是一个命名ListBoxItem中):

 <的EventTrigger RoutedEvent =ListBoxItem.SelectedSOURCENAME =ITEM1>
    < BeginStoryboard故事板={StaticResource的OnClick1}/>
< /&的EventTrigger GT;

I'm having the darndest time figuring this out: say I've got two Button and three TextBlocks. I want either button to trigger a simple Storyboard on ALL TextBlocks. Currently I'm trying to define a generic Textblock style that contains the Storyboard, and then the trigger comes from any Button click. This is the closest I've come but the app crashes on startup...what am I don't wrong here:

<Window.Resources>

<Style TargetType="TextBlock" >
	<Setter Property="Foreground" Value="Blue" />
	<Style.Resources>
		<Storyboard x:Key="TextBlockOpacity" Storyboard.TargetProperty="Opacity">
			<DoubleAnimation From="0" To="1" />
		</Storyboard>
	</Style.Resources>		
</Style>

<Window.Triggers>
	<EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button">
		<BeginStoryboard Storyboard="{StaticResource TextBlockOpacity}"/>
	</EventTrigger>
</Window.Triggers>


<Grid x:Name="LayoutRoot">
	<Button x:Name="button" HorizontalAlignment="Left" Margin="51,54,0,0" VerticalAlignment="Top" Width="96" Height="45" Content="Button"/>

	<TextBlock x:Name="textBlock1" Margin="228,54,172,0" VerticalAlignment="Top" Height="45" FontSize="26.667" Text="TextBlock" TextWrapping="Wrap" />
	<TextBlock x:Name="textBlock2" Margin="228,103,172,0" VerticalAlignment="Top" Height="45" FontSize="26.667" Text="Hello" TextWrapping="Wrap"/>
</Grid>

解决方案

Based on kek444's Xaml-only solution, I present a slightly improved version that doesn't rely on the DataContext of the button and can have multiple triggers.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">
    <Window.Resources>
        <UIElement x:Key="OpacityCounter" Opacity="0"/>
        <Style TargetType="TextBlock">
    		<Setter Property="Opacity" Value="{Binding Source={StaticResource OpacityCounter}, Path=Opacity}" />
    	</Style>
    	<Storyboard x:Key="OnClick1">
    		<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.Target="{StaticResource OpacityCounter}" Storyboard.TargetProperty="(UIElement.Opacity)">
    			<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
    			<SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1"/>
    		</DoubleAnimationUsingKeyFrames>
    	</Storyboard>
    </Window.Resources>
    <Window.Triggers>
    	<EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button1">
    		<BeginStoryboard Storyboard="{StaticResource OnClick1}"/>
    	</EventTrigger>
    	<EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button2">
    		<BeginStoryboard Storyboard="{StaticResource OnClick1}"/>
    	</EventTrigger>
    </Window.Triggers>

    <Grid x:Name="LayoutRoot">
    	<StackPanel>
    		<StackPanel Orientation="Horizontal">
    			<Button x:Name="button1" Width="131" Height="37" Content="Button 1" Margin="0,0,0,22"/>
    			<Button x:Name="button2" Width="131" Height="37" Content="Button 2" Margin="0,0,0,22"/>
    		</StackPanel>
    		<TextBlock x:Name="textBlock" Height="27" Text="TextBlock 1" TextWrapping="Wrap" />
    		<TextBlock x:Name="textBlock1" Height="27" Text="TextBlock 2" TextWrapping="Wrap" />
    		<TextBlock x:Name="textBlock2" Height="27" Text="TextBlock 3" TextWrapping="Wrap" />
    		<TextBlock x:Name="textBlock3" Height="27" Text="TextBlock 4" TextWrapping="Wrap" />
    	</StackPanel>
    </Grid>
</Window>

To use a ListBox as a trigger mechanism (provided you have a ListBox named "listbox1" someplace, add the following to Window.Triggers:

<EventTrigger RoutedEvent="Selector.SelectionChanged" SourceName="listbox1">
    <BeginStoryboard Storyboard="{StaticResource OnClick1}"/>
</EventTrigger>

or to trigger off a specific ListBoxItem, you'll need (where item1 is a named ListBoxItem):

<EventTrigger RoutedEvent="ListBoxItem.Selected" SourceName="item1">
    <BeginStoryboard Storyboard="{StaticResource OnClick1}"/>
</EventTrigger>

这篇关于WPF - 它得是更容易比我做它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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