在自定义控件中交换ContentPresenter内容

时间:2016-07-11 15:28:41

标签: c# wpf xaml

我有2个ContentPresenter // test `success` callback let testUser = { user : 'test' }; let stub = sinon.stub(Parse.User.prototype, 'signUp') .yieldsTo('success', testUser); fixedContentPresenter,显然是Generic.xaml中的内容。如果resizableContentPresenter fixedContentPresenter,内容仅 FixedContent = true;resizableContentPresenter内的内容仅 <{1}} }}

我尝试更改代码中的内容,但内容未显示。 XAML:

FixedContent = false;

3 个答案:

答案 0 :(得分:2)

在内容演示者上使用隐式样式隐藏或显示特定演示者,具体取决于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>

答案 1 :(得分:1)

您应该在此使用ContentControl而不是ContentPresenter

FixedContent值发生变化时,Content变为空,因此没有Animation相关的副作用。

 <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>

答案 2 :(得分:0)

经过一天的奋斗,我终于找到了比@OmegaMan提出的更好的答案,让我们希望它能帮助别人。这是新的XAML(Content="{x:Null}"添加ContentPresenter):

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

FixedContent逻辑:

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;
        }
    }
}