1

I've discovered what the problem was, It is nothing to do with the C# code itself, But it's in the XAML instead, The issue was the default colors that I've set in the XAML were overriding my style's colors.

So in conclusion, when you are setting any property by XAML it always overrides later styles set by C# code at runtime, this seems strange to me but at least that is how it worked for me.

David von Tamar
  • 797
  • 3
  • 12
  • 29
  • Styles should be created in XAML. And applied also in XAML. Why to fill the codeBehind with unuseful things. Try to take a see in MVVM pattern for WPF. http://csharperimage.jeremylikness.com/2010/04/model-view-viewmodel-mvvm-explained.html You'll notice that is not a good practice the usage of a style in the Window (or other UserControl) constructor. – mihai Feb 22 '13 at 07:24
  • Also, here is a good tutorial which will teach you how to create and use in a correct manner the styles. http://www.wpftutorial.net/ :) BTW, in the future, you'll never create styles with C# .. You'll use XAML templates (STyles or other resources) – mihai Feb 22 '13 at 07:27
  • 1
    Why using C# instead of XAML for styling ? There is a reason , But i can't really describe here why , in a limited comment of 500 characters . Generally i don't like the XAML mechanism , But there are also many technical reasons for it too . – David von Tamar Feb 22 '13 at 07:51
  • 1
    meorfi , Thanks for the wpftutorial.net suggestion , I am already using it :) – David von Tamar Feb 22 '13 at 07:56

3 Answers3

1

The default Background colors in the XAML code avoided the C#'s style to apply on the panels (At-least avoided the new Background to be applied over the default ones).

David von Tamar
  • 797
  • 3
  • 12
  • 29
0

You haven't posted the creation of your style, maybe something is missing there?

There is another similar answer on StackOverflow which is a very good and short example of creating and setting a style in code:

Q: Does anyone know how to create a wpf Style in code behind, I can't find anything on the web or MSDN docs. I have tried this but it is not working:

A: You need to add setters to the style rather than using RegisterName. The following code, in the Window_Loaded event, will create a new TextBlock style which will become the default for all instances of a TextBlock within the Window. If you'd rather set it explicitly on one particular TextBlock, you can set the Style property of that control rather than adding the style to the Resources dictionary.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Style style = new Style(typeof (TextBlock));
    style.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Green));
    style.Setters.Add(new Setter(TextBlock.TextProperty, "Green"));
    Resources.Add(typeof (TextBlock), style);
}
Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

I used your code and modified little bit for verification. Seems to be working fine. Have a look:

 Style Style_Panel = new Style(typeof(Panel));

        public void Init_Style()
        {
            // Create Styles :
            #region "Create Styles"

            Style_Panel.Setters.Add(new Setter()
            {
                Property = Panel.BackgroundProperty,
                Value = new SolidColorBrush(Colors.Red)
            });
            Resources.Add(Style_Panel.TargetType, Style_Panel);

            #endregion

            // Apply Styles :
            #region "Apply Styles"

            List<Visual> List_Visual = new List<Visual>();
            List_Visual.Add(new StackPanel() { Name = "btn" });
            //Enum_Visual(Panel_Main, List_Visual);
            foreach (Visual visual in List_Visual)
            {
                if (visual is Panel)
                {
                    Panel panel = visual as Panel;
                    //if (Tagged(panel, "titlebar"))
                    //{

                    //}
                    //else if (Tagged(panel) == false)
                    {
                        // panel.Background = new SolidColorBrush( Colors.Red ); // <- WORKS .
                        panel.Style = Style_Panel; // <- DOES NOT WORKS !
                    }
                }
            }

            #endregion
        }
Manish
  • 510
  • 2
  • 12
  • Hey , Thanks for the try , But i discovered that the problem isn't in my C# code even .. It is in the XAML code , After i did removed the default colors of each panels , The new styles applied to them correctly so the red color was seen , But then suddenly the Events of each panel won't work Like the MouseDown , Maybe it is because they don't have background so you can not really press them .. I dont know i will check it out . I Still don't understand why the default colors in the XAML avoids the styles to apply over them . – David von Tamar Feb 22 '13 at 07:35
  • That is the way dependency property works. The value coming from Style will be overridden by the value you set directly. Read this http://msdn.microsoft.com/en-us/library/ms743230.aspx – Manish Feb 22 '13 at 08:42