Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

c# - Name cannot be found in the name scope of 'System.Windows.Controls.Button'

I have the following Template for my 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>

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

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll

Additional information: 'col1' name cannot be found in the name scope of 'System.Windows.Controls.Button'.

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.

Any help would be greatly appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

Storyboard.Target="{Binding ElementName=col1}"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...