簡體   English   中英

使用自定義ChromeWindow不能進行XAML設計器預覽

[英]No XAML designer preview with custom ChromeWindow

我一直在嘗試構建自定義鑲邊窗口與沒有邊框樣式的窗口。 在Visual Studio中,XAML設計器在內容區域中顯示自定義標題欄,而實際的內容區域控件不可見。 當我編譯並運行它時,一切看起來都正確。

另一個問題是我似乎無法正確顯示標題欄按鈕上的Command綁定。 我嘗試將它們綁定到適當的SystemCommands,但是我也不知道自己在做什么錯。 因此,我只是添加了click事件處理程序,然后在此處執行代碼。 只是試圖為此提供XAML解決方案。 Click事件處理程序可以正常工作。

以下是我的XAML。 XAML的末尾是一個帶有TextBlock的基本網格,上面寫着“我應該是可見的!” 但它不在設計器中。

另附上(如果我有權)顯示設計器在Visual Studio中的外觀的屏幕截圖。 如您所見,放置在自定義標題欄中的控件清晰可見。 您還將在屏幕截圖中看到默認的窗口標題欄也可見。

任何人都可以向我指出為什么我的設計師不是所見即所得的正確方向嗎?

設計師截圖

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:ChromeWindowTest"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="Window1"
    Title="Window1"
    Width="800"
    Height="450"
    Background="#252525"
    Foreground="White"
    mc:Ignorable="d">
<WindowChrome.WindowChrome>
    <WindowChrome CornerRadius="0"
                  GlassFrameThickness="0,0,0,1"
                  NonClientFrameEdges="None"
                  ResizeBorderThickness="5"
                  UseAeroCaptionButtons="True" />
