更改自定义Grid的TextBlock

时间:2012-10-25 10:28:43

标签: wpf xaml treeview setter datatrigger

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

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

  

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

1 个答案:

答案 0 :(得分: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>