2017-11-12 20 views
0

私はウィンドウに使用されているコントロールテンプレートを定義しており、ウィンドウは基本的に同じビューを持っています。

その後、別のコントロールを別のウィンドウに追加する必要があります。私はどのように制御のさまざまな部分を保持するために、コントロールのテンプレートの要素のホストを追加するのか分かりません。

ともう1つ、コントロールにアクセスする方法と、異なるウィンドウのcontroltemplateでボタンのアクションを設定する方法は?それを行うには、windowBaseクラスを使用する必要がありますか?wpf controltemplateがウィンドウに適用され、コントロールを追加する方法

第【選択画面画像 enter image description here

とコントロールテンプレートファイル


<ImageSource x:Key="BtnCloseNormal">../images/others/popup_btn_reduction_normal.png</ImageSource> 
<ImageSource x:Key="BtnCloseMouseOver">../images/others/popup_btn_reduction_mouseover.png</ImageSource> 
<ImageSource x:Key="BtnClosePressed">../images/others/popup_btn_reduction_selected.png</ImageSource> 


<Style x:Key="StatedButtonStyle" TargetType="{x:Type c:StatedButton}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type c:StatedButton}" > 
       <Grid> 
        <Border> 
         <Image x:Name ="btnImg" Source="{Binding NormalBackground ,RelativeSource={RelativeSource TemplatedParent}}" Stretch="Fill"/> 
        </Border> 
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter TargetName="btnImg" Property="Source" Value="{Binding MouseOverBackground ,RelativeSource={RelativeSource TemplatedParent}}"/> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="True"> 
         <Setter TargetName="btnImg" Property="Source" Value="{Binding PressedBackground ,RelativeSource={RelativeSource TemplatedParent}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 


<Style x:Key="WindowBaseStyle" TargetType="{x:Type Window}"> 
    <Setter Property="Background" Value="{x:Null}"></Setter> 
    <Setter Property="AllowsTransparency" Value="True"></Setter> 
    <Setter Property="WindowStyle" Value="None"></Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Window}"> 
       <Grid Background="{x:Null}"> 
        <Border CornerRadius="10" Background="White" Margin="20"> 
         <Border.Effect> 
          <DropShadowEffect BlurRadius="10" Color="Black" Direction="270" ShadowDepth="5" RenderingBias="Quality" Opacity="0.6"></DropShadowEffect> 
         </Border.Effect> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="40"/> 
           <RowDefinition Height="10"/> 
           <RowDefinition/> 
           <RowDefinition Height="40"/> 
           <RowDefinition Height="20"/> 
          </Grid.RowDefinitions> 
          <Grid Grid.Row="0"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="2*"/> 
            <ColumnDefinition Width="*"/> 
            <ColumnDefinition Width="40"/> 
            <ColumnDefinition Width="40"/> 
            <ColumnDefinition Width="60"/> 
           </Grid.ColumnDefinitions> 
           <Label x:Name="lblTitle" Grid.Column="0" Margin="20,6,0,0" FontSize="18" FontWeight="Bold" 
             Content="{TemplateBinding Title}"> 
           </Label> 
           <c:StatedButton Grid.Column="2" Width="30" Height="30" Style="{StaticResource StatedButtonStyle}" 
                NormalBackground="{StaticResource BtnCloseNormal}" 
                MouseOverBackground="{DynamicResource BtnCloseMouseOver}" 
                PressedBackground="{StaticResource BtnClosePressed}"/> 
           <c:StatedButton Grid.Column="3" Width="30" Height="30" Style="{StaticResource StatedButtonStyle}" 
                NormalBackground="{StaticResource BtnCloseNormal}" 
                MouseOverBackground="{DynamicResource BtnCloseMouseOver}" 
                PressedBackground="{StaticResource BtnClosePressed}"/> 
           <c:StatedButton Grid.Column="4" Width="30" Height="30" Style="{StaticResource StatedButtonStyle}" 
                NormalBackground="{StaticResource BtnCloseNormal}" 
                MouseOverBackground="{DynamicResource BtnCloseMouseOver}" 
                PressedBackground="{StaticResource BtnClosePressed}"/> 
          </Grid> 
          <Separator Background="LightGray" Grid.Row="1" Height="2"></Separator> 
          <Grid Grid.Row="2"> 
           <!--some controls will be insert here--> 
          </Grid> 
          <Grid Grid.Row="3" Background="Red" > 
           <!--some controls will be insert here--> 
           <ContentControl Content="{Binding }"></ContentControl> 
          </Grid> 
         </Grid> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

とwindow.xaml


<Window x:Class="WpfApp1.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:WpfApp1" 
    xmlns:uctrls="clr-namespace:WpfApp1.Uctrls" 
    mc:Ignorable="d" 
    Style="{StaticResource WindowBaseStyle}" 
    Title="Window1 -- title" Height="300" Width="500"> 
<Grid> 
    <Label x:Name="lblTitle" Content="title"></Label> 
    <Button x:Name="BtnTest" Content="Get TextBox1" Click="BtnTestClick"></Button> 
    <uctrls:UserControlBase> 
     <Grid> 
      <Button Content="what "></Button> 
     </Grid> 
    </uctrls:UserControlBase> 
</Grid> 

答えて

0

私はそれを自分で行っています。 はちょうど新しいコントロール

<AdornerDecorator> 
    <contentpresenter/> 
</AdornerDecorator> 

をホストし、新しいウィンドウの親クラスとしてwindowBaseを作成するには以下のコードを使用します。

関連する問題