繁体   English   中英

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

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

我通过重新定义并从 window 继承来创建了一个 customwindow 模板。 这个模板驻留在一个单独的类库中,我构建了一个 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 应用程序中调用它的属性内容,它继承自这个自定义窗口。 一切正常,除了我不知道我应该如何调用这个标签来设置它的内容。 任何帮助,将不胜感激

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

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

您还可以在CustomWindow覆盖OnApplyTemplate并使用类似FindName的方法使用其名称获取Label ,然后通过直接引用更新它,但绑定方式更清晰,因此我不会考虑该路线,尽管我想在至少提一下。

暂无
暂无

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

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