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")
{
}
else if (chart == "ProductB")
{
}
}
}