Wpf 标签的装订不起作用

Wpf 标签的装订不起作用,wpf,Wpf,我的标签上没有显示任何内容。我试图做的是我有一个usercontrol TemplateForPlan,我从usecontrol中获取所选项目,然后我进入下一个usercontrol,所选模板名称必须在标签内容中 对不起,描述不好。我是个新手,刚开始做WPF <UserControl x:Class="ChaosMonkeyUI.TemplateForPlan" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre

我的标签上没有显示任何内容。我试图做的是我有一个usercontrol TemplateForPlan,我从usecontrol中获取所选项目,然后我进入下一个usercontrol,所选模板名称必须在标签内容中

对不起,描述不好。我是个新手,刚开始做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">

这是另一个UC上显示所选模板的标签

 <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" />

这是计划模板的.cs文件

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");
    }
公共部分类TemplateForPlan:UserControl
{
IList模板;
已选择公共int Noofelements;
TemplateHelperXmlParser;
NewChaosSteps ParentNewChaosSteps页面用于下一个按钮;
公共模板类型selectedTemplate=null;
计划的公共模板(NewChaosSteps parentnewchaosstepspagefornextbutton)
{
初始化组件();
this.parentNewChaosStepPageForNextButton=parentNewChaosStepPageForNextButton;
parentNewChaosStepPageForNextButton.EnableOrDisableNextButton(“禁用”);
xmlParser=newtemplatehelper();
template=xmlParser.GetTemplates();
listTemplate.ItemsSource=模板;
}
私有无效列表模板\u SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
selectedTemplate=模板[listTemplate.SelectedIndex];
parentNewChaosStepPageForNextButton.EnableOrdSableNextButton(“启用”);
}
模板类型在其他项目中定义,其定义为:

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;
        }
    }
}
public分部类TemplateType
{
私有TemplateRuleType[]templateRuleField;
私有字符串templateNameField;
私有字符串templateDescriptionField;
/// 
[System.Xml.Serialization.XmlElementAttribute(“TemplateRule”)]
公共模板规则类型[]模板规则{
得到{
返回此.templateRuleField;
}
设置{
this.templateRuleField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串模板名{
得到{
返回此.templateNameField;
}
设置{
this.templateNameField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串模板描述{
得到{
返回此.templateDescriptionField;
}
设置{
this.templateDescriptionField=值;
}
}
}

请提供一些好的链接,以便我能正确理解绑定。我对此非常困惑。

您不能绑定到字段

listTemplate
是一个items控件,因此它将具有SelectedItem属性,您可以将该属性绑定到代码中的属性

public TemplateType SelectedTemplate { get; set; }
然后更改标签绑定:

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

(注意路径中名称大写的变化。如果您在TemplateForPlanUC中发布ItemsControl的XAML,那么我将在我的答案中包含一个适合您的示例)


您还需要确保在控件上实现INotifyPropertyChanged,并确保您的
SelectedTemplate
属性在其setter中进行通知。我在这里不详细说明,因为在StackOverflow上,它在这里已经被讨论了10亿次。

您可以添加TemplateType类的代码吗?我猜它不会实现ement INotifyProperty已更改。我看不到您在哪里创建“TemplateForPlan”对象。您正在声明它是什么,但您没有在代码中的任何位置定义或创建可视元素树。标签中的绑定只能访问同一可视树中的元素名称。显示声明标签的文件的更多内容。您需要在同一文件中实际实例化TemplateForPlan对象,以便即使如此,为了使绑定工作,您需要实现DependencyProperty或INotifyPropertyChanged。TemplateType类是在其他项目中定义的。