Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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

C#
 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

HTML
<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
Updated 18-Apr-13 21:31pm
v2

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.
 
Share this answer
 
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[^]
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900