RATHI
RATHI

Reputation: 5299

Binding of Label not working

my label is showing no content . what i am trying to do is i have a usercontrol TemplateForPlan and i am getting the selected item from that usecontrol and after that i am coming to next usercontrol and that selected template name must be there in label content.

sorry for poor description . i am a newbie and just started to work on WPF.

<UserControl x:Class="ChaosMonkeyUI.TemplateForPlan"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="344" d:DesignWidth="424" Name="TemplateForPlanUC">

and this this is the label on another UC to show selected template

 <Label Content="{Binding ElementName=TemplateForPlanUC, Path=selectedTemplate.TemplateName }" Grid.Row="1" Grid.Column="1" Height="28" HorizontalAlignment="Stretch" 
        Name="labelTemplateName" VerticalAlignment="Stretch" Margin="10,5,0,5" />

this is .cs file of TemplateForPlan and

public partial class TemplateForPlan : UserControl
{
    IList<TemplateType> template;
    public int noOfElementSelected;
    TemplateHelper xmlParser ;
    NewChaosSteps parentNewChaosStepPageForNextButton;
    public TemplateType selectedTemplate = null;

    public TemplateForPlan( NewChaosSteps parentNewChaosStepPageForNextButton)
    {
        InitializeComponent();
        this.parentNewChaosStepPageForNextButton = parentNewChaosStepPageForNextButton;
        parentNewChaosStepPageForNextButton.EnableOrDisableNextButton("disable");
        xmlParser = new TemplateHelper();
        template = xmlParser.GetTemplates();
        listTemplate.ItemsSource = template;
    }

    private void listTemplate_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        selectedTemplate = template[listTemplate.SelectedIndex];
        parentNewChaosStepPageForNextButton.EnableOrDisableNextButton("enable");
    }

and TemplateType is defined in other project and its defination is:

public partial class TemplateType 
{

    private TemplateRuleType[] templateRuleField;

    private string templateNameField;

    private string templateDescriptionField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("TemplateRule")]
    public TemplateRuleType[] TemplateRule {
        get {
            return this.templateRuleField;
        }
        set {
            this.templateRuleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string TemplateName {
        get {
            return this.templateNameField;
        }
        set {
            this.templateNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string TemplateDescription {
        get {
            return this.templateDescriptionField;
        }
        set {
            this.templateDescriptionField = value;
        }
    }
}

please also give some good link so that i can properly understand binding . i am very much confused in it.

Upvotes: 0

Views: 203

Answers (1)

slugster
slugster

Reputation: 49965

You cannot bind to a field.

listTemplate is an items control, so it will have a SelectedItem property which you can bind to a property in your code behind.

public TemplateType SelectedTemplate { get; set; }

Then change your Label binding:

<Label Content="{Binding ElementName=TemplateForPlanUC, Path=SelectedTemplate.TemplateName }"  />

(Notice the change in capitalisation of the name in the Path. If you post the XAML for your ItemsControl in TemplateForPlanUC then I will include an example that suits your case in my answer).

You also need to ensure you implement INotifyPropertyChanged on your control, and ensure that your SelectedTemplate property notifies in its setter. I won't detail that here because it has been covered a billion times before here on StackOverflow.

Upvotes: 1

Related Questions