Click here to Skip to main content
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 20:56pm
Edited 18-Apr-13 22: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 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
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