Hisham Maudarbocus
Hisham Maudarbocus

Reputation: 620

Swapping ContentPresenter content in custom control

I have 2 ContentPresenter fixedContentPresenter and resizableContentPresenter and obviously a Content in Generic.xaml. How can i set the Content to be only in fixedContentPresenter when FixedContent = true; and Content to be only in resizableContentPresenter when FixedContent = false;

I tried changing the content in the code but the content is not showing. Xaml:

<Grid>
    <ContentPresenter x:Name="fixedContent"/>
    <Grid>
        <ContentPresenter x:Name="resizableContent"/>
    </Grid>
</Grid>

Upvotes: 2

Views: 1166

Answers (3)

AnjumSKhan
AnjumSKhan

Reputation: 9827

You should use ContentControl here instead of ContentPresenter.

When FixedContent values changes, Content becomes null, so no Animation related side-effects.

 <Grid Background="Purple">

    <ContentControl x:Name="fixedContent" Margin="0,75,0,0">
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding FixedContent, Mode=OneWay}" Value="false">
                        <DataTrigger.Setters>
                            <Setter Property="Content" Value="{StaticResource ContentKey}"/>                               
                        </DataTrigger.Setters>
                    </DataTrigger>                        
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>

    <Grid Background="Red" Margin="0,54,0,0">
        <ContentControl x:Name="resizableContent" Margin="0,75,0,0">
            <ContentControl.Style>
                <Style TargetType="{x:Type ContentControl}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding FixedContent, Mode=OneWay}" Value="true">
                            <DataTrigger.Setters>
                                <Setter Property="Content" Value="{StaticResource ContentKey}"/>
                            </DataTrigger.Setters>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    </Grid>
</Grid>

Upvotes: 1

Hisham Maudarbocus
Hisham Maudarbocus

Reputation: 620

After a day of struggle, I finally found a better answer than that proposed by @OmegaMan, let's hope it help others. Here's the new XAML (added Content="{x:Null}" to both ContentPresenter):

<Grid>
    <ContentPresenter x:Name="fixedContent" Content="{x:Null}"/>
    <Grid>
        <ContentPresenter x:Name="resizableContent" Content="{x:Null}"/>
    </Grid>
</Grid>

And the FixedContent logic:

public bool FixedContent
{
    get { return (bool)GetValue(FixedContentProperty); }
    set
    {
        SetValue(FixedContentProperty, value);
        if (value) // Is Fixed
        {
            ResizableContentPresenter.Content = null;
            FixedContentPresenter.Content = Content;
        }
        else
        {
            FixedContentPresenter.Content = null;
            ResizableContentPresenter.Content = Content;
        }
    }
}

Upvotes: 0

ΩmegaMan
ΩmegaMan

Reputation: 31576

Use an implicit style on the content presenters to either hide or show a specific presenter depending on the value of FixedContent

<Style TargetType={x:Type ContentPresenter } x:Key="HideOrShow">
   <Style.Triggers>
      <DataTrigger Binding="{Binding FixedContent}" Value="False">
          <Setter Property="Visibility" Value="Hidden"/>
      </DataTrigger>
   </Style.Triggers>
</Style>

Upvotes: 2

Related Questions