如何将 ContentTemplate 直接绑定到 Grid?

发布于 2024-10-18 17:25:24 字数 3134 浏览 5 评论 0 原文

在下面的 XAML 中,我尝试将各种 DataTemplate 直接绑定到 Grid ContentPresenter。我将按钮放入网格中只是为了向自己证明 ContentTemplate 已绑定并且 DataTriggers 工作正常 - 它们确实如此(请注意,此时我不需要任何类型的控件)。 如果我替换 >与 >什么也没有出现。 显然我在这里错过了一些非常简单的东西。

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

In the following XAML, I am trying to bind the various DataTemplates directly to the Grid ContentPresenter. I have put the Button inside the Grid just to prove to myself that the ContentTemplate is binding and the DataTriggers are working correctly - which they are (note I don't want any type of control at this point).
If I replace <Button> with <ContentPresenter> nothing shows up.
Obviously I'm missing something really simple here.

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

For completeness this is what I'm trying to achieve:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2024-10-25 17:25:24

也许没有显示任何内容,因为您没有在 contentPresenter 中设置任何内容?

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

Maybe nothing shows up because you do not have any content set in your contentPresenter?

p.s.: it looks like you have a lot of code which doesn't relate to the your issue (ellipse styles, many templates). It takes a while to go through all this code, so I'd ask to remove unneeded code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文