2011-07-07 8 views
0

EventTriggersを使用しているときに問題が発生しています。私は2つのボタンと2つのEventTriggersを持っています。これらのボタンからClickイベントを待ち受けたい場合、トリガーはコントロールの高さをアニメーション化するStoryBoardを開始する必要があります。これにより特定のユーザーインターフェイス要素のEventTriggerに関する問題

<UserControl x:Class="MyWindow" 
      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" 
      > 

    <UserControl.Resources> 
     <Storyboard x:Key="GrowPanel1"> 
      <DoubleAnimation To="400" Duration="0:0:0.75" Storyboard.TargetName="Panel1" Storyboard.TargetProperty="Height" /> 
     </Storyboard> 
     <Storyboard x:Key="GrowPanel2"> 
      <DoubleAnimation To="400" Duration="0:0:0.75" Storyboard.TargetName="Panel2" Storyboard.TargetProperty="Height" /> 
     </Storyboard> 
    </UserControl.Resources> 

    <Grid > 
     <Grid.Triggers> 
      <EventTrigger RoutedEvent="Button.Click" SourceName="TriggerElement1" > 
       <EventTrigger.Actions> 
        <BeginStoryboard Storyboard="{StaticResource GrowPanel1}" /> 
       </EventTrigger.Actions> 
      </EventTrigger> 
      <EventTrigger RoutedEvent="Button.Click" SourceName="TriggerElement2" > 
       <EventTrigger.Actions> 
        <BeginStoryboard Storyboard="{StaticResource GrowPanel2}" /> 
       </EventTrigger.Actions> 
      </EventTrigger> 
     </Grid.Triggers> 
     <Border BorderBrush="HotPink" BorderThickness="2"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="Auto" /> 
       </Grid.ColumnDefinitions> 


       <ContentPresenter Panel.ZIndex="20" Grid.Column="1" > 
        <ContentPresenter.Content> 
         <StackPanel Orientation="Horizontal"> 
          <Button x:Name="TriggerElement2" Background="BurlyWood" Width="30" Height="30" VerticalAlignment="Top" /> 
          <Button x:Name="TriggerElement1" Background="Chartreuse" Width="30" Height="30" VerticalAlignment="Top" /> 
         </StackPanel> 
        </ContentPresenter.Content> 
       </ContentPresenter> 

       <Grid Grid.Column="0" Grid.ColumnSpan="2"> 
        <my1:TreeViewNav Name="Panel1" Height="0" VerticalAlignment="Top" /> 
        <my:ListViewNav x:Name="Panel2" Height="0" VerticalAlignment="Top" /> 
       </Grid> 
      </Grid> 
     </Border> 
    </Grid> 
</UserControl> 

私は、実行時エラーを取得:

EventTriggerを削除

The most likely causing exception was was: 'System.Windows.Markup.XamlParseException: 'Initialization of 'System.Windows.Controls.Grid' threw an exception.' Line number '23' and line position '6'. ---> System.ArgumentException: Cannot find a FrameworkElement with Name 'TriggerElement1'. at System.Windows.FrameworkElement.FindNamedFrameworkElement(FrameworkElement startElement, String targetName) at System.Windows.EventTrigger.ProcessOneTrigger(FrameworkElement triggersHost, TriggerBase triggerBase) at System.Windows.EventTrigger.ProcessTriggerCollection(FrameworkElement triggersHost) at System.Windows.FrameworkElement.TryFireInitialized() at System.Windows.FrameworkElement.EndInit() at MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin) --- End of inner exception stack trace ---


私は絵コンテを開始すると何の問題もないが、私は単にEventTriggerのsourcenameここを指定すると問題を抱えていますSourceNameプロパティはエラーを修正しますが、どのボタンがクリックされたかに応じて別のStoryBoardを起動する必要があります。だから私が知る限り、私はSourceNameを使用する必要があります。

TriggerElement1というボタンがWPFで見つからないのはなぜですか?私は、RouteEventsがビジュアルツリーの上にバブリングされると思ったので、トリガーが十分高いレベルにある限り、名前付けに問題はないはずです。 Panel1というStoryBoard.Targetを見つけることは問題ありません。異なるストーリーボードを起動するために2つのボタンを取得する正しい方法をとっていますか?

答えて

0

ボタン "TriggerElement1"はContentPresenter内にあるため、アプリケーションは初期化時にボタンを見つけることができません。 contentpresentorコンテンツは実行時にレンダリングされるためです。

私はあなたのコードを少し変更しましたが、これはうまくいきました...これがあなたの問題を解決することを願っています。

<UserControl x:Class="WpfApplication1.MyWindow" 
      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"> 

    <UserControl.Resources> 
     <Storyboard x:Key="GrowPanel1"> 
      <DoubleAnimation To="400" Duration="0:0:0.75" Storyboard.TargetName="Panel1" Storyboard.TargetProperty="Height" /> 
      <DoubleAnimation To="0" Duration="0:0:0.75" Storyboard.TargetName="Panel2" Storyboard.TargetProperty="Height" /> 
     </Storyboard> 
     <Storyboard x:Key="GrowPanel2"> 
      <DoubleAnimation To="400" Duration="0:0:0.75" Storyboard.TargetName="Panel2" Storyboard.TargetProperty="Height" /> 
      <DoubleAnimation To="0" Duration="0:0:0.75" Storyboard.TargetName="Panel1" Storyboard.TargetProperty="Height" /> 
     </Storyboard> 
    </UserControl.Resources> 
    <Grid> 
     <Border BorderBrush="HotPink" BorderThickness="2"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="Auto" /> 
       </Grid.ColumnDefinitions> 

       <StackPanel Orientation="Horizontal" Grid.Column="1" > 
        <Button x:Name="TriggerElement2" Background="BurlyWood" Width="30" Height="30" VerticalAlignment="Top"> 
         <Button.Triggers> 
          <EventTrigger RoutedEvent="Button.Click"> 
           <EventTrigger.Actions> 
            <BeginStoryboard Storyboard="{StaticResource GrowPanel2}" /> 
           </EventTrigger.Actions> 
          </EventTrigger> 
         </Button.Triggers> 
        </Button> 
        <Button x:Name="TriggerElement1" Background="Chartreuse" Width="30" Height="30" VerticalAlignment="Top" > 
         <Button.Triggers> 
          <EventTrigger RoutedEvent="Button.Click"> 
           <EventTrigger.Actions> 
            <BeginStoryboard Storyboard="{StaticResource GrowPanel1}" /> 
           </EventTrigger.Actions> 
          </EventTrigger> 
         </Button.Triggers> 
        </Button> 
       </StackPanel> 

       <Grid Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Bottom"> 
        <Grid Name="Panel1" Height="0" VerticalAlignment="Top" Background="Red"/> 
        <Grid x:Name="Panel2" Height="0" VerticalAlignment="Top" Background="Blue"/> 
       </Grid> 
      </Grid> 
     </Border> 
    </Grid> 
</UserControl> 
+0

ありがとう、ありがとう!私はContentPresenterについてはそれほど気付きませんでした。 – user833115xxx

関連する問題