Search code examples
wpfattached-properties

Attached property declaration in xaml


I'm creating attached property. My attached class is helper:FocusDetail and has 2 property. second property DetailBody type is object. I'm using this property on items control

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

That is successfully working

I'm changing attached value like this

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

That is work I'm changing again

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

That is working. My last change is here

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

This is not work. Textblock is empty.

I'm changing

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

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

But textblock returns Window DataContext type. Already quit from Itemscontrol iteration.

Why Binding wrong working?

How to declare attached property like last code?

I need attached property contains visual tree controls.


Solution

  • Your Binding is breaking because it depends on the inherited DataContext but is being taken out of the FrameworkElement DataContext inheritance structure by being assigned to a (non-Content) property.

    From what it looks like you're trying to do the best solution would probably be to instead use a DataTemplate to define your UI elements (the TextBlock here) and have a separate property for the data itself that can then be applied to the template with a ContentControl or ContentPresenter at the point in the tree where you expect the visuals to be displayed (I assume this is to drive some sort of tooltip/popup).

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