Unable to bind DataContext property

This is my first question. I have been looking at this issue for about a whole day, but I canโ€™t understand why this binding does not work.

I want the label to display the name of the "hotspot" object, which is a property of an instance of the class named Plan. There are several plans, and each plan contains several hot spots. When I click on an access point, the property Plan.SelectedHotSpotsets this hotspot with a click as a value. If HotSpot is not selected, it will be zero.

XAML:

<Label Name="lblHotSpotName" />

MainWindow code when Plan is selected from a ListBox:

private void lstPlans_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    canvas.Plan = PlanBLL.GetPlanByID(plans[lstPlans.SelectedIndex].ID);
    lblHotSpotName.DataContext = canvas.Plan;
    lblHotSpotName.SetBinding(Label.ContentProperty, "SelectedHotSpot.Name");
}

Planning Class:

public class Plan : INotifyPropertyChanged
{
    private HotSpot selectedHotSpot;

    public HotSpot SelectedHotSpot
    {
        get { return selectedHotSpot; }

        set
        {
            selectedHotSpot = value;
            OnPropertyChanged("SelectedHotSpot");
            OnPropertyChanged("SelectedHotSpot.Name");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 }

, lblHotSpotName, . , SelectedHotSpot NULL hotspot, . ? , , . , Plan.SelectedHotSpot.

.

+3
3

, , , Label.Content XAML? SelectedHotSpot.Name Plan, ListBox, - :

<Label Name="lblHotSpotName" 
   Content="{Binding SelectedItem.SelectedHotSpot.Name, ElementName=lstPlans}" />

โ†’ >

XAML Binding. string lstPlans_SelectionChanged:

<Label Name="lblHotSpotName" Content="{Binding SelectedItemHotSpotName}" />

...

private void lstPlans_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    canvas.Plan = PlanBLL.GetPlanByID(plans[lstPlans.SelectedIndex].ID);
    SelectedItemHotSpotName = canvas.Plan.SelectedHotSpot.Name;
}
+1

, , lstPlans_SelectionChanged Binding:

var myBinding = new Binding();

myBinding.Path = new PropertyPath("SelectedHotSpot.Name");
myBinding.Source = canvas.Plan;
lblHotSpotName.SetBinding(Label.ContentProperty, myBinding);

SelectedHotSpot.Name , :

OnPropertyChanged("SelectedHotSpot.Name");

SelectedHotSpot.

0

( Raise .Name ).

, selectedHotSpot . :

selectedHotSpot = new HotSpot(Name="Default")

and you should see "Default" in your tag.

0
source

All Articles