wpf 在“System.Windows.Controls.Button”的名称范围内找不到名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27714597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Name cannot be found in the name scope of 'System.Windows.Controls.Button'
提问by Justin XL
I have the following Templatefor my Button
我有以下Template为我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 col1is not being picked up by Storyboard.TargetName="col1". I receive the error:
问题是col1没有被Storyboard.TargetName="col1". 我收到错误:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
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'.
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 col1would be tried to be found in any containing elements. I'm not sure how to resolve this issue.
我认为这可能与我正在使用的事实有关Items Control......我认为col1会尝试在任何包含元素中找到它。我不知道如何解决这个问题。
Any help would be greatly appreciated!
任何帮助将不胜感激!
回答by Justin XL
The problem is indeed caused by the ItemsControlwhich makes the Buttonand its resources in a different scope.
问题确实是由ItemsControl使Button及其资源处于不同范围的 引起的。
A simple fix would be, instead of using Storyboard.TargetName, use Storyboard.Targetbindinginstead, something like this -
一个简单的解决方法是,不要使用Storyboard.TargetName,而是使用Storyboard.Target绑定,就像这样 -
Storyboard.Target="{Binding ElementName=col1}"
Storyboard.Target="{Binding ElementName=col1}"
回答by Abhishek
Try using x:Name instead of Name in
尝试使用 x:Name 而不是 Name
<ColumnDefinition Name="col1" Width="{Binding ElementName=root, Path=DataContext.gla.LeftGridWidth}" />

