Setter is called twice

411 Views Asked by At

I have run into a problem I cannot understand and I have not found a solution on here or on Google. I needed a way to inform my Viewmodel about changes to the application size to change visibility of columns in my Grid. The solution I am using is not the prettiest so if you have any other suggestion, I am gratious.

The problem is that when the Adaptive Trigger triggers it is updating the value of a two-way bound text in a hidden textbox. This is then pushed to the viewmodel and the layout is updated. But due to some reason the old value is pushed again directly after and just resets the changes. So the setter is called twice one with the new correct value and directly after again with the old value.

XAML:

        <VisualStateGroup x:Name="Base">
            <VisualState x:Name="Full">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="1024"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="NavigationRow.Height" Value="0"/>
                    <Setter Target="HelperBox.Text" Value="Full"/>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="Medium1">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="600"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="HelperBox.Text" Value="Medium"/>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="Small1">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="HelperBox.Text" Value="Small"/>
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>

Hidden textbox:

<TextBox x:Name="HelperBox" Text="{Binding ApplicationSize, 
Mode=TwoWay, UpdateSourceTrigger=Default}" 
Visibility="Collapsed"/>

ViewModel The bound string:

private string _applicationSize;
    public string ApplicationSize
    {
        get { return _applicationSize; }
        set
        {
            _applicationSize = value;
            UpdateLayout();
        }
    }

The UpdateLayout Method:

   private void UpdateLayout()
    {
        switch (ApplicationSize)
        {
            case "Small":
                FirstColumnVisibility = true;
                SecondColumnVisibility = false;
                ThirdColumnVisibility = false;
                break;
            case "Medium":
                FirstColumnVisibility = true;
                SecondColumnVisibility = true;
                ThirdColumnVisibility = false;
                break;
            case "Full":
                FirstColumnVisibility = true;
                SecondColumnVisibility = true;
                ThirdColumnVisibility = true;
                break;
        }
    }
1

There are 1 best solutions below

0
On BEST ANSWER

Thanks for your responses. I solved it by changing the width of the containing grids with the triggers. The width was also bound to the view model and that way I could update the data accordingly.