1
<Grid Grid.IsSharedSizeScope="True" Name="treeGrid" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.Resources>
        <local:LevelConverter x:Key="levelConverter" />
        <HierarchicalDataTemplate ItemsSource="{Binding Items}"
                              DataType="{x:Type local:DirectoryRecord}">
            <Grid ShowGridLines="False">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="rowHeaderColumn"/>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition SharedSizeGroup="column1"/>
                    <ColumnDefinition SharedSizeGroup="column2"/>
                    <ColumnDefinition SharedSizeGroup="column3"/>
                    <ColumnDefinition SharedSizeGroup="column4"/>
                    <ColumnDefinition SharedSizeGroup="column5"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0"
                       Text="{Binding Name}"></TextBlock>
                <Rectangle Grid.Column="1">
                    <Rectangle.Width>
                        <MultiBinding Converter="{StaticResource levelConverter}">
                            <Binding Path="Level"></Binding>
                            <Binding ElementName="treeViewItemToMeasure" Path="ActualWidth"></Binding>
                        </MultiBinding>
                    </Rectangle.Width>
                </Rectangle>
                <TextBlock Grid.Column="2"
                       Text="{Binding LastAccessed}"></TextBlock>
                <TextBlock Grid.Column="3"
                       Text="{Binding Files.Count}"></TextBlock>
                <TextBlock Grid.Column="4" 
                       Text="{Binding Inherited}"></TextBlock>
                <Grid.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Background" Value="Aquamarine" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Inherited}" Value="True">
                                <Setter Property="Background" Value="Black" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>
            </Grid>
        </HierarchicalDataTemplate>
    </Grid.Resources>
    <TreeView>
        <!-- My binding goes here -->
    </TreeView>
</Grid>

我正在尝试更改自定义网格(TreeGrid)中包含的 TextBlock 的背景,但是此代码因 XamlParseException 而失败

“TextBlock”TargetTypes 与元素“Grid”的类型不匹配。

4

1 回答 1

3

而不是在节内定义样式Grid.Style,您必须将其声明为Grid.Resource Style, 并TargetType指定,即

<Grid.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="Aquamarine" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Inherited}" Value="True">
                <Setter Property="Background" Value="Black" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Grid.Resources>
于 2012-10-25T11:39:16.410 回答