[C#][VB.NET][Windows Phone] 以程式方式變更PivotItem的Foreground

摘要:[C#][VB.NET][Windows Phone] 以程式方式變更PivotItem的Foreground

假設我們有一個 PhonePage 他的XAML長這樣:

<phone:PhoneApplicationPage 
    x:Class="ChangePivotItemForeground.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True" xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls">

    <!--LayoutRoot 是放置所有頁面的根資料格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <controls:Pivot  HorizontalAlignment="Left" Margin="0,0,0,0" Name="pivot1" Title="pivot" VerticalAlignment="Top" Width="480" Height="800" Foreground="SkyBlue">
            <controls:PivotItem Header="item1">
                <Grid />
            </controls:PivotItem>
            <controls:PivotItem Header="item2">
                <Grid />
            </controls:PivotItem>
        </controls:Pivot>
    </Grid>
</phone:PhoneApplicationPage>

執行的畫面會是像這樣:


首先 修改XAML 檔 將pivotitem heaer 另外處理 並且再其中放入一個TextBlock,並指定Name屬性, 像這樣:

<phone:PhoneApplicationPage 
    x:Class="ChangePivotItemForeground.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True" xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls">

    <!--LayoutRoot 是放置所有頁面的根資料格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <controls:Pivot  HorizontalAlignment="Left" Margin="0,0,0,0" Name="pivot1" Title="pivot" VerticalAlignment="Top" Width="480" Height="800" Foreground="SkyBlue">
            <controls:PivotItem>
            <!-- 設定 PivotItem Header -->
                <controls:PivotItem.Header>
                    <TextBlock Text="item1" Name="pivotItem1"></TextBlock>
                </controls:PivotItem.Header>
                <Grid />
            </controls:PivotItem>
            <controls:PivotItem>
                <controls:PivotItem.Header>
                    <TextBlock Text="item2" Name="pivotItem2"></TextBlock>
                </controls:PivotItem.Header>
                <Grid />
            </controls:PivotItem>
        </controls:Pivot>
    </Grid>
</phone:PhoneApplicationPage>

 

接下來我們只要在.cs 或.vb中針對TextBlock 處理就可以了 (包括事件的處理喔), 像這樣:

 

C#:

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Brush SolidLimeBrush = new SolidColorBrush(Color.FromArgb(0xFF,0x00,0xFF,0x00));
            pivotItem1.Foreground = SolidLimeBrush;
            pivotItem2.Foreground = SolidLimeBrush;
        }

VB .Net :

    Private Sub PhoneApplicationPage_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Dim SolidLimeBrush As New SolidColorBrush(Color.FromArgb(255, 0, 255, 0))
        pivotItem1.Foreground = SolidLimeBrush
        pivotItem2.Foreground = SolidLimeBrush
    End Sub

執行畫面就像這樣:

 

C# SAMPLE

VB SAMPLE

 

 


[後記]

感謝 Nobel 提供的參考資料:

1. WP7 PivotItem doesn't accept foreground if Pivot has set foreground
2. Alter a Pivot controls header template the easy way (WP7)