Associating an attached property with an item in an ItemsControl with a custom panel problem

I can't get the next XAML to work the way I want. All bindings work because they do not get errors from the bindings. But I do not get the expected result from snapping on the RatioShape rectangle. The problem is that the attached wpflib: RatioPanel.Ratio property always returns the default value, not the database value.

So, I think the attached property in RatioShape is set in the wrong "context". How to bind to an attached property so that wpflib: RatioPanel gets the bound value?

<wpflib:RatioContentPresenter2 RatioMaxValue="{Binding Path=RatioMaxValue}"> <ItemsControl Grid.Row="0" wpflib:RatioContentPresenter2.RatioOffset="{Binding Path=RatioOffset}" wpflib:RatioContentPresenter2.RatioValue="{Binding Path=RatioValue}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <wpflib:RatioPanel /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Rectangle x:Name="RatioShape" wpflib:RatioPanel.Ratio="{Binding Path=Value}" Fill="{Binding Path=Brush}" /> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsSource> <Binding Path="RatioItems" Mode="OneWay" /> </ItemsControl.ItemsSource> </ItemsControl> </wpflib:RatioContentPresenter2> 
+4
source share
1 answer

RatioPanel children will be instances of ContentPresenter, assuming elements are not UIElements. ContentPresenter displays the DataTemplate that you defined in the ItemTemplate.

Usually panels work directly with their children. You set your attached property to the ContentPresenter child, which is the child of your panel. I believe that you should install this directly in ContentPresenter. So something like this:

 <ItemsControl.ItemContainerStyle> <Style> <Setter Property="wpflib:RatioPanel.Ratio" Value="{Binding Path=Value}" /> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemTemplate> <DataTemplate> <Rectangle x:Name="RatioShape" Fill="{Binding Path=Brush}" /> </DataTemplate> </ItemsControl.ItemTemplate> 
+6
source

All Articles