</WindowChrome.WindowChrome>
<Window.Resources>
    <local:WindowStateToMarginConverter x:Key="WindowStateToMarginConverter" />
    <local:WindowStateToVisibilityConverter x:Key="WindowStateToVisibilityConverter" />

    <Style TargetType="TextBlock">
        <Setter Property="Foreground"
                Value="White" />
        <Setter Property="FontFamily"
                Value="Lucida Sans Unicode" />
        <Setter Property="FontSize"
                Value="12 pt" />
        <Setter Property="Margin"
                Value="3,0" />
    </Style>

    <Style x:Key="CaptionButtonStyle"
           TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="LayoutRoot"
                          Width="44"
                          Height="30"
                          Background="Transparent"
                          WindowChrome.IsHitTestVisibleInChrome="True">
                        <TextBlock x:Name="txt"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   FontFamily="Segoe MDL2 Assets"
                                   FontSize="10"
                                   Foreground="White"
                                   RenderOptions.ClearTypeHint="Auto"
                                   Text="{TemplateBinding Content}"
                                   TextOptions.TextFormattingMode="Display"
                                   TextOptions.TextRenderingMode="Aliased" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver"
                                 Value="True">
                            <Setter TargetName="LayoutRoot"
                                    Property="Background"
                                    Value="#F79721" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MinimizeButtonStyle"
           BasedOn="{StaticResource CaptionButtonStyle}"
           TargetType="Button">
        <Setter Property="Content"
                Value="&#xE949;" />
    </Style>

    <Style x:Key="MaximizeButtonStyle"
           BasedOn="{StaticResource CaptionButtonStyle}"
           TargetType="Button">
        <Setter Property="Content"
                Value="&#xE739;" />
        <Setter Property="Visibility"
                Value="{Binding WindowState,
                                Converter={StaticResource WindowStateToVisibilityConverter},
                                ConverterParameter=0}" />
    </Style>

    <Style x:Key="RestoreButtonStyle"
           BasedOn="{StaticResource CaptionButtonStyle}"
           TargetType="Button">
        <Setter Property="Content"
                Value="&#xE923;" />
        <Setter Property="Visibility"
                Value="{Binding WindowState,
                                Converter={StaticResource WindowStateToVisibilityConverter},
                                ConverterParameter=2}" />
    </Style>

    <Style x:Key="CloseButtonStyle"
           TargetType="Button">
        <Setter Property="Content"
                Value="&#xE711;" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="LayoutRoot"
                          Width="44"
                          Height="30"
                          Background="Transparent"
                          WindowChrome.IsHitTestVisibleInChrome="True">
                        <TextBlock x:Name="txt"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   FontFamily="Segoe MDL2 Assets"
                                   FontSize="10"
                                   Foreground="White"
                                   RenderOptions.ClearTypeHint="Auto"
                                   Text="{TemplateBinding Content}"
                                   TextOptions.TextFormattingMode="Display"
                                   TextOptions.TextRenderingMode="Aliased" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver"
                                 Value="True">
                            <Setter TargetName="LayoutRoot"
                                    Property="Background"
                                    Value="Red" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Window.Template>
    <ControlTemplate TargetType="{x:Type Window}">
        <Grid Background="{Binding RelativeSource={RelativeSource TemplatedParent},
                                   Path=Background}">
            <StackPanel Height="32"
                        Margin="{Binding RelativeSource={RelativeSource TemplatedParent},
                                         Path=WindowState,
                                         Converter={StaticResource WindowStateToMarginConverter}}"
                        VerticalAlignment="Top"
                        Orientation="Horizontal">
                <Button HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Click="Icon_Click"
                        WindowChrome.IsHitTestVisibleInChrome="True">
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <Grid Background="Transparent">
                                <ContentPresenter />
                            </Grid>
                        </ControlTemplate>
                    </Button.Template>
                    <Image Width="32"
                           Height="32"
                           Source="{Binding RelativeSource={RelativeSource TemplatedParent},
                                            Path=Icon}"
                           WindowChrome.IsHitTestVisibleInChrome="True" />
                </Button>
                <StackPanel Orientation="Vertical">
                    <TextBlock HorizontalAlignment="Left"
                               VerticalAlignment="Top"
                               FontSize="10 pt"
                               LineHeight="16"
                               LineStackingStrategy="BlockLineHeight"
                               Text="{Binding RelativeSource={RelativeSource TemplatedParent},
                                              Path=Title}" />
                    <TextBlock HorizontalAlignment="Left"
                               VerticalAlignment="Top"
                               FontSize="10 pt"
                               LineHeight="16"
                               LineStackingStrategy="BlockLineHeight"
                               Text="{Binding Path=SubTitle}" />
                </StackPanel>
            </StackPanel>
            <Button x:Name="btn"
                    Margin="3"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Top"
                    Click="btn_Click"
                    Content="Placeholder"
                    WindowChrome.IsHitTestVisibleInChrome="True" />

            <StackPanel HorizontalAlignment="Right"
                        VerticalAlignment="Top"
                        Orientation="Horizontal">
                <Button x:Name="MinimizeButton"
                        Click="MinimizeButton_Click"
                        Style="{DynamicResource MinimizeButtonStyle}" />
                <Button x:Name="MaximizeButton"
                        Click="MaximizeButton_Click"
                        Style="{DynamicResource MaximizeButtonStyle}" />
                <Button x:Name="RestoreButton"
                        Click="MaximizeButton_Click"
                        Style="{DynamicResource RestoreButtonStyle}" />
                <Button x:Name="CloseButton"
                        Click="CloseButton_Click"
                        Style="{DynamicResource CloseButtonStyle}" />
            </StackPanel>
            <ContentPresenter Content="{Binding RelativeSource={RelativeSource FindAncestor,
                                                                               AncestorType=Window},
                                                Path=Content}" />
        </Grid>
    </ControlTemplate>
</Window.Template>
<Grid Margin="0,35,0,0">
    <TextBlock Text="I should be visible!" />
</Grid>

我是偶然發現的。

如果其他任何人也遇到類似問題。

以下代碼是錯誤的:

            <ContentPresenter Content="{Binding RelativeSource={RelativeSource FindAncestor,
                                                                           AncestorType=Window},
                                            Path=Content}" />

正確的代碼只是一個空的ContentPresenter或ChromeWindow的MSDN示例:

<ContentPresenter Content="{TemplateBinding Content}" />

沒有綁定似乎也很好。 另外,在我上面的原始代碼中,我可能應該將內容演示者作為網格中的第一項,以使內容不會模糊標題欄。 我原來在那里,但我將其移至最后一個條目,以查看是否僅看到標題欄的原因是因為z順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM