2014-01-12 87 views
0

我想根據當前選定的主題更改Windows Phone應用程序中邊框的顏色。我寫了以下內容,但不起作用。無論選擇的主題如何,它都是白色的邊框。使用C#Windows Phone 7.1更改Border.BorderBrush值

這是用於應用程序(contacts.xaml)的xaml代碼。

[XAML代碼]

 <StackPanel x:Name="StackPost_Viewer"> 
      <Border BorderBrush="White" BorderThickness="3" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="436" Tap="Show_ContactList_gsecs"> 
       <TextBlock TextWrapping="Wrap" Text="General Secretaries Social and Cultural" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      </Border> 
      <Border BorderBrush="White" BorderThickness="3" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="436" Tap="Show_ContactList_publicity"> 
       <TextBlock TextWrapping="Wrap" Text="Publicity" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      </Border> 
      <Border BorderBrush="White" BorderThickness="3" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="436" Tap="Show_ContactList_events"> 
       <TextBlock TextWrapping="Wrap" Text="Events" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      </Border> 
      <Border BorderBrush="White" BorderThickness="3" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="436" Tap="Show_ContactList_spons"> 
       <TextBlock TextWrapping="Wrap" Text="Marketing and Sponsorship" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      </Border> 
      <Border BorderBrush="White" BorderThickness="3" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="436" Tap="Show_ContactList_finance"> 
       <TextBlock TextWrapping="Wrap" Text="Finance" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
      </Border> 
     </StackPanel> 

相應的C#代碼(contacts.xaml.cs)

[C#代碼]

public contacts() 
    { 
     InitializeComponent(); 
     if (!isDarkTheme()) 
     { 
      StackPanel main_stack = StackContact_Viewer; 
      var borders = main_stack.Children.OfType<Border>(); 
      foreach (Border each in borders) 
      { 
       each.BorderBrush = new SolidColorBrush(Colors.Black); 
      } 
     } 
     else 
     { 
      StackPanel main_stack = StackContact_Viewer; 
      var borders = main_stack.Children.OfType<Border>(); 
      foreach (var each in borders) 
      { 
       each.BorderBrush = new SolidColorBrush(Colors.White); 
      } 

     } 

    } 
    private bool isDarkTheme() 
    { 
     return Visibility.Visible == (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"]; 
    } 

回答

1

如果你只是想你的邊框顏色爲黑色對於黑暗主題和白色對於淺色主題,則可以簡單地將邊框元素定義爲

<Border BorderBrush="{StaticResource PhoneBackgroundBrush}" ... 

您不需要在構造函數中編寫任何代碼來更改顏色。

+0

其實我需要的光的主題,反之亦然反向黑色邊框。所以PhoneBackgroundBrush在這裏沒有任何幫助。告訴我一些其他的方法。 – bytestorm

+0

哦,順便說一句PhoneForegroundBrush做的工作,感謝您的想法@Kanishk – bytestorm

0

試試這個

XAML

<Border Name="border" BorderThickness="3"> 
    <TextBlock Text="General Secretaries Social and Cultural" /> 
</Border> 

C#

border.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red); 
相關問題