次の XAML では、さまざまな DataTemplates を Grid ContentPresenter に直接バインドしようとしています。ContentTemplate がバインドされ、DataTriggers が正しく機能していることを証明するために、Grid 内に Button を配置しました。<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>