I have some StackPanel
s in a Grid. They are filled with Labels (height of all labels > displayable space). A possible XAML would be:
<Grid>
<StackPanel>
<Label Content="bla" Background="lime" />
<Label ...>
...
</StackPanel>
</Grid>
Every time the size of the Grid
changes, I need to change the inner content of the StackPanel
. I need to hide the overflowing Label
that is only shown partly. To achieve this, I can use following solutions: with a Converter and make a new class that is inherited from StackPanel.
I want to create a different way by using an attached property. I have following code:
//DepProp OverflowVisibility (double), can save height value
public static void Initialized(DependencyObject pObject, DependencyPropertyChangedEventArgs e)
{
var panel = (pObject as Panel) //the StackPanel in this case
panel.SizeChanged += panel_updateInnerLayout;
}
static void panel_LayoutUpdated(object sender, EventArgs e)
{
var parent = sender as Panel;
if(parent != null)
foreach(FrameworkElement element in parent.Children)
{
var elementPos = element.TransformToAncestor(parent).Transform(new Point(0,0));
if(element.ActualHeight + elementPos.Y >
(double)parent.GetValue(OverflowVisibilityProperty))
element.Visibility = Visibility.Hidden;
else
element.Visibility = Visibility.Visible;
}
}
And an example for usage in XAML:
<Grid>
<ItemsControl>
<ItemsPanel>
<ItemsPanelTemplate>
<StackPanel own:OverflowVisibility.OverflowVisibility="{Binding Grid height}" />
</ItemsPanelTemplate>
</ItemsPanel>
</ItemsControl>
</Grid>
Every time the StackPanel
changes it's size, I can update my labels with the panel_updateInnerLayout
event handler. When the size of StackPanel
is changed, everything is working fine.
My Problem: The StackPanel
itself doesn't raise SizeChanged
because it has a bigger height than the Grid
. I need an event that raises every time the Grid
changes its height.
My Question: Is there any event instead of SizeChanged
, that is called every time I change the Grid
size? If not, is there an alternative way with an attached property to solve my problem?
I also tried to set a binding of the height of the StackPanel
to ItemsControl
ActualHeight but it does not raise the SizeChanged
.