xaml(wpf)如何从我的自定义窗口模板中处理控件?

时间:2016-05-12 21:55:29

标签: c# wpf xaml

我通过重新定义并从窗口继承来创建了一个自定义窗口模板。这个模板位于一个单独的类库中,我构建了一个dll并从我的主项目中引用它 这是自定义窗口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>

如您所见,有一个名为“WindowName”的标签。我希望这个标签在我的自定义窗口中是一种标题栏,我想从我的主要wpf应用程序中调用它的属性内容,该应用程序继承自此自定义窗口。一切正常,除了我不知道如何调用这个标签来设置它的内容。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您希望将Label的内容绑定到基类Title类的Window属性,因为基类已经具有您可以重用的依赖项属性。您需要做的就是更新标签组件的xaml,如下所示:

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

您还可以覆盖OnApplyTemplate中的CustomWindow,并使用FindName等方法获取Label使用其名称,然后通过直接引用进行更新,绑定方式更清晰,所以我不会考虑这条路线,尽管我想至少提一下。