1

Here's the idea: I need an expand/collapse button in my DataGridTemplateColumn to allow user to expand a cell to view full contents, or collapse it to only see the first line. Here's my template:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Grid HorizontalAlignment="Stretch" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <local:ExpandCollapseButton />
            <TextBlock x:Name="MyTB" Text="{Binding Body}" Grid.Column="1" TextTrimming="WordEllipsis" />
        </Grid>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

ExpandCollapseButton is a simple UserControl that toggles between + and - icons when clicked. It has got a read/write bool property named IsExpanded.

I'm now trying to add a simple Trigger to ExpandCollapseButton's triggers, which would simply set TextBlock's TextTrimming property to WordEllipsis in collapsed state and set it to None in expanded state, but can't figure out correct way of doing it. I tried adding the following code under my Expand control above:

<local:ExpandCollapseButton.Triggers>
    <Trigger Property="IsExpanded" Value="True">
        <Setter TargetName="MyTB" Property="TextTrimming" Value="None" />
    </Trigger>
</local:ExpandCollapseButton.Triggers>

but this gives error saying Cannot find the static member 'IsExpandedProperty' on the type 'ContentPresenter', which I'm unable to comprehend.

4

1 回答 1

4

将 DataTemplate 触发器与 SourceName 和 TargetName 一起使用:

<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
    <Grid HorizontalAlignment="Stretch" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <local:ExpandCollapseButton x:Name="MyButton"/>
        <TextBlock x:Name="MyTB" Text="{Binding Body}" Grid.Column="1" TextTrimming="WordEllipsis" />
    </Grid>
     <DataTemplate.Triggers>
            <Trigger Property="IsExpanded" Value="True" SourceName="MyButton">
                <Setter Property="TextBlock.TextTrimming" Value="None" TargetName="MyTB"/>
            </Trigger>
        </DataTemplate.Triggers>
</DataTemplate>

谢谢

于 2013-08-29T05:31:44.020 回答