DataTemplate中的故事板指的是extern控件

时间:2012-12-07 09:25:13

标签: c# wpf storyboard datatemplate itemscontrol

需要做出反应。当您按项目控件上的按钮时,弹出隐藏。我做了一个布局,假设它应该工作,但不起作用,请帮助。

<Style x:Key="gbListViewItemStyle"
         TargetType='{x:Type ListViewItem}' BasedOn='{StaticResource BaseListBoxItemStyle}'>
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                            <ToggleButton x:Name="pupMenuButton" Command="{Binding Path=ActionCommand}" Style="{DynamicResource FlatToggleButtonStyle}">
                                <Grid>
                                    <TextBlock>text</TextBlock>
                                </Grid>
                            </ToggleButton>
                            <Popup Placement="Bottom" AllowsTransparency="True" StaysOpen="False"
                               PopupAnimation="Fade" x:Name="pupMenu" 
                                   IsOpen="{Binding ElementName=pupMenuButton, Path=IsChecked}">
                                <ItemsControl ItemsSource="{Binding Path=ListItems}" >
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel/>
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Button Content="{Binding Text1}" Command="{Binding Path=ActionCommand}" CommandParameter="{Binding}" Style="{DynamicResource ButtonStyleFlatBorder}">
                                                <Button.Triggers>
                                                    <EventTrigger RoutedEvent="ButtonBase.Click">
                                                        <BeginStoryboard>
                                                            <Storyboard>
                                                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(ToggleButton.IsChecked)" Storyboard.TargetName="pupMenuButton">
                                                                    <DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
                                                                </BooleanAnimationUsingKeyFrames>
                                                            </Storyboard>
                                                        </BeginStoryboard>
                                                    </EventTrigger>
                                                </Button.Triggers>
                                            </Button>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </Popup>
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

返回错误:“在名称空间”System.Windows.Controls.Button“中找不到名称”pupMenuButton“。” 这里的故事板不起作用,为什么?如何工作?

1 个答案:

答案 0 :(得分:1)

在xaml中,有名称范围http://msdn.microsoft.com/en-us/library/ms746659.aspx的概念。一个名称范围内的命名元素不知道另一个名称范围内的元素。在这种情况下,DataTemplate for ItemTemplate属性是它自己的名称范围,因此绑定到元素名称pupMenuButton将不起作用。您可以在网格级别侦听click事件,因为网格与pupMenuButton ToggleButton位于同一个名称范围内。这样做会表现相同,因为datatemplate创建的每个按钮都会引发最终到达网格的冒泡点击事件。

<Style x:Key="gbListViewItemStyle"
     TargetType='{x:Type ListViewItem}' BasedOn='{StaticResource BaseListBoxItemStyle}'>
<Setter Property="ContentTemplate">
    <Setter.Value>
        <DataTemplate>
            <Grid>
                <Grid.Triggers>
                    <EventTrigger RoutedEvent="ButtonBase.Click">
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(ToggleButton.IsChecked)" Storyboard.TargetName="pupMenuButton">
                                    <DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Grid.Triggers>
                <ToggleButton x:Name="pupMenuButton" Command="{Binding Path=ActionCommand}" Style="{DynamicResource FlatToggleButtonStyle}">
                    <Grid>
                        <TextBlock>text</TextBlock>
                    </Grid>
                </ToggleButton>
                <Popup Placement="Bottom" AllowsTransparency="True" StaysOpen="False"
                           PopupAnimation="Fade" x:Name="pupMenu" 
                               IsOpen="{Binding ElementName=pupMenuButton, Path=IsChecked}">
                    <ItemsControl ItemsSource="{Binding Path=ListItems}" >
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Button Content="{Binding Text1}" Command="{Binding Path=ActionCommand}" CommandParameter="{Binding}" Style="{DynamicResource ButtonStyleFlatBorder}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Popup>
            </Grid>
        </DataTemplate>
    </Setter.Value>
</Setter>

等等我刚才意识到这是在做什么。您希望关闭弹出窗口以响应单击弹出窗口中的按钮。这将是一个使用Restyled MenuItem的好地方,因为这正是菜单的用途。