2017-06-10 13 views
0

Wpfウィンドウ用のControlTemplateを作成した後、デザインビューで正常に動作しています。しかし、私が走ったとき、外側の赤いボーダーは表示されません。wpf実行時にコントロールテンプレートが動作しない

は、ここに私のコード

<Window x:Class="MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow" Height="350" Width="525" 
WindowStyle="None" 
AllowsTransparency="True" 
WindowStartupLocation="CenterScreen" 
> 
<Window.Resources> 
    <Style TargetType="Window"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Window"> 

        <Border Padding="20" Background="red"> 
         <ContentPresenter Content="{TemplateBinding Content}" /> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<WindowChrome.WindowChrome> 
    <WindowChrome 
     ResizeBorderThickness="10" 
     CaptionHeight="40" 
     CornerRadius="0" 
     GlassFrameThickness="0" 
     /> 
</WindowChrome.WindowChrome> 

<Grid> 
    <Border Background="Black" Padding="20"> 
     <Button Content="ok"/> 
    </Border> 
</Grid> 
</Window> 

enter image description here

私はそれを実行したときに表示されていない外側の赤い境界線です。何か間違いがあれば教えてもらえますか?

答えて

0

実行時に、ウィンドウのタイプはで、Windowではなく、スタイルは適用されません。

あなたがスタイルのTargetTypeメインウィンドウへの変更可能性があります

<Window xmlns:local="clr-namespace:YourNamespace" ...> 
    <Window.Resources> 
     <Style TargetType="local:MainWindow"> 
      ... 
     </Style> 
    </Window.Resources> 
    ... 
</Window> 

か、直接ウィンドウのStyleプロパティを設定します。

<Window ...> 
    <Window.Style> 
     <Style TargetType="Window"> 
      ... 
     </Style> 
    </Window.Style> 
    ... 
</Window> 

、または直接だけTemplateプロパティを設定する:

<Window ...> 
    <Window.Template> 
     <ControlTemplate TargetType="Window"> 
      <Border Padding="20" Background="red"> 
       <ContentPresenter Content="{TemplateBinding Content}" /> 
      </Border> 
     </ControlTemplate> 
    </Window.Template> 
    ... 
</Window> 
+0

ありがとう、ありがとう。今私はあなたの簡単な例から理解しています。 –

0

ちょうど小さなものを変えること私のためにうまくいった。

<Window.Resources> 
     <Style TargetType="local:MainWindow"> 
      <Setter Property="Template"> 
       ... 
     </Style> 
</Window.Resources> 
+0

xmlns:local = "clr-namespace:YourNamespace"をウィンドウセクションに追加する必要があるため、local:mainwindowだけで作業しません。 –

+0

はい、この名前空間を追加する必要があります。私の場合、xmlns:local = "clr-namespace:WpfApplication1"です。 –

関連する問題