Как вы привязываете ContentTemplate непосредственно к Grid? - PullRequest
0 голосов
/ 23 февраля 2011

В следующем XAML я пытаюсь привязать различные шаблоны данных непосредственно к 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>

1 Ответ

0 голосов
/ 23 февраля 2011

Может быть, ничего не появляется, потому что в вашем contentPresenter?

не установлено никакого контента

p.s .: похоже, у вас много кода, который не имеет отношения к вашей проблеме (стили эллипса, много шаблонов). Чтобы пройти весь этот код, требуется время, поэтому я бы попросил удалить ненужный код.

...