ControlTemplate代码中的访问控制

时间:2014-05-08 21:17:31

标签: wpf vb.net controls controltemplate

我写了一个包含Window的模板的类。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:ClassAccounts">

    <SolidColorBrush x:Key="CustomWhite" Color="White"/>
    <SolidColorBrush x:Key="CustomDarkBlue" Color="#8DA3C1"/>
    <SolidColorBrush x:Key="CustomGray" Color="LightGray"/>
    <SolidColorBrush x:Key="CustomDarkHighlight" Color="#5082A4"/>
    <!-- Submenu item highlight -->
    <SolidColorBrush x:Key="CustomLightHighlight" Color="#3399FF"/>
    <!-- TreeviewItem highlight -->
    <Color x:Key="CustomLightHighlightC" A="#FF" R="#33" G="#99" B="#FF"/>
    <SolidColorBrush x:Key="CustomBackground" Color="#B5CBEF"/>
    <Color x:Key="CustomBackgroundC" A="#FF" R="#B5" G="#CB" B="#EF"/>
    <SolidColorBrush x:Key="CustomHighlightedControlGlyph" Color="#3C7FB1"/>
    <SolidColorBrush x:Key="CustomHighlightedControlBorder" Color="#222"/>
    <SolidColorBrush x:Key="CustomPressedControlGlyph" Color="#003366"/>
    <SolidColorBrush x:Key="CustomPressedControlBorder" Color="#526C7B"/>
    <SolidColorBrush x:Key="CustomPressedControlBackground" Color="#595959"/>
    <SolidColorBrush x:Key="CustomGlyph" Color="#444"/>

    <!-- Window Colors -->
    <SolidColorBrush x:Key="CustomWindowBorder" Color="#395984"/>
    <SolidColorBrush x:Key="CustomHeaderLight" Color="#E7EBF7"/>
    <Color x:Key="CustomHeaderLightC" A="#FF" R="#E7" G="#EB" B="#F7"/>
    <SolidColorBrush x:Key="CustomHeaderDark" Color="#CEE3FF"/>
    <Color x:Key="CustomHeaderDarkC" A="#FF" R="#CE" G="#E3" B="#FF"/>
    <SolidColorBrush x:Key="CustomControl" Color="Gray"/>

    <!-- Window style -->
    <Style TargetType="{x:Type local:CustomWindow}">
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="ResizeMode" Value="NoResize"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="ShowInTaskbar" Value="True"/>
        <Setter Property="Icon" Value="coins.ico"/>        
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomWindow}">
                    <Border Width="Auto" Height="Auto" Name="windowFrame" BorderBrush="{StaticResource CustomWindowBorder}" BorderThickness="1" CornerRadius="0,20,20,20">
                        <Border.Background>
                            <LinearGradientBrush>
                                <GradientBrush.GradientStops>
                                    <GradientStopCollection>
                                        <GradientStop Color="{StaticResource CustomHeaderLightC}" Offset="0.0"></GradientStop>
                                        <GradientStop Color="{StaticResource CustomHeaderDarkC}" Offset="0.25"></GradientStop>
                                    </GradientStopCollection>
                                </GradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </Border.Background>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="100"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" x:Name="tbTitle" Text="{TemplateBinding Title}" FontSize="14" Margin="0" Padding="4"/>
                                <StackPanel Grid.Column="1" Orientation="Horizontal" Margin="0,5,15,0" HorizontalAlignment="Right" VerticalAlignment="Top" Background="{StaticResource CustomHeaderDark}">
                                    <TextBlock x:Name="RefreshButton" Text="q" FontFamily="Webdings" FontSize="16" Foreground="{StaticResource CustomControl}" Margin="0" HorizontalAlignment="Right" VerticalAlignment="Top"/>
                                    <TextBlock x:Name="MinimizeButton" Text="0" FontFamily="Webdings" FontSize="16" Foreground="{StaticResource CustomControl}" Margin="0" HorizontalAlignment="Right" VerticalAlignment="Top"/>
                                    <TextBlock x:Name="OKButton" Text="a" FontFamily="Webdings" FontSize="16" Foreground="{StaticResource CustomControl}" Margin="0" HorizontalAlignment="Right" VerticalAlignment="Top"/>
                                    <TextBlock x:Name="CloseButton" Text="r" FontFamily="Webdings" FontSize="16" Foreground="{StaticResource CustomControl}" Margin="0" HorizontalAlignment="Right" VerticalAlignment="Top"/>
                                </StackPanel>
                            </Grid>
                            <Grid x:Name="grdMain" Grid.Row="1" Background="{StaticResource CustomBackground}">
                                <AdornerDecorator>
                                    <ContentPresenter/>
                                </AdornerDecorator>
                            </Grid>
                            <Grid Grid.Row="2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="100"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="150"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" x:Name="tbStatusLeft" FontSize="14" Margin="15,0,0,0" Padding="2"/>
                                <TextBlock Grid.Column="1" x:Name="tbStatusMiddle" FontSize="14" Margin="0,0,0,0" Padding="2" TextAlignment="Center"/>
                                <TextBlock Grid.Column="2" x:Name="tbStatusRight" FontSize="14" Margin="0,0,15,0" Padding="2" TextAlignment="Right"/>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

