简体   繁体   中英

Window Style and using controls (C# and WPF)

I'm using Visual Studio 2013 to create my applications. I started to learn programming 4 years ago, and now a days I wanna to improve my programmer experience.

In the last days I had to create a simply application, but I didn't want to use a default style of a window. So I read that I could create a style for my window. I did it, it works fine, but there's a little problem: I can't put anything inside my window now... All the controls I put into a grid, into the window tag, aren't visible, but the compiler doesn't show any problems.

Here is the Style Code, then my Window XAML

<Style TargetType="Window" x:Key="DefaultWindow">
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">                       
                    <Border BorderThickness="1" BorderBrush="Black">
                        <Border BorderThickness="3" BorderBrush="#FF244E97">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="50"></RowDefinition>
                                    <RowDefinition></RowDefinition>
                                </Grid.RowDefinitions>

                                <Grid Grid.Row="0" Background="#FF244E97">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition></ColumnDefinition>
                                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>

                                    <Viewbox Grid.Column="0" Grid.Row="0" Stretch="Uniform" HorizontalAlignment="Left" Margin="10">
                                        <TextBlock FontFamily="Corbel" Background="#FF244E97" Foreground="White">Costo Unitario</TextBlock>
                                    </Viewbox>

                                    <Grid Grid.Column="1">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition></ColumnDefinition>
                                            <ColumnDefinition></ColumnDefinition>
                                            <ColumnDefinition></ColumnDefinition>
                                        </Grid.ColumnDefinitions>

                                        <Viewbox Grid.Column="0" Stretch="Uniform" HorizontalAlignment="Left" Margin="2, 10, 2, 10">
                                            <TextBlock FontFamily="Corbel" Background="#FF244E97" Foreground="White">_</TextBlock>
                                        </Viewbox>

                                        <Viewbox Grid.Column="1" Stretch="Uniform" HorizontalAlignment="Left" Margin="2, 10, 2, 10">
                                            <TextBlock FontFamily="Corbel" Background="#FF244E97" Foreground="White">◘</TextBlock>
                                        </Viewbox>

                                        <Viewbox Grid.Column="2" Stretch="Uniform" HorizontalAlignment="Left" Margin="2, 10, 20, 10">
                                            <TextBlock FontFamily="Corbel" Background="#FF244E97" Foreground="White">X</TextBlock>
                                        </Viewbox>

                                    </Grid>

                                </Grid>

                                <Grid x:Name="ContenentGrid" Grid.Row="1" Background="White">
                                    <ResizeGrip Width="10" Height="10" Grid.Column="1" VerticalAlignment="Bottom"/>
                                </Grid>

                            </Grid>                                                             
                        </Border>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And my Windows Xaml:

<Window x:Class="Pabich.Marcin._5HI.CostoUnitario.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:chrt="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="MainWindow" Height="600" Width="800" WindowStartupLocation="CenterScreen"
    Style="{StaticResource DefaultWindow}">

<Grid>
    <TextBlock>Hello!</TextBlock>
</Grid>

This should be a no-border window, within I can put controls, in the central grid. This is a screen of the window for now: Screen of the Window

As you can see, I leaved all the white space for my controls, but they aren't there. So how can I make it?

PS: Keep in mind that I'm only a beginner, and I wanna to use simply code

I think that's happens because you don't use ContentPresenter to display window content. Without ContentPresenter WPF content model doesn't know where content should be placed and doesn't show it.

Try to add the ContentPresenter to your ContentGrid (WPF window also requires it in the AdornerDecorator).

<Grid x:Name="ContenentGrid" Grid.Row="1" Background="White">
    <AdornerDecorator>
       <ContentPresenter Content="{TemplateBinding Content}" />
    </AdornerDecorator>
    ... ResizeGrip here ...
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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