简体   繁体   中英

Unable to bind dependency property of usercontrol

I have main UI in which I have called the UserControl1(Charting control). Charting control have following dependency properties:

  • ChartTitle

  • ChartDataDetails

In UserControl1 I have called another UserControl2(MultiCharting Control). Which has some dependency properties like

  • MultichartInputData etc.

My problem is when I launch main UI I am unable to bind DataStoreDetail property to DP (ChartDataDetail) of UserControl1. DataStoreDetail property defined in SetPointModel.cs, which is referred in SetPointVM which is ViewModel for main UI.

MainUI.xaml:

<Charting:ChartControl x:Uid="Charting:ChartControl_1"
    ChartDataDetails="{Binding Path=Model.DataStoreDetails, Mode=TwoWay,
    diag:PresentationTraceSources.TraceLevel=High}"/>

UserControl1:

<multicharting:MultiChartControl x:Uid="multicharting:MultiChartControl_1"
    x:Name="MultiChart" MultiChartInputDetails="{Binding MultiChartsInputDetails,
    Mode=TwoWay, "/>

Binding between UserControl1 and UserControl2 is working fine, problem in binding between MainUI and UserControl1.

DP in UserControl1:

public DataDetails ChartDataDetails
{
    get { return (DataDetails)GetValue(ChartDataDetailsProperty); }
    set { SetValue(ChartDataDetailsProperty, value); }
}

public static readonly DependencyProperty ChartDataDetailsProperty =
    DependencyProperty.Register("ChartDataDetails",
    typeof(DataDetails), typeof(ChartControl),
    new UIPropertyMetadata(new PropertyChangedCallback(ChartDataDetailsChanged)));

Try to use the same ViewModel for the UserControls mentioned and set their DataContexts accordingly.

Or

try to change the DataContext on your UserControls :

Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
 AncestorType={x:Type Window}}, Path=DataContext.DataStoreDetails}" ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM