C# 如何在资源中按ElementName绑定?

C# 如何在资源中按ElementName绑定?,c#,.net,wpf,xaml,.net-4.5,C#,.net,Wpf,Xaml,.net 4.5,它是有效的,但并不总是有效的 工作 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.

它是有效的,但并不总是有效的

  • 工作
  • 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">
    <Window.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>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="UIElement.MouseDown" SourceName="ellipse">
            <BeginStoryboard Storyboard="{StaticResource OnMouseDown1}"/>
        </EventTrigger>
    </Window.Triggers>
    <Viewbox x:Name="viewbox">
        <Ellipse x:Name="ellipse" Width="100" Height="100" Fill="Red"></Ellipse>
    </Viewbox></Window>
    
    <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>
    
    <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>
    
    
    
  • 工作
  • 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">
    <Window.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>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="UIElement.MouseDown" SourceName="ellipse">
            <BeginStoryboard Storyboard="{StaticResource OnMouseDown1}"/>
        </EventTrigger>
    </Window.Triggers>
    <Viewbox x:Name="viewbox">
        <Ellipse x:Name="ellipse" Width="100" Height="100" Fill="Red"></Ellipse>
    </Viewbox></Window>
    
    <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>
    
    <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>
    
    
    
    UserControl1.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>
    
    <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>
    
    
    
  • 不起作用
  • 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">
    <Window.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>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="UIElement.MouseDown" SourceName="ellipse">
            <BeginStoryboard Storyboard="{StaticResource OnMouseDown1}"/>
        </EventTrigger>
    </Window.Triggers>
    <Viewbox x:Name="viewbox">
        <Ellipse x:Name="ellipse" Width="100" Height="100" Fill="Red"></Ellipse>
    </Viewbox></Window>
    
    <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>
    
    <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>
    
    
    
    UserControl1.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>
    
    <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>
    
    
    
    例外情况:

    PresentationCore.dll中发生类型为“System.Windows.Media.Animation.AnimationException”的未处理异常 其他信息:无法使用“System.Windows.Media.Animation.DoubleAnimation”对“System.Windows.Controls.Viewbox”上的“Width”属性设置动画。有关详细信息,请参见内部异常


    如何使其工作?

    内部异常是什么?@Nikola.Lukovic我不知道如何检查内部异常。我尝试过,但检查失败。内部异常应该是
    System.Windows.Media.Animation.DoubleAnimation不能使用默认的原始值NaN
    。设置视图框的宽度应该可以解决这个问题。@Elite026是的,但我需要自动设置宽度。如示例中所述,有时有效,有时无效。