locked
Change System Brush with Code?

    Question

  • I know I can change the System Brush in StandardStyles.xaml.

    I wonder can I change it with code?

    For example, I want to change ListBoxItemSelectedPointerOverBackgroundThemeBrush brush dynamicly. 

    Monday, January 28, 2013 1:29 PM

Answers

  • An alternative to Can's Page level proposal consists in changing it at the Application level. The drawback is that one needs to navigate from/into in order to get the wanted effect on _all_ selected items of the current page. Otherwise it affects only the newly selected items.

    ---

    <Application
        x:Class="App1.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                   <ResourceDictionary Source="Common/StandardStyles.xaml"/>
                    <ResourceDictionary>
                        <SolidColorBrush x:Key="ListBoxItemSelectedPointerOverBackgroundThemeBrush" Color="Red"></SolidColorBrush>
                    </ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>

    ---

    <Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <ListBox VerticalAlignment="Top" FontSize="24" SelectionMode="Multiple">
                <ListBoxItem Content="Item 1" IsSelected="True"/>
                <ListBoxItem Content="Item 2" IsSelected="False"/>
                <ListBoxItem Content="Item 3" IsSelected="False"/>
                <ListBoxItem Content="Item 4" IsSelected="False"/>
            </ListBox>
            <Button Content="Go" Click="Button_Click_1" x:Name="button" VerticalAlignment="Bottom" />
        </Grid>
    </Page>

    -

    using Windows.UI;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    namespace App1
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                base.OnNavigatedFrom(e);
            }
            static bool flipFlop;
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                Application.Current.Resources["ListBoxItemSelectedPointerOverBackgroundThemeBrush"] =
                    flipFlop ? new SolidColorBrush(Colors.Olive) : new SolidColorBrush(Colors.Orange);
                flipFlop = !flipFlop;
                // Possibly you want to navigate away to refresh the color 
                // Try with the following commented or not:
                this.Frame.Navigate(typeof(MainPage));
                // If commented out, then navigating to the new page will set the new color on all selected items
                // If commented,then staying on the current page will set the new color on all newly selected items
            }
        }
    }
    


    • Marked as answer by sscd Tuesday, January 29, 2013 1:17 AM
    Monday, January 28, 2013 4:34 PM

All replies

  • I don't think you can change the static resources in the runtime. If you can explain the scenario a bit, probably we can come up with a solution.

    For instance, if this is actually for using a different theme brush on different pages (not application wide), you can add a page resource with the same key to override the existing resource.


    Can Bilgin
    Blog Samples CompuSight

    Monday, January 28, 2013 3:56 PM
  • An alternative to Can's Page level proposal consists in changing it at the Application level. The drawback is that one needs to navigate from/into in order to get the wanted effect on _all_ selected items of the current page. Otherwise it affects only the newly selected items.

    ---

    <Application
        x:Class="App1.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                   <ResourceDictionary Source="Common/StandardStyles.xaml"/>
                    <ResourceDictionary>
                        <SolidColorBrush x:Key="ListBoxItemSelectedPointerOverBackgroundThemeBrush" Color="Red"></SolidColorBrush>
                    </ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>

    ---

    <Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <ListBox VerticalAlignment="Top" FontSize="24" SelectionMode="Multiple">
                <ListBoxItem Content="Item 1" IsSelected="True"/>
                <ListBoxItem Content="Item 2" IsSelected="False"/>
                <ListBoxItem Content="Item 3" IsSelected="False"/>
                <ListBoxItem Content="Item 4" IsSelected="False"/>
            </ListBox>
            <Button Content="Go" Click="Button_Click_1" x:Name="button" VerticalAlignment="Bottom" />
        </Grid>
    </Page>

    -

    using Windows.UI;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    namespace App1
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                base.OnNavigatedFrom(e);
            }
            static bool flipFlop;
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                Application.Current.Resources["ListBoxItemSelectedPointerOverBackgroundThemeBrush"] =
                    flipFlop ? new SolidColorBrush(Colors.Olive) : new SolidColorBrush(Colors.Orange);
                flipFlop = !flipFlop;
                // Possibly you want to navigate away to refresh the color 
                // Try with the following commented or not:
                this.Frame.Navigate(typeof(MainPage));
                // If commented out, then navigating to the new page will set the new color on all selected items
                // If commented,then staying on the current page will set the new color on all newly selected items
            }
        }
    }
    


    • Marked as answer by sscd Tuesday, January 29, 2013 1:17 AM
    Monday, January 28, 2013 4:34 PM