【问题标题】:Removing full screen in xaml dynamically动态删除xaml中的全屏
【发布时间】:2017-03-13 04:51:01
【问题描述】:

这对你们大多数人来说可能非常简单,我是 XAML 和 WPF 的新手。 我有一个启动 att 全屏的应用程序,我通过添加

来做到这一点
  WindowState="Maximized"
  WindowStyle="None"

我想要一个简单地消除这部分的按钮。我在 xaml 中有一个“全屏”按钮,通过单击,他在我的代码中调用了“FullScreen_Click”函数。 我只需要知道在代码中写什么,如果它处于全屏模式,它将消除全屏,并在不是时将其恢复为全屏。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    试试这个:

    private void FullScreen_Click(object sender, RoutedEventArgs e)
    {
        WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
    }
    

    每次点击Button 时,这将在WindowState.MaximizedWindowState.Normal 之间切换。

    【讨论】:

      【解决方案2】:

      对于仅用于参考的 xaml 技术,可以比较查看 xaml 示例(但我会做 @mm8 的路线,它更简单);

      1. Bind your property to that of another like: 
      

      <Window WindowState="{Binding Tag, ElementName=toggleState}" .... />

      2. Use a `ToggleButton` or similar control and `Triggers`
      

      .

      <!-- like this PoC -->
          <Grid>
              <Grid.Resources>
                  <Style x:Key="cwWindowState_PoC" TargetType="{x:Type ToggleButton}">
                      <Setter Property="Tag" Value="Maximized"/>
                      <Setter Property="Template">
                          <Setter.Value>
                              <ControlTemplate TargetType="ToggleButton">
                                  <Grid>
                                      <Border Background="{TemplateBinding Background}"/>
                                      <ContentPresenter x:Name="MyContentPresenter" 
                                                        Content="{TemplateBinding Tag}" 
                                                        HorizontalAlignment="Center" 
                                                        VerticalAlignment="Center" />
      
                                  </Grid>
                                  <ControlTemplate.Triggers>
                                      <Trigger Property="IsChecked" Value="True">
                                          <Setter Property="Tag" Value="Normal" />
                                      </Trigger>
                                      <Trigger Property="IsChecked" Value="False">
                                          <Setter Property="Tag" Value="Maximized" />
                                      </Trigger>
                                  </ControlTemplate.Triggers>
                              </ControlTemplate>
                          </Setter.Value>
                      </Setter>
                  </Style>
              </Grid.Resources>
      
                  <ToggleButton x:Name="toggleState" Content="Click Me" 
                                Background="Green"
                                Style="{StaticResource cwWindowState_PoC}"/>
      
          </Grid>
      

      也可以使用 DataTrigger,但这需要交互触发器,而不仅仅是来自模板的属性设置器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-12
        • 1970-01-01
        • 2017-10-13
        • 1970-01-01
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        相关资源
        最近更新 更多