2012-01-21 5 views
0

私は私のプロジェクトのいくつかの場所で使用するユーザーコントロールを持っています。私はフィールドを表示するために使用しますが、レイアウトはフィールドの種類によって異なります...コードやテンプレートの外で使用するとうまく動作しますが、データテンプレートで使用すると正しく動作しませんリストビュー。レイアウトを変更するだけですが、値は表示されません(名前&のように)。ListViewでUserControlをDataTemplate(いくつかのバインディング付き)として使用

public string Title 
    { 
     get { return (string)GetValue(TitleProperty); } 
     set { SetValue(TitleProperty, value); } 
    } 
    public static readonly DependencyProperty TitleProperty = 
     DependencyProperty.Register("Title", typeof(string), typeof(ucBoardField), new UIPropertyMetadata("", new PropertyChangedCallback(UpdateTitle))); 

    public string Description 
    { 
     get { return (string)GetValue(DescriptionProperty); } 
     set { SetValue(DescriptionProperty, value); } 
    } 
    public static readonly DependencyProperty DescriptionProperty = 
     DependencyProperty.Register("Description", typeof(string), typeof(ucBoardField), new UIPropertyMetadata("", new PropertyChangedCallback(UpdateDescription))); 

    public string FieldTypeDescription 
    { 
     get { return (string)GetValue(FieldTypeDescriptionProperty); } 
     set { SetValue(FieldTypeDescriptionProperty, value); } 
    } 
    public static readonly DependencyProperty FieldTypeDescriptionProperty = 
     DependencyProperty.Register("FieldTypeDescription", typeof(string), typeof(ucBoardField), new UIPropertyMetadata("street", new PropertyChangedCallback(UpdateFieldTypeDescription))); 

そしてこれらは、彼らのとおりです。

は、私はタイトル、説明プロパティを添付している&タイプなどのプロパティをバインドしたいが、彼らはDataTemplateの中で動作するようには思えません...これらのプロパティが表示されアップデート方法:

public static void UpdateTitle(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs) 
    { 
     ucBoardField bf = sender as ucBoardField; 
     if (bf == null) return; 
     if (bf.Field == null) bf.Field = new Field(); 

     bf.Field.Title = (string)eventArgs.NewValue; 
     bf.lblExtraTax.Text = bf.lblStation.Text = bf.lblStreetname.Text = bf.lblUtility.Text = bf.Field.Title; 
    } 
    public static void UpdateDescription(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs) 
    { 
     ucBoardField bf = sender as ucBoardField; 
     if (bf == null) return; 
     if (bf.Field == null) bf.Field = new Field(); 

     bf.Field.Description = (string)eventArgs.NewValue; 
     bf.lblCity.Text = bf.Field.Description; 
    } 
    public static void UpdateFieldTypeDescription(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs) 
    { 
     ucBoardField bf = sender as ucBoardField; 
     if (bf == null) return; 
     if (bf.Field == null) bf.Field = new Field(); 
     if (bf.Field.FieldType == null) bf.Field.FieldType = new FieldType(); 

     bf.Field.FieldType.Description = bf.FieldTypeDescription = (string)eventArgs.NewValue; 
     bf.ShowContent(); //updates layout 
    } 

コードで対処とき、彼らが正常に動作しますが、すべてのFieldTypeDescription以外のDataTemplateで自分の結果を示し、これは私がそれらをバインドする方法を示します。

<ListView> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <uc:ucBoardField 
        Id="{Binding Path=Field.Id}" 
        FieldTypeId="{Binding Path=Field.FieldTypeId}" 
        Title="{Binding Path=Field.Title}" 
        Description="{Binding Path=Field.Description}" 
        FieldTypeDescription="{Binding Path=FieldType.Description}" /> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

実際には、彼らは仕事をするが、データが表示されないそれらの結合がトリガされたときに... はまた、私は私の出力にエラー(または警告)を取得するには:

System.Windows.Data Error: 40 : BindingExpression path error: 'Field' property not found on 'object' ''Field' (HashCode=64502806)'. BindingExpression:Path=Field.Title; DataItem='Field' (HashCode=64502806); target element is 'ucBoardField' (Name=''); target property is 'Title' (type 'String') 
System.Windows.Data Error: 40 : BindingExpression path error: 'Field' property not found on 'object' ''Field' (HashCode=64502806)'. BindingExpression:Path=Field.Description; DataItem='Field' (HashCode=64502806); target element is 'ucBoardField' (Name=''); target property is 'Description' (type 'String') 

プロパティフィールドは、サーバーから取得する表示するフィールドに関するすべての情報を含む単なるオブジェクトです。 そして、これらは私のリストビューにある:

System.Windows.Data Warning: 40 : BindingExpression path error: 'Title' property not found on 'object' ''TextBlock' (Name='lblTitle')'. BindingExpression:Path=Title; DataItem='TextBlock' (Name='lblTitle'); target element is 'TextBlock' (Name='lblTitle'); target property is 'Text' (type 'String') 
System.Windows.Data Warning: 40 : BindingExpression path error: 'Description' property not found on 'object' ''TextBlock' (Name='lblDescription')'. BindingExpression:Path=Description; DataItem='TextBlock' (Name='lblDescription'); target element is 'TextBlock' (Name='lblDescription'); target property is 'Text' (type 'String') 

それは私がこれらのエラーを取得し、どのように私はそれらを解決することができ、なぜしかし... は、私が間違って行う可能性がありますどのような誰もが知っていクラッシュしませんか?私は事前に感謝します!

+0

はどのようにして、リストビューとアルのためのItemsSourceを設定していますだからDataContext? –

+0

私はフィールドのリストを取得しており、各フィールドをforeachとlvw.items.add(フィールド)で追加します。 ...私はデータコンテキストを設定していません –

+0

ItemsSourceを使用しないとDataContextを設定する必要がありますか? –

答えて

0

私はほんの数分前に同じ問題を抱えていた:あなたが持っている場合

<ListView> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <uc:ucBoardField 
       DataContext="{Binding .}" 
       Id="{Binding Path=Field.Id}" 
       FieldTypeId="{Binding Path=Field.FieldTypeId}" 
       Title="{Binding Path=Field.Title}" 
       Description="{Binding Path=Field.Description}" 
       FieldTypeDescription="{Binding Path=FieldType.Description}" /> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

しかし:あなたはDataContextを設定する必要がありますが、あなたがコントロールで再びそれを設定することはできません

DataContextが再び上書きされますので、それが再び動作しませんあなたのUserControlに(または類似)以下:

<UserControl ... 
      DataContext="{Binding RelativeSource={RelativeSource Self}}" 
      ...> 
+0

私は自分のDataContextを設定していないので、最初のコード例を試してみましょう! :-) –

+0

{Binding。}で動作しません。確認する必要がありますか?またはコードビハインドでDataContextを使用しますか? –

+0

申し訳ありませんが、何か違いがあるようです。私はあなたに何も言えません... – ChrFin

関連する問題