简体   繁体   中英

Change the Border.BorderBrush value with C# Windows Phone 7.1

I want to change the color of borders in a windows phone app depending on the currently selected theme. I have wrote the following but its not working. It is always white color border irrespective of the theme selected.

Here is the xaml code used for the app(contacts.xaml).

[XAML CODE]

       <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>

Corresponding c# code(contacts.xaml.cs)

[C# CODE]

    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"];
    }

If you just want your Border color to be Black for Dark theme and White for Light theme then you can simply define your border elements as

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

You don't need to write any code in your constructor to change the color then.

try this one

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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