【问题标题】:How to add style to datagridtemplatecolumn (WPF XAML)如何将样式添加到 datagridtemplatecolumn (WPF XAML)
【发布时间】:2014-12-05 13:19:36
【问题描述】:

如何将样式添加到 datagridtemplatecolumn (WPF,XAML) 我有一个数据网格,我在其中使用 datagridtemplate 列,因为在其中我需要一个文本块和一个按钮。 文本块从另一个工作正常的视图中获取其数据。但是文本块无法进行任何滚动/文本换行,因为我无法向文本块添加任何样式。 因此,当此文本块获取多行数据时,只有一行可见。

我正在寻找类似于datagridtextcolumn.elementstyle的功能。请指教。

【问题讨论】:

  • 请尽可能多地分享您拥有的信息(例如相关代码块)
  • 向 datagrid.cellstyle 添加样式,现在一切正常。如果还有其他方法可以做到这一点,请发布。

标签: c# wpf xaml wpf-controls


【解决方案1】:

我使用 DataGridTemplateColumn 将垃圾桶的图像放置在网格的每一行上。在本例中,您可以放置​​一个带有图像作为其内容的 Button,而不是 Image。当用户单击图像时,它将从可观察集合中删除该行。 您将需要添加 xmlns:i="clr-amespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform" 以便使用交互。但是您可以设置文本块、单元格和图像/按钮的样式,如下面的 Xaml 中所示。 希望这可以帮助。

<DataGridTemplateColumn MinWidth="30" IsReadOnly="True">      
<DataGridTemplateColumn.CellTemplate>       
<DataTemplate>
<DataGridCell Style="{StaticResource YOURCellStyle}" >
<TextBlock Style="{StaticResource YOURTextBlockStyle}">
<Image Width="12"
Margin="0,0,0,0"
HorizontalAlignment="Center"                                
Source="pack://application:,,,/Resources/Images/DialogIcons/rubbish-bin.PNG"
Stretch="Uniform"
Style="{StaticResource YOURImageStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand Command="{Binding 
Path=DataContext.YOURDeleteComand, ElementName=YOURDataGrid}" 
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</TextBlock>
</DataGridCell>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

<Style x:Key="YourImageOrButtonStyle" TargetType="ImageOrButton">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="ToolTip" Value="Delete"/>
</Style>
<Style x:Key="YOURCellStyle" TargetType="DataGridCell">
<Setter Property="Background" Value="Transparent"/>    
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>     
</Style>
<Style x:Key="YOURTextBlockStyle" TargetType="TextBlock">
<Setter Property="Background" Value="Transparent"/>    
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>

【讨论】:

    猜你喜欢
    • 2020-07-27
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    相关资源
    最近更新 更多