如何在Resources中通过ElementName绑定?

时间:2016-03-21 11:01:53

标签: c# .net wpf xaml .net-4.5

它有效,但并非总是如此。

  1. 作品。
  2. MainWindow.xaml:

    <Window x:Class="WpfApplication2.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <local:UserControl1></local:UserControl1></Window>
    
    1. 作品。
    2. MainWindow.xaml:

      <UserControl x:Class="WpfApplication2.UserControl1"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:WpfApplication2"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300">
      <UserControl.Resources>
          <Storyboard x:Key="OnMouseDown1">
              <DoubleAnimation Storyboard.TargetProperty="Width" Storyboard.TargetName="viewbox"
                               From="{Binding ElementName=viewbox, Path=ActualWidth}"
                               To="0"
                               Duration="0:0:1">
              </DoubleAnimation>
          </Storyboard>
      </UserControl.Resources>
      <Viewbox x:Name="viewbox">
          <Ellipse x:Name="ellipse" Width="100" Height="100" Fill="Red">
              <Ellipse.Triggers>
                  <EventTrigger RoutedEvent="UIElement.MouseDown" SourceName="ellipse">
                      <BeginStoryboard Storyboard="{StaticResource OnMouseDown1}"/>
                  </EventTrigger>
              </Ellipse.Triggers>
          </Ellipse>
      </Viewbox></UserControl>
      

      UserControl1.xaml:

      <Window x:Class="WpfApplication2.MainWindow"
          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"
          xmlns:local="clr-namespace:WpfApplication2"
          mc:Ignorable="d"
          Title="MainWindow" Height="350" Width="525">
      <local:UserControl1></local:UserControl1></Window>
      
      1. 不能工作。
      2. MainWindow.xaml:

        <UserControl x:Class="WpfApplication2.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:WpfApplication2"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <UserControl.Resources>
            <Storyboard x:Key="OnMouseDown1">
                <DoubleAnimation Storyboard.TargetProperty="Width" Storyboard.TargetName="viewbox"
                                 From="{Binding ElementName=viewbox, Path=ActualWidth}"
                                 To="0"
                                 Duration="0:0:1">
                </DoubleAnimation>
            </Storyboard>
        </UserControl.Resources>
        <UserControl.Triggers>
            <EventTrigger RoutedEvent="UIElement.MouseDown" SourceName="ellipse">
                <BeginStoryboard Storyboard="{StaticResource OnMouseDown1}"/>
            </EventTrigger>
        </UserControl.Triggers>
        <Viewbox x:Name="viewbox">
            <Ellipse x:Name="ellipse" Width="100" Height="100" Fill="Red"></Ellipse>
        </Viewbox></UserControl>
        

        UserControl1.xaml:

        $('#calendar').fullCalendar('updateEvent', event);
        

        例外:

          

        未处理的类型&#39; System.Windows.Media.Animation.AnimationException&#39;发生在PresentationCore.dll中   附加信息:无法设置&#39;宽度&#39; “System.Windows.Controls.Viewbox”上的属性&#39;使用&#39; System.Windows.Media.Animation.DoubleAnimation&#39;。有关详细信息,请参阅内部异常。

        如何让它发挥作用?

0 个答案:

没有答案