0

I have created class which has Static properties. Now i would like to bind all to textbox. but i couldn't able to bind.

Class with Static Properties:

namespace QSys.Library.Security
{
    public class CustomerServiceData
    {
        public static string UserName
        {
            get
            {
                return "Imdad";
            }
        }
    }
}

Page.xaml

<UserControl  x:Class="QSys.Admin.Views.AdminHomeView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:my="clr-namespace:QSys.Library.Security;assembly=QSys.Library"
    mc:Ignorable="d"
    d:DesignHeight="400" d:DesignWidth="640">
    <UserControl.Resources>
        <my:CustomerServiceData  x:Name="mySecurity" />
    </UserControl.Resources>
    <Grid>
         <TextBox Text="{Binding Source={StaticResource mySecurity}, Path=CustomerServiceData.UserName}" ></TextBox>
    </Grid>
</UserControl>

Can anybody suggest me why i am not getting values in my textbox? even it doesn't giving me any error yet.

Thanks, Imdadhusen

imdadhusen
  • 2,493
  • 7
  • 40
  • 75

1 Answers1

1

You are specifying the path wrong over there and another thing always specify a key to you added resource .Below are the changes which you need to condier to make it work :

 <UserControl.Resources> 
<my:CustomerServiceData  x:Key="mySecurity" />
 </UserControl.Resources>

Text boxes default to two-way bindings, so you will get an error if you dont set it to OneWay as it will also look for the setter in your property defined due to its TwoWay binding beahviour.

<TextBox Text="{Binding Source={StaticResource mySecurity},Mode=OneWay, Path=UserName}" >
</TextBox>
Malcolm
  • 1,801
  • 3
  • 21
  • 48