然后我(在另一个项目中)像这样引用它:

<control:CustomWindow x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:control="clr-namespace:ClassAccounts;assembly=ClassAccounts"
        Title="Test" Height="350" Width="525" WindowStartupLocation="CenterScreen">
    <Grid>
        <TextBlock Text="Here goes the content!"/>
    </Grid>
</control:CustomWindow>

在这个项目中,我有以下代码

Partial Public Class MainWindow
    Inherits ClassAccounts.CustomWindow

    Public Sub New()

        InitializeComponent()

    End Sub

    Private Sub MainWindow_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized

        UpdateStatus("Test")

    End Sub

End Class

UpdateStatus可以在Template

的代码中找到
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Input
Imports System.Windows.Shapes

Public Class CustomWindow

    Inherits Window

    Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(CustomWindow), New FrameworkPropertyMetadata(GetType(CustomWindow)))
    End Sub

    Public Overrides Sub OnApplyTemplate()
        Dim minimizeButton As TextBlock = TryCast(GetTemplateChild("MinimizeButton"), TextBlock)
        If minimizeButton IsNot Nothing Then
            AddHandler minimizeButton.MouseLeftButtonUp, AddressOf MinimizeClick
        End If

        Dim closeButton As TextBlock = TryCast(GetTemplateChild("CloseButton"), TextBlock)
        If closeButton IsNot Nothing Then
            AddHandler closeButton.MouseLeftButtonUp, AddressOf CloseClick
        End If

        Dim title As TextBlock = TryCast(GetTemplateChild("tbTitle"), TextBlock)
        If title IsNot Nothing Then
            AddHandler title.MouseDown, AddressOf moveRectangle_PreviewMouseDown
        End If

        MyBase.OnApplyTemplate()
    End Sub

    Protected Sub MinimizeClick(sender As Object, e As RoutedEventArgs)
        WindowState = WindowState.Minimized
    End Sub

    Protected Sub RestoreClick(sender As Object, e As RoutedEventArgs)
        WindowState = If((WindowState = WindowState.Normal), WindowState.Maximized, WindowState.Normal)
    End Sub

    Protected Sub CloseClick(sender As Object, e As RoutedEventArgs)
        Close()
    End Sub

    Private Sub moveRectangle_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
        If Mouse.LeftButton = MouseButtonState.Pressed Then
            DragMove()
        End If
    End Sub

    Public Sub UpdateStatus(s As String)
        Dim t As TextBlock = TryCast(Me.GetTemplateChild("tbStatusLeft"), TextBlock)
        Dim u As TextBlock = TryCast(Me.Template.FindName("tbStatusLeft", Me), TextBlock)

        If t IsNot Nothing Then
            t.Text = s
        End If
    End Sub

End Class

在这个例子中,t和u都是Nothing。我怎样才能引用tbStatusLeft TextBlock? (我使用Initialized而不是Loaded,因为还有其他一些只能运行一次的事情)

由于 安迪

0 个答案:

没有答案