Click here to Skip to main content
11,711,009 members (83,030 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WPF C#4.0 .NET4
Dear All,
I have a class (Session), which contain static property. Static property contain Brush type property (ForeGround). I want to bind Foreground property in a resource in wpf xaml.
(Foreground="{Binding Source={StaticResource session}, Path=Object.ForeGround}") does not work

I am unable to bind Foreground property. Please Help
Below is sample code

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Session.Object = new Info() { ForeGround = Brushes.White };
        } 
 
private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            //If I quick watch item. i got session object, and required foreground value is Ok.
            
            var item = this.FindResource("session");
        }       
    }
 
   //*****************************************
    public class Session
    {
        public static Info Object
        {
            get;
            set;
        }
    }
 
    //**************************************
    public class Info
    {
        public Brush ForeGround
        {
            get;
            set;
        }
    }

and here is WPF XAML

<Window.Resources>
       <local:Session x:Key="session"/>
   </Window.Resources>
   <Grid removed="Gray">
       <Viewbox>
           <TextBlock Name="txt" Text="Hello Text" Foreground="{Binding Source={StaticResource session}, Path=Object.ForeGround}"/>
       </Viewbox>
 
   </Grid>
Posted 18-Apr-13 19:56pm
Edited 18-Apr-13 21:31pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

take a look at : http://stackoverflow.com/questions/936304/binding-to-static-property[^]

I take it this is one way binding? To bind to statics you can use the x:Static binding expression.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This might work for what you are trying to achieve:

http://stackoverflow.com/questions/936304/binding-to-static-property[^]

If not, there is this article:

Dynamic Binding of Static Properties[^]
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 549
1 OriginalGriff 255
2 Peter Leow 154
3 Maciej Los 120
4 Abhinav S 120
0 Maciej Los 95
1 Richard MacCutchan 90
2 Abhinav S 90
3 OriginalGriff 80
4 Member 4653839 50


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100