【发布时间】:2013-08-28 21:17:27
【问题描述】:
我的想法是:我需要在 DataGridTemplateColumn 中添加一个展开/折叠按钮,以允许用户展开单元格以查看完整内容,或者将其折叠以仅查看第一行。这是我的模板:
<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 是一个简单的UserControl,单击时会在 + 和 - 图标之间切换。它有一个名为IsExpanded 的读/写bool 属性。
我现在正在尝试将一个简单的Trigger 添加到ExpandCollapseButton 的触发器中,这将简单地将TextBlock 的TextTrimming 属性设置为WordEllipsis 在折叠状态下并将其设置为None处于展开状态,但无法找出正确的方法。我尝试在上面的展开控件下添加以下代码:
<local:ExpandCollapseButton.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="MyTB" Property="TextTrimming" Value="None" />
</Trigger>
</local:ExpandCollapseButton.Triggers>
但这会给出错误提示 Cannot find the static member 'IsExpandedProperty' on the type 'ContentPresenter',我无法理解。
【问题讨论】:
-
您不能将触发器放在这样的 UI 元素中。您只能将
EventTriggers添加到 UI 元素的触发器集合中。使用DataTemplate.Triggers或其他东西。 -
@HighCore:你能举个例子吗?
标签: .net wpf xaml datagrid triggers