Răspuns How do I change a brush based on a style in VS 2010?

  • 5 aprilie 2012 15:49
     
      Are cod

    I am trying to set the Background property on a grid in my app based on a style, and, for some reason, the brush does not change based on the style.

    Here is what I am doing:

    In the grid, I have the Background property set as follows:

    <Grid Background="{DynamicResource GridBackgroundBrush}" />

    In one style ResourceDictionary I have a SolidColorBrush like this:

    <SolidColorBrush x:Key="GridBackgroundBrush" .../>

    In the other style ResourceDictionary I have a LinearGradientBrush like this:

    <LinearGradientBrush x:Key="GridBackgroundBrush" .../>

    I know the LinearGradientBrush works as I have tried it by itself on the grid.

    Swapping styles changes the colors in my app as I would expect; however, no matter what I do, I am unable to change any brush to another brush.

    I have also tried a style in each style ResourceDictionary that sets the brush, and declared the style on the grid element as a DynamicResource, and that also does not work.

    In all other cases where my app uses SolidColorBrush, I found that if I declare the color as a dynamic resource, and redefine it in my style ResourceDictionary, the color changes when I switch styles.

    Now, however, I really want to change the brush from a SolidColorBrush to a LinearGradientBrush, but nothing I have tried seems to work. From what I have read, this should work. Is there something else that I am missing, or is this a bug? A coworker of mine did exactly this in VS 2008, however, this same approach does not seem to work in VS 2010.

    Thanks in advance!



    • Editat de wiyosaya 5 aprilie 2012 15:52
    •  

Toate mesajele

  • 5 aprilie 2012 18:40
     
      Are cod

    May sure you are setting ResourceDictionary correctly. Most probably you might be doing something wrong there. Following article should help you with steps to do the same

    http://www.codeproject.com/Articles/35346/Using-a-Resource-Dictionary-in-WPF

    Application.Current.Resources.Source = new Uri("NewResourceDictionary.xaml", UriKind.Relative);


    Gaurav Khanna | Microsoft VB.NET MVP

  • 6 aprilie 2012 07:04
    Moderator
     
      Are cod

    Hi wiyosaya,

    If you want to change resourceDictionary dynamically, refer to this link, it provides you a sample:

    http://weblogs.asp.net/psheriff/archive/2009/12/01/load-resource-dictionaries-at-runtime-in-wpf.aspx

    If you want to change it by code, refer to below sample:

    <Window.Resources>
        <SolidColorBrush x:Key="MyColor" Color="Aqua" />
    
        <VisualBrush x:Key="MyBrush">
            <VisualBrush.Visual>
                <Ellipse Height="50" Width="100" Fill="{DynamicResource MyColor}" />
            </VisualBrush.Visual>
        </VisualBrush>
    </Window.Resources>
    <Grid Background="{DynamicResource MyBrush}">
        <Button Height="30" Width="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" Content="ChangeColor" Click="Button_Click" />
    </Grid>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ((SolidColorBrush)Resources["MyColor"]).Color = Colors.Purple;
        }

    Best regards,

    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • 9 aprilie 2012 13:47
     
      Are cod

    May sure you are setting ResourceDictionary correctly. Most probably you might be doing something wrong there. Following article should help you with steps to do the same

    http://www.codeproject.com/Articles/35346/Using-a-Resource-Dictionary-in-WPF

    Application.Current.Resources.Source = new Uri("NewResourceDictionary.xaml", UriKind.Relative);


    Gaurav Khanna | Microsoft VB.NET MVP


    I had not come across this post yet. If my current efforts fail, I will try this.
  • 9 aprilie 2012 13:49
     
      Are cod

    Hi wiyosaya,

    If you want to change resourceDictionary dynamically, refer to this link, it provides you a sample:

    http://weblogs.asp.net/psheriff/archive/2009/12/01/load-resource-dictionaries-at-runtime-in-wpf.aspx

    If you want to change it by code, refer to below sample:

    <Window.Resources>
        <SolidColorBrush x:Key="MyColor" Color="Aqua" />
    
        <VisualBrush x:Key="MyBrush">
            <VisualBrush.Visual>
                <Ellipse Height="50" Width="100" Fill="{DynamicResource MyColor}" />
            </VisualBrush.Visual>
        </VisualBrush>
    </Window.Resources>
    <Grid Background="{DynamicResource MyBrush}">
        <Button Height="30" Width="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" Content="ChangeColor" Click="Button_Click" />
    </Grid>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ((SolidColorBrush)Resources["MyColor"]).Color = Colors.Purple;
        }

    Best regards,

    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    This approach seems to be what most of the examples in internet land show, and it is consistent with what I have tried already. For whatever reason, it seems to be failing for me. There is a reply on the blog that also says that it was not working for one other person, too, however, there is no follow-up that states whether the person who posted the trouble ever resolved it.
  • 10 aprilie 2012 05:02
    Moderator
     
     

    Hi wiyosaya,

    I have provided you two approaches, which one does not work, I have tested first one before post it, it works on my side, if it has issues on your side, could you share your sample with me, it will be helpful for me to find your issue.

    the second approach coould work as well.

    best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • 16 aprilie 2012 13:46
     
     Răspuns
    In my case, I am styling an application that has several UserControl derived pages in it. It appears that what needs to be done is that the style has to be applied to every page individually. Most example code that I have seen is not as complicated, and from the information that I have read, I was lead to believe that if the styles were placed in the Apps.ResrouceDictionary.MergedDictionaries member, it would "just work." This does not seem to be the case, as the only means that I have yet found is to apply the style to the MergedDictionaries element on each page.
    • Marcat ca răspuns de wiyosaya 16 aprilie 2012 13:46
    •