c#
  • wpf
  • storyboard
  • datatemplate
  • itemscontrol
  • 2012-12-07 4 views 0 likes 
    0

    반응을 필요로합니다. 에 단추를 누르면 ItemsControl팝업 숨기기. 나는 그 일을해야한다고 가정하고 레이아웃을했지만 작동하지 않습니다. 제발 도와주세요.DataTemplate의 스토리 보드는 extern 컨트롤을 참조합니다.

    <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

    xaml에는 이름 범위 http://msdn.microsoft.com/en-us/library/ms746659.aspx이라는 개념이 있습니다. 한 네임 스코프 내의 명명 된 요소는 다른 네임 스코프의 요소에 대해 알지 못합니다. 이 경우 ItemTemplate 속성의 DataTemplate 속성은 자체 이름 범위이므로 요소 이름 pupMenuButton에 바인딩하면 작동하지 않습니다. 그리드가 pupMenuButton ToggleButton과 동일한 이름 범위에 있기 때문에 그리드 레벨에서 click 이벤트를 수신 할 수 있습니다. 데이터 템플릿에 의해 생성 된 각 버튼은 결국 그리드에 도달하는 버블 링 클릭 이벤트를 발생시키기 때문에 동일하게 동작합니다.

    <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가 정확히 무엇인지에 대한 것이기 때문입니다.

    +0

    "Restyled MenuItem"- 이것은 내가 필요한 것입니다. 고맙습니다. – Ivan

    관련 문제