source

WPF 페이지에서 네비게이션바를 숨기는 방법

manysource 2023. 4. 15. 09:04

WPF 페이지에서 네비게이션바를 숨기는 방법

WPF를 사용하여 작성한 페이지에서 네비게이션바를 숨깁니다.난 시도했다.ShowsNavigationUI = false단, 컨트롤은 아직 표시되어 있습니다.

Container 페이지에 네비게이션 바를 포함하지 않는 의도를 지정합니다.NavigationUIVisibility소유물.

<Frame NavigationUIVisibility="Hidden" Panel.ZIndex="1" ... />

매우 간단한 구현입니다.

<Frame x:Name="_FrameName" NavigationUIVisibility="Hidden" />

설정ShowsNavigationUI=False에서Page할 수 있을 거야단, 적어도1개의 이벤트에서 장애가 발생하는 버그가 있는 것 같습니다.

  1. 페이지가 이미 있습니다.NavigationWindow이것이 설정되어 있는 경우
  2. 페이지가 이동했다가 다시 되돌아갑니다.

실패하게 만드는 다른 시나리오가 있을지도 몰라

이 작업을 완전히 안정적으로 수행하기 위해 저는 페이지를 무시하는 일을 합니다.표시내비게이션UI 속성 전체를 Navigation Window에서 설정합니다.이것은 완전히 신뢰할 수 있는 것 같다.

페이지 생성자에서 이 작업을 수행할 수 있는 방법은 다음과 같습니다.

Dispatcher.BeginInvoke(ApplicationPriority.Render, new Action(() =>
{
  var navWindow = Window.GetWindow(this) as NavigationWindow;
  if(navWindow!=null) navWindow.ShowsNavigationUI = false;
}));

이 경우 ShowsNavigation을 설정하지 마십시오.임의의 페이지 오브젝트의 UI.

참고로 ControlTemplate를 변경하여 원하는 방식으로 NavigationWindow의 파일링을 변경할 수도 있습니다.예를 들어, 실제 페이지 내용을 제외한 모든 내용이 삭제됩니다.

  <Style TargetType="{x:Type NavigationWindow}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type NavigationWindow}">

          <AdornerDecorator>
            <ContentPresenter Name="PART_NavWinCP" 
                              ClipToBounds="true"/>
          </AdornerDecorator>
          
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

프레임을 사용하는 경우 프레임의 기본 스타일을 변경하여 탐색 버튼(아래 그림)을 제거할 수 있습니다.Navigation Window에서도 같은 접근방식을 실행할 수 있습니다.원래 페이지 설정을 시도했습니다.표시내비게이션UI와 그것은 아무런 효과가 없었다.Resource Dictionary에 다음 스타일을 추가하면 정상적으로 작동합니다.

<Style TargetType="{x:Type Frame}">
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Frame}">
        <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}">
          <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Name="PART_FrameCP" />
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

이건 정말 쉬웠어요.메인 창에서 다음을 수행합니다.

public MainWindow()
   public partial class MainWindow : NavigationWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            ShowsNavigationUI = false;
        }
    }
}

버튼을 클릭하여 새 페이지를 여는 이벤트가 있는 경우 다음을 수행하십시오.

private void btnEndUserSearch_Click(object sender, RoutedEventArgs e)
{
            EndUser EndUserSearchPage = new EndUser();
            this.NavigationService.Navigate(EndUserSearchPage);
            EndUserSearchPage.ShowsNavigationUI = false;
}

위는 네비게이션 창에서만 작동하지만 일반 WPF 창을 사용하고 있습니다.내비게이션 창보다 낫다는 의견도 있습니다.DockPanel을 사용하여 페이지를 호스트하고 있습니다.내 솔루션은 DockPanel용 새 템플릿을 만들지만 버튼을 추가하거나 숨기지 않습니다(StackPanel Visibility= 참조).숨김')잘 먹힌다.

<DockPanel>    
    <Frame x:Name="_mainFrame">
    <Frame.Template>

        <ControlTemplate TargetType="Frame">
            <DockPanel Margin="7">
                <StackPanel Visibility="Hidden"
                    Margin="0"
                    Orientation="Horizontal"
                    DockPanel.Dock="Top"
                    >
                    <!--<Button
                        Content="Avast! Go back!" 
                        Command="{x:Static NavigationCommands.BrowseBack}" 
                        IsEnabled="{TemplateBinding CanGoBack}" 
                        />
                    <Button 
                        Content="Forward you dogs!" 
                        Command="{x:Static NavigationCommands.BrowseForward}" 
                        IsEnabled="{TemplateBinding CanGoForward}" 
                        />-->
                </StackPanel>

               <Border>
                    <ContentPresenter />
               </Border>
            </DockPanel>
        </ControlTemplate>

        </Frame.Template>
    </Frame>
</DockPanel>

가장 쉽고 간단한 방법 중 하나는 다음을 추가하는 것입니다.표시내비게이션UI = false. Initialize Component() 아래의 cs 파일 생성자에 있습니다.

초보자라면 Main Window.xaml에서 간단하게 코드를 변경할 수 있습니다.

    <Window>
       <Grid >
          <Frame x:Name="LeftFrame" NavigationUIVisibility="Hidden"/>
          <Frame x:Name="RightFrame" NavigationUIVisibility="Hidden"/>
       </Grid>
    </Window>

프레임의 Content 속성을 동적으로 변경할 때마다 이 문제가 발생했고, click() 이벤트에서 다음 코드를 사용하여 해결했습니다.

ContentFrame.NavigationUIVisibility = NavigationUIVisibility.Hidden;

여기서 Content Frame은 XAML에서 정의된 프레임 이름입니다.

<Frame x:Name="ContentFrame"  />

Navigation Window 자체에서 ShowsNavigation을 사용합니다.UI="False"

언급URL : https://stackoverflow.com/questions/3059650/how-to-hide-the-navigation-bar-in-a-wpf-page