2012-05-31 40 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之前已經被覆蓋了十億次。