0

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:

  1. Minimize the main window — some elements in my control are hidden, because the control shrinks
  2. Go to another view in the application
  3. Maximize the main window
  4. 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>
vladek
  • 577
  • 1
  • 4
  • 17
  • Not sure which "*size change event*" you are referring to. You are apparently handling the DataContextChanged event only. Be aware that the relevant parts of your code should be shown in your question, not in other posts linked from your question. – Clemens May 13 '21 at 09:26
  • I am handling `DataContextChanged` only because the rest works with the solution described in the linked question. – vladek May 13 '21 at 09:31
  • I don't believe it's needed for me to duplicate code easily accessible else where on my question — this would only make it harder to read. I'm just building a question on another one which I believe is a legitimate thing to do. – vladek May 13 '21 at 09:32

0 Answers0