【问题标题】:How do you bind a ContentTemplate directly to a Grid?如何将 ContentTemplate 直接绑定到 Grid?
【发布时间】:2011-02-24 04:20:36
【问题描述】:

在以下 XAML 中,我尝试将各种 DataTemplates 直接绑定到 Grid ContentPresenter。我将 Button 放在 Grid 中只是为了向自己证明 ContentTemplate 已绑定并且 DataTriggers 工作正常 - 它们是(注意我此时不需要任何类型的控件)。 如果我将<Button> 替换为<ContentPresenter> 什么都不会出现。 显然我在这里遗漏了一些非常简单的东西。

      <DataTemplate x:Key="MyTemplate">
        <Grid Style="{StaticResource GridAllocatedStyle}">
            <Ellipse Stroke="#FF5A71FB" 
                     StrokeThickness="0.5"
                     Style="{StaticResource EllipseFinanciallyAllocatedStyle}" />
            <TextBlock Style="{StaticResource TextBlockInsideEllipseStyle}" 
                       Text="A"
                       ToolTip="Allocated" />
        </Grid>
    </DataTemplate>


    <DataTemplate x:Key="AllocationTemplate">
        <Grid>           
            <Button> <!-- I want to bind to the Grid.ContentPresenter here -->
                <Button.Style>
                    <Style TargetType="Button">                             
                        <Style.Triggers>                               
                            <DataTrigger Binding="{Binding Allocated}" Value="PreAllocatedBoth">
                                <Setter Property="ContentTemplate" Value="{StaticResource MyTemplate}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>            
        </Grid>      
    </DataTemplate>

为了完整起见,这是我想要实现的目标:

 <DataTemplate x:Key="AllocationTemplate">
        <Grid>
            <Grid.Style>
                <Style TargetType="Grid">                    
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Allocated}" Value="None">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>                       
                    </Style.Triggers>
                </Style>
            </Grid.Style>
            <ContentPresenter> <!-- I want to bind to the Grid.ContentPresenter here -->
                <ContentPresenter.Style>
                    <Style TargetType="ContentPresenter">                             
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Allocated}" Value="FinanciallyAllocated">
                                <Setter Property="ContentTemplate"  Value="{StaticResource MyTemplate}" />
                            </DataTrigger>                                                          
                        </Style.Triggers>
                    </Style>
                </ContentPresenter.Style>
            </ContentPresenter>            
        </Grid>      
    </DataTemplate>

【问题讨论】:

    标签: wpf contentpresenter


    【解决方案1】:

    可能没有任何显示,因为您的contentPresenter 中没有设置任何内容?

    ps:看起来您有很多与您的问题无关的代码(椭圆样式,许多模板)。浏览所有这些代码需要一段时间,所以我会要求删除不需要的代码。

    【讨论】:

    • 我想要做的是将 DataTemplate (MyTemplate) 设置为内容演示者的内容 - 但显然我需要提供一个可以接受 ContentTemplate 的控件。如果我想显示 MyTemplate 的内容,我将在此处使用哪个控件?
    • @Jeremy,嗯,这个用法看起来是正确的。 ContentPresenter 应该接受 DataTemplateContentPresenter 的唯一替代品是 ContentControl,但我怀疑此更改会对您有所帮助。
    • 不幸的是,这正是问题所在——DataTemplate 没有出现。我会去 ContentControl 看看是否有什么不同。无论如何,我重构了整个东西,因此它不再使用 DataTemplate - 但它现在看起来非常混乱:) 如果我像第一个示例那样绑定到 Button,我得到了正确的绑定,所以我知道DataTrigger 正在工作,但如上所述,我只希望 DataTemplate 的内容出现在网格内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2011-11-03
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多