0

添付プロパティを作成しています。私の添付クラスはhelper:FocusDetailであり、2 つのプロパティがあります。2 番目のプロパティDetailBodyタイプはオブジェクトです。このプロパティをアイテム コントロールで使用しています

<ItemsControl ItemsSource="{Binding Riches}" BorderThickness="0">
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <TextBox Text="{Binding TextInfo}"
             helper:FocusDetail.DetailTitle="{StaticResource strTitle}"
             helper:FocusDetail.DetailBody="{Binding Description}"
             />
     </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

それは正常に機能しています

私はこのように添付値を変更しています

<DataTemplate>
   <TextBox Text="{Binding TextInfo}"
         helper:FocusDetail.DetailTitle="{StaticResource strTitle}">
       <helper:FocusDetail.DetailBody>
           <Binding Path="Description"/>
       </helper:FocusDetail.DetailBody>
   </TextBox>  
</DataTemplate>

それは私がまた変えている仕事です

<DataTemplate>
   <TextBox Text="{Binding TextInfo}"
         helper:FocusDetail.DetailTitle="{StaticResource strTitle}"
         >
         <helper:FocusDetail.DetailBody>
            <TextBlock Text="Some static text"></TextBlock>
         </helper:FocusDetail.DetailBody>
   </TextBox>  

それは働いています。私の最後の変更はここにあります

<DataTemplate>
   <TextBox Text="{Binding TextInfo}"
         helper:FocusDetail.DetailTitle="{StaticResource strTitle}">
        <helper:FocusDetail.DetailBody>
           <TextBlock Text="{Binding Description}"></TextBlock>
        </helper:FocusDetail.DetailBody>
   </TextBox>  
</DataTemplate>

これは仕事ではありません。テキストブロックが空です。

私は変わっています

<TextBlock Text="{Binding Description}"></TextBlock>

<TextBlock Text="{Binding }"></TextBlock>.

しかし、textblock は Window DataContext タイプを返します。Itemscontrol の繰り返しから既に終了しています。

なぜバインディングが間違って動作するのですか?

最後のコードのように添付プロパティを宣言する方法は?

ビジュアル ツリー コントロールを含む添付プロパティが必要です。

4

1 に答える 1

1

Binding は、継承された DataContext に依存しているが、(非コンテンツ) プロパティに割り当てられることによって FrameworkElement DataContext 継承構造から取り出されているため、壊れています。

最善の解決策を実行しようとしているように見えることから、代わりに DataTemplate を使用して UI 要素 (ここでは TextBlock) を定義し、データ自体に別のプロパティを用意して、テンプレートに適用できるようにすることをお勧めします。ビジュアルが表示されると予想されるツリー内のポイントにある ContentControl または ContentPresenter (これは、何らかのツールチップ/ポップアップを駆動するためのものだと思います)。

     <TextBox Text="{Binding TextInfo}"
         helper:FocusDetail.DetailTitle="{StaticResource strTitle}"
         helper:FocusDetail.DetailBody="{Binding}"
         >
         <helper:FocusDetail.DetailBodyTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Description}"></TextBlock>
            </DataTemplate>
         </helper:FocusDetail.DetailBodyTemplate>
     </TextBox>
于 2010-10-15T14:59:24.037 に答える