SiberianGuy
SiberianGuy

Reputation: 25292

WPF Postpone control rendering

I need to postpone rendering of a part of the view till ViewModel's property gets some value. I tried to set Visible = Hidden to some container but it didn't work: UI is rendered anyway (but is not displayed). I guess the solution is to use DataTemplate. I tried the following:

<ContentControl>
    <DataTemplate>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding DataContext.State}" Value="0"></DataTrigger>
        </DataTemplate.Triggers>

        <TextBlock>Yes</TextBlock>
    </DataTemplate>
</ContentControl>

But it didn't work. Please hint me the way of right DataTemplate usage (or some alternative ways to solve this problem).

Upvotes: 0

Views: 40

Answers (1)

Damascus
Damascus

Reputation: 6651

Tricky problem here, but here is my take.

I'll assume that you are waiting for the State property to be initialized. Add a boolean property to your ViewModel, namely IsStateReady, firing PropertyChanged:

private bool isStateReady;

public bool IsStateReady
{
   get { return isStateReady; }
   set 
   {
      isStateReady = value;
      OnPropertyChanged("IsStateReady");
   }
}

Then, define your control with a Trigger setting the Template property only when IsStateReady is true:

        <Control>
            <Control.Style>
                <Style TargetType="{x:Type Control}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsStateReady, UpdateSourceTrigger=PropertyChanged}" Value="True">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <!-- your template here-->
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Control.Style>
        </Control>

This should do the trick for you

Upvotes: 2

Related Questions