繁体   English   中英

WPF控件模板应用于窗口,以及如何添加控件

[英]wpf controltemplate is applied to window,and how to add controls

我已经定义了一个用于Windows的控件模板,然后Windows具有与基本相同的视图。

之后,我应该在不同的窗口中添加不同的控件。 而且我不知道如何在控件模板中添加元素的宿主以容纳控件的不同部分,例如下面的图片。

还有一件事,如何访问控件以及如何在不同窗口的控件模板中设置按钮的动作? 应该使用windowBase类来做到这一点吗?

首先。屏幕图片 在此处输入图片说明

和控制模板文件


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

我自己做了。 只需使用以下代码来托管新控件

<AdornerDecorator>
     <contentpresenter/>
</AdornerDecorator>

并创建一个windowBase作为新窗口的父类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM