Silverlight RelativeSource TemplatedParent Binding in a DataTemplate, is this possible?

I am trying to set up user column control. I create each bar using a DataTemplate .

The problem is to calculate the height of each bar, first I need to know the height of its container ( TemplatedParent ). Unfortunately, I have:

 Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height, Converter={StaticResource HeightConverter}, Mode=OneWay}" 

does not work. Every time the NaN value is returned to my converter. Does RelativeSource={RelativeSource TemplatedParent} not work in this context? What else can I do to let the DataTemplate "talk" to the element to which it applies?

In case this helps, these are the DataTemplate bytes:

 <DataTemplate x:Key="BarGraphTemplate"> <Grid Width="30"> <Rectangle HorizontalAlignment="Center" Stroke="Black" Width="20" Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height, Converter={StaticResource HeightConverter}, Mode=OneWay}" VerticalAlignment="Bottom" /> </Grid> </DataTemplate> 
+4
source share
2 answers

To answer your question RelativeSource only works in ControlTemplate , it does not work in DataTemplate .

Is there a reason why the Silverlight Toolkit Chart controls do not work to create a bar chart (or a bar chart like Tookit refers to a vertical set of bars).

+5
source

Have you tried ActualHeight? It should give you meaning. A RelativeSource with TemplatedParent mode will work in the data template, but it will return a presentation of the contents of the template control / element, and not the control / element itself (which it does when used in the control template). To experiment, put the button in the data template and assign this required expression (without path) to its Tag property. Handle its Click event and place the breakpoint in the event handler. Now, when you start the project and click on the button, the breakpoint will be clicked in your code, and you can see the object that it is attached to from the property of the button tag (which you can see from the sender parameter). Hope this helps ...

0
source

Source: https://habr.com/ru/post/1308680/


All Articles