简体   繁体   中英

xaml(wpf) How to address a control from my customwindow template?

I've created a customwindow template by redefining it, and inheriting from window. This template resides in a separate class library, and i build a dll and reference it from my main project.
This is a part of code from custom window xaml:

        <!-- Window style -->
        <Style TargetType="{x:Type local:CustomWindow}">
            <Setter Property="WindowStyle" Value="None"/>
            <Setter Property="ResizeMode" Value="NoResize"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="AllowsTransparency" Value="True"/>
            <Setter Property="Opacity" Value="1" />
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="BorderBrush" Value="Silver"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomWindow}">
                        <Border BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}">
                            <Grid>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Rectangle x:Name="moveRectangle" Fill="#24282A"
                                            Grid.Row="0" Grid.Column="1"/>
                                    <Label x:Name="WindowName" Background="#24282A" Foreground="White" Grid.Row="0" Grid.Column="0"/>
                                    <StackPanel Grid.Row="0" Grid.Column="2" Orientation="Horizontal" Background="#24282A">
                                        <Button x:Name="minimizeButton" Style="{StaticResource WindowButtonStyle}"
                                                Content="0" />
                                        <Button x:Name="restoreButton" Style="{StaticResource WindowButtonStyle}"
                                                Content="1" />
                                        <Button x:Name="closeButton" Style="{StaticResource WindowButtonStyle}"
                                                Content="r" />
                                    </StackPanel>
                                    <Grid Background="#24282A" Opacity="0.9"
                                            Grid.Row="1" Grid.ColumnSpan="3" Margin="5,0,5,5">
                                        <AdornerDecorator>
                                            <ContentPresenter/>
                                        </AdornerDecorator>
                                    </Grid>
                                </Grid>
                                <Grid x:Name="resizeGrid">
                                                        <Rectangle
                                    Stroke="{x:Null}"
                                    Fill="#24282A"
                                    VerticalAlignment="Top"
                                    Height="5"
                                    x:Name="top"
                                    Margin="5,0,5,0" />
                                                        <Rectangle
                                    Stroke="{x:Null}"
                                    Fill="#24282A"
                                    x:Name="bottom"
                                    Height="5"
                                    VerticalAlignment="Bottom"
                                    Margin="5,0,5,0" />
                                                        <Rectangle
                                    Stroke="{x:Null}"
                                    Fill="#24282A"
                                    HorizontalAlignment="Left"
                                    Margin="0,5,0,5"
                                    Width="5"
                                    x:Name="left"/>
                                                        <Rectangle
                                    Stroke="{x:Null}"
                                    Fill="#24282A"
                                    Margin="0,5,0,5"
                                    Width="5"
                                    HorizontalAlignment="Right"
                                    x:Name="right" />
                                                        <Rectangle
                                    Stroke="{x:Null}"
                                    Fill="#24282A"
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Bottom"
                                    Width="5"
                                    Height="5"
                                    x:Name="bottomLeft" />
                                                        <Rectangle
                                    Stroke="{x:Null}"
                                    Fill="#24282A"
                                    VerticalAlignment="Bottom"
                                    Height="5"
                                    Width="5"
                                    HorizontalAlignment="Right"
                                    x:Name="bottomRight" />
                                                        <Rectangle
                                    Stroke="{x:Null}"
                                    Fill="#24282A"
                                    HorizontalAlignment="Right"
                                    Width="5"
                                    Height="5"
                                    VerticalAlignment="Top"
                                    x:Name="topRight" />
                                                    <Rectangle
                                    Stroke="{x:Null}"
                                    Fill="#24282A"
                                    HorizontalAlignment="Left"
                                    Width="6"
                                    VerticalAlignment="Top"
                                    Height="5"
                                    x:Name="topLeft" />
                                </Grid>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

As you can see, there is a label called "WindowName". i want this label to be kind of Title bar in my custom window, and i wanna call it's property content from my main wpf application which inherits from this custom window. Everything works ok, except i have no idea how i should call this label to set it's content. Any help would be appreciated

You would want to bind the content of that Label to the Title property of the base Window class since the base class already has a dependency property that you can just reuse. All you will need to do is update the xaml for you label component as below:

<Label x:Name="WindowName" Content={TemplateBinding Title} Background="#24282A" Foreground="White" Grid.Row="0" Grid.Column="0"/>

You could also override OnApplyTemplate in your CustomWindow and use a method like FindName to get the Label using its name and then updating it through a direct reference, but the binding way is much cleaner so I wouldn't consider that route although I wanted to at least mention it.

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