简体   繁体   English

WPF将UserControl绑定到资源

[英]WPF Bind UserControl to resource

At the moment, I'm doing some experimentation. 目前,我正在做一些实验。 My current scenario: I have a StoryBoard to transition between 2 UserControls (for example, it shrinks the current UserControl then grows the other). 我当前的场景:我有一个StoryBoard可以在2个UserControl之间进行转换(例如,它缩小了当前的UserControl,然后增长了另一个)。

What I want to do is to have 2 UserControls defined as part of the XAML, with keys of "Current" and "Next". 我想要做的是将2个UserControls定义为XAML的一部分,使用“Current”和“Next”键。 The Current should be bound to the currently viewed UserControl. Current应绑定到当前查看的UserControl。 The Next should be bound before transition, so the StoryBoard knows which element to transition to. Next应该在转换之前绑定,因此StoryBoard知道要转换到哪个元素。 Here's where I'm stuck: ENTIRELY using XAML, how would one go about this? 这就是我被困的地方:完全使用XAML,怎么会这样呢?

I have a simple StoryBoard that is a Resource of the ItemsControl, as well as two UserControl items: 我有一个简单的StoryBoard,它是ItemsControl的Resource,还有两个UserControl项:

<ItemsControl.Resources>
  <Storyboard x:Key="TransitionStoryboard">
    <!-- Shrink this one. -->
    <DoubleAnimation Storyboard.Target="{Binding Current}" Storyboard.TargetProperty="Width" To="0" Duration="0:0:1"/>
    <!--Grow the next.-->
    <DoubleAnimation Storyboard.Target="{Binding Next}" Storyboard.TargetProperty="Width" To="100" BeginTime="0:0:1" Duration="0:0:1"/>
  </Storyboard>

  <UserControl x:Key="Current"/>
  <UserControl x:Key="Next" Width="0"/>
</ItemsControl.Resources>

So, when I define a new UserControl that belongs to the ItemsControl (like this): 所以,当我定义一个属于ItemsControl的新UserControl时(如下所示):

<my:Control1 x:Name="ControlOne"/>

How do I set the "Current" UserControl top be ControlOne? 如何将“当前”UserControl设置为ControlOne? Then, when I want to transition, how do I set one as "Next"? 然后,当我想要转换时,如何将其设置为“下一步”? And how can I change these after a trigger? 如何在触发后更改这些?

Thanks 谢谢

This is a total mess. 这是一团糟。 You seem to completely misunderstand on how the static resources are used. 您似乎完全误解了静态资源的使用方式。

To achieve what you are trying to do, you should first decide on what will trigger the animation. 要实现您要做的事情,您应该首先决定触发动画的内容。 Ideally it should be some DependencyProperty on your controls, or a property on your view model (which implements INotifyPropertyChanged ). 理想情况下,它应该是控件上的一些DependencyProperty ,或视图模型上的属性(实现INotifyPropertyChanged )。 For example, you can declare an IsSelected property. 例如,您可以声明IsSelected属性。 Then you should create a style which triggers "grow" animation when control is selected and triggers "shrink" animation, when control loses selection. 然后你应该创建一个样式,当选择控件时触发“增长”动画,并在控件失去选择时触发“缩小”动画。 For example: 例如:

 <Style TargetType="Control" x:Key="FancyStyle">
     <Style.Triggers>
         <DataTrigger Binding={Binding IsSelected} Value="True">
             <DataTrigger.EnterActions>
                  <BeginStoryboard Storyboard="{StaticResource YourGrowAnim}"/>
             </DataTrigger.EnterActions>
             <DataTrigger.ExitActions>
                  <BeginStoryboard Storyboard="{StaticResource YourShrinkAnim}"/>
             </DataTrigger.ExitActions>
         </DataTrigger>
     </Style.Triggers>
 </Style>

Then you should assign that style to every control, you want animated this way and set up transitions between IsSelected properties. 然后,您应该将该样式分配给每个控件,您希望以这种方式设置动画并在IsSelected属性之间设置过渡。 You can also use EventTrigger instead and bind animations to events (for example you can trigger those animations when control gets/loses focus). 您也可以使用EventTrigger并将动画绑定到事件(例如,当控件获得/失去焦点时,您可以触发这些动画)。

You should also fix your "grow" animation, it wont work, most likely. 你也应该修复你的“成长”动画,它很可能无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM