I use this solution to observe my control's actual height to show/hide certain elements based on those values and a threshold using a converter. The solution works great, except for the following scenario:
- Minimize the main window — some elements in my control are hidden, because the control shrinks
- Go to another view in the application
- Maximize the main window
- Go back to the previous view — the elements are still hidden, despite the control being at full size
I am assuming this is because the size change bindings are initialized after the size change event is fired — i.e. before the initial initial view model is initialized. Any solution to this, preferably not involving the code behind? I've already tried the following and it's not working:
public void MyInitialView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is IInitialViewModel viewModel)
{
viewModel.Width = ActualWidth;
viewModel.Height = ActualHeight;
}
}
For reference this is what my hide/show logic looks like:
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource DoubleToVisibilityConverter}">
<!-- Height -->
<Binding Path="Height" />
<!-- Threshold -->
<Binding>
<Binding.Source>
<sys:Double>120</sys:Double>
</Binding.Source>
</Binding>
<!-- Is visible below threshold -->
<Binding>
<Binding.Source>
<sys:Boolean>false</sys:Boolean>
</Binding.Source>
</Binding>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Grid.Style>