- WPF

2017-11-06 16 views
0

は、どのように私は、ウィンドウのオブジェクトを作成し、ウィンドウのすべてのようにそれを使用することができ、私は私のプロジェクトに4ウィンドウを持っていると私は特定の閉じるボタンを提供しようとすると、1つのタイトル- WPF

考えてみましょうパターン。ここで

たちは、パターン・ウィンドウのために持っているものの例である:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     WindowStyle="None" AllowsTransparency="True" > 
<Grid> 
<Button Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/> 
<TextBlock VerticalAlignment="Top" HorizontalAlignment="Center" X:Name="WindowTitle/> 
</Grid> 
</Window> 

にはどうすればパターンとして私の窓のすべてのために使用することができます。ありがとう

+0

これはあなたが探しているものですか? https://stackoverflow.com/questions/7174315/understanding-wpf-deriving-window-class – Aaron

+0

@Aaronそれは大丈夫です、ありがとう、しかし、私はどのようにパターン上に特定のボタンを持つべきですか? –

+0

基本クラスのボタンのテキストと機能を、基本クラスの1つのプロパティ(または複数のプロパティ)にバインドしている可能性があります。次に、子クラスでそのプロパティを設定すると、 "OnPropertyChanged"通知はボタンに対して何をしてもかまいません(別のハンドラやバインディングを追加したり、不要なハンドラやバインディングを削除したり、テキストを変更したりします)あなたがベースウィンドウを作ったのと同じ方法で「ベースボタン」を作成して、それを使って完全にナットすることができます。 – Aaron

答えて

0

実際には、親ウィンドウを書く必要はありません。代わりにStyleTemplateを使用できます。より便利で、Microsoft WPF Teamが推奨します。

enter image description here

あなたApp.xamlに以下のコードを書いて、あなたは上の画像を取得します:

<Application x:Class="Walterlv.Demo.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      .........> 
    <Application.Resources> 
     <Style x:Key="Style.Window.Default" TargetType="Window"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Window"> 
         <Grid Background="{TemplateBinding Background}"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="40"/> 
           <RowDefinition/> 
          </Grid.RowDefinitions> 
          <Button Grid.Row="0" Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/> 
          <TextBlock Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Center" 
             Text="{TemplateBinding Title}"/> 
          <Border Grid.Row="1" BorderThickness="{TemplateBinding BorderThickness}" 
            BorderBrush="{TemplateBinding BorderBrush}"> 
           <!-- This is the container to host your Window.Content --> 
           <ContentPresenter/> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Application.Resources> 
</Application> 

そして、あなたは、このような「パターン」を共有する唯一のプロパティStyleを使用することができます。

<Window x:Class="Walterlv.Demo.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Style="{StaticResource Style.Window.Default}"> 

</Window> 

App.xamlファイルにさまざまな種類のスタイルを定義して、内の誰かを選択することができますが必要です。

+0

それは素晴らしいです、それは私が必要なもの、ありがとう –