Click here to Skip to main content
14,301,895 members
Rate this:
Please Sign up or sign in to vote.
See more:
The DependencyProperty is always shows value null.

Even i pass static or binding variable from main usercontrol (UCProfitGraphs)

The referred usercontrol(UCCurrentMonth) DependencyProperty is null.

What I have tried:

<UserControl x:Class="DashBoard.UCProfitGraphs"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:screen="clr-namespace:DashBoard.ProfitGraphs"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<grid>
<grid.columndefinitions>
<columndefinition>
<columndefinition>

<grid.rowdefinitions>
<rowdefinition>
<rowdefinition>

<screen:UCCurrentMonth SelectedProductProfitChart="ProductA" >

<screen:UCCurrentMonth Grid.Row="1" SelectedProductProfitChart="ProductB" />



 public partial class UCCurrentMonth: UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public static readonly DependencyProperty SelectedProductProfitChartProperty = DependencyProperty.Register
           (
           "SelectedProductProfitChart",
           typeof(string),
           typeof(UCCurrentMonth),
           null
           //,
           //new PropertyMetadata("")
           );

        public string SelectedProductProfitChart
        {
            get { return (string)GetValue(SelectedProductProfitChartProperty); }
            set { SetValue(SelectedProductProfitChartProperty, value); }
        }

       
        public UCCurrentMonth()
        {
            InitializeComponent();
            LoadData();
            this.DataContext = this;
        }

        private void LoadData()
        {

//SelectedProductProfitChart is always null
            if (SelectedProductProfitChart== "ProductA")
            {
//                GetProductA Info
            }
            else if (SelectedProductProfitChart== "ProductB")
            {
//                GetProductB Info
            }
        }
    }
Posted
Updated 20-Nov-18 5:38am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Aehm, lets ask the question the other way around, why should the datacontext of UCCurrentMonth not be null? I cannot see any useful bindings in the code you are posting and it looks quite strange that you implement
NotifyPropertyChanged
in the UserControl(?)

You do know that SelectedProductProfitChart="ProductA" is just setting the property to a string (if this property accepts a string) while a binding looks slightly different, right?

In the end its not possible to tell what your problem is because the snippets you posted are in-complete (are missing at least a viewmodel?)...
   
Comments
Sadique KT 18-Nov-18 5:21am
   
I am not using MVVM pattern...in this project and its a sample code..
Rate this:
Please Sign up or sign in to vote.

Solution 2

You're calling LoadData too soon. The property value won't be set until after the constructor has executed.

Use a property change callback instead:
public partial class UCCurrentMonth: UserControl
{
    public static readonly DependencyProperty SelectedProductProfitChartProperty = DependencyProperty.Register(
        "SelectedProductProfitChart",
        typeof(string),
        typeof(UCCurrentMonth),
        new PropertyMetadata("", OnSelectedProductProfitChartChanged));
    
    public string SelectedProductProfitChart
    {
        get { return (string)GetValue(SelectedProductProfitChartProperty); }
        set { SetValue(SelectedProductProfitChartProperty, value); }
    }
    
    private static void OnSelectedProductProfitChartChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        var control = source as UCCurrentMonth;
        if (control != null) control.OnSelectedProductProfitChartChanged(e);
    }
    
    protected virtual void OnSelectedProductProfitChartChanged(DependencyPropertyChangedEventArgs e)
    {
        LoadData();
    }
    
    public UCCurrentMonth()
    {
        InitializeComponent();
        DataContext = this;
    }
    
    private void LoadData()
    {
        string chart = SelectedProductProfitChart;
        if (chart == "ProductA")
        {
            // GetProductA Info
        }
        else if (chart == "ProductB")
        {
            // GetProductB Info
        }
    }
}
   
v2

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




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