2012-05-31 111 views
0

我的标签没有显示任何内容。我想要做的是我有一个用户控件TemplateForPlan,我从该usecontrol中获取选定的项目,然后我来到下一个用户控件,并且选定的模板名称必须在标签内容中。标签绑定不起作用

对不起差描述。我是一个新手,刚开始在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文件TemplateForPlan

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"); 
    } 

和TemplateType在其他定义项目及其认定中的是:

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; 
     } 
    } 
} 

请也给了一些很好的链接,这样我可以正确理解结合。我非常困惑。

+0

Coul dyou添加TemplateType类的代码?我的猜测是它没有实现INotifyPropertyChanged。 –

+0

我看不到你在哪里创建一个“TemplateForPlan”对象。你正在声明它是什么,但你没有定义它,或者在代码中的任何地方创建一个可视化元素树。标签中的绑定只能访问同一视觉树中的元素名称。显示更多您声明标签的文件。您需要实际在该文件中实例化一个TemplateForPlan对象以绑定到该文件。即使如此,为了使绑定工作,您需要实现DependencyProperty或INotifyPropertyChanged。 –

+0

TemplateType类是在其他项目中定义的。 – RATHI

回答

1

不能绑定到字段。

listTemplate是项目控制,所以它有它可以绑定到一个属性在你的代码后面的SelectedItem属性。

public TemplateType SelectedTemplate { get; set; } 

然后更改标签绑定:

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

(注意在路径名的大小写的变化。如果你张贴在TemplateForPlanUC您的ItemsControl的XAML然后我将包括一个例子在我的回答中适合你的情况)。

你还需要确保你对你的控制执行INotifyPropertyChanged,并确保您的SelectedTemplate财产在其制定者通知。我不会在这里详细说明,因为它在StackOverflow之前已经被覆盖了十亿次。