在“ System.Windows.Controls.Button”的名称范围中找不到名称 [英] Name cannot be found in the name scope of 'System.Windows.Controls.Button'

查看:82
本文介绍了在“ System.Windows.Controls.Button”的名称范围中找不到名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的按钮

<Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <DockPanel Width="Auto">
            <Button DockPanel.Dock="Top">                                           
                <Button.Template>
                    <ControlTemplate >
                        <Image  Source="{Binding image}"/>
                    </ControlTemplate>
                </Button.Template>                    
                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <BeginStoryboard>
                            <Storyboard>
                                <local:GridLengthAnimation
                                    Storyboard.TargetName="col1"
                                    Storyboard.TargetProperty="Width"
                                    LeftGridWidth="*" RightGridWidth="1*" Duration="0:0:2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Button.Triggers>
            </Button>
        </DockPanel>
    </DataTemplate>
</Window.Resources>

<Grid>
     ...
     ...
     <Grid Grid.Row="2" >

        <Grid.ColumnDefinitions>
            <ColumnDefinition Name="col1" Width="{Binding ElementName=root, Path=DataContext.gla.LeftGridWidth}" />
            <ColumnDefinition Name="col2" Width="{Binding ElementName=root, Path=DataContext.gla.RightGridWidth}" />
        </Grid.ColumnDefinitions>

        <Grid x:Name="LeftGrid" Grid.Row="2" Grid.Column="0" >
              <Border BorderThickness="1" BorderBrush="Red">
                  <ItemsControl ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ElementName=root, Path=DataContext._movies}">
                      <ItemsControl.ItemsPanel>
                          <ItemsPanelTemplate>
                              <UniformGrid Columns="5"/>
                          </ItemsPanelTemplate>
                      </ItemsControl.ItemsPanel>
                  </ItemsControl>
              </Border>
        </Grid>
     </Grid>
</Grid>

问题是 col1 不是由 Storyboard.TargetName = col1 拾取。我收到错误消息:

The issue is that col1 is not being picked up by Storyboard.TargetName="col1". I receive the error:

PresentationFramework.dll中发生了'System.InvalidOperationException'类型的未处理异常

其他信息:在 System.Windows.Controls.Button的名称范围中找不到 col1名称。

我认为这可能与我正在使用 Items Control ...这一事实有关认为会尝试在任何包含元素中找到 col1 。我不确定如何解决此问题。

I think it may have to do with the fact that I'm using Items Control... I thought that col1 would be tried to be found in any containing elements. I'm not sure how to resolve this issue.

任何帮助将不胜感激!

推荐答案

问题确实是由 ItemsControl 引起的,它使 Button 及其资源位于不同的范围。

The problem is indeed caused by the ItemsControl which makes the Button and its resources in a different scope.

一个简单的解决方法是使用 Storyboard,而不是使用 Storyboard.TargetName 。目标 绑定,而是类似这样的东西-

A simple fix would be, instead of using Storyboard.TargetName, use Storyboard.Target binding instead, something like this -

Storyboard.Target = { ElementName = col1}

这篇关于在“ System.Windows.Controls.Button”的名称范围中找不到名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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