0

需要做出反应。当您按下Popup隐藏的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”。” 故事板在这里不起作用,为什么?怎么做作业?

4

1 回答 1

1

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

<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 的好地方,因为这正是 Menu 的用途。

于 2012-12-09T05:35:24.517 回答