user1814688
user1814688

Reputation:

wpf controltemplate is applied to window,and how to add controls

i have defined a controltemplate that is used to windows ,then windows have the same view as basically.

after that ,i should add different controls in different windows. and i have no idea how to add the elements' host in controltemplate to hold different parts of controls which likes the picture bellow.

and one more thing, how to access the controls and how to set the buttons' actions in controltemplate of different window? it should be use a windowBase class to do that ?

first .the screen picture enter image description here

and the control template file


<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>

and the 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>

Upvotes: 0

Views: 730

Answers (1)

user1814688
user1814688

Reputation:

i have done it myself. just use code bellow to host new controls

<AdornerDecorator>
     <contentpresenter/>
</AdornerDecorator>

and create a windowBase as parent class of new windows.

Upvotes: 1

Related Questions