0
votes

How to add style to datagridtemplatecolumn (WPF,XAML) I have a datagrid where I am using datagridtemplate column as inside it I need a textblock and a button. The textblock gets its data from an another view which is working fine. But the textblock is unable to have any scroll/textwrapping as I am unable to add any style to the textblock. As a result when this textblock is getting multiline data only one line is visible.

I am looking for a functionality similar to datagridtextcolumn.elementstyle.Please advise.

1
please share as much information as you have (e.g. relevant code blocks)geert3
Added style to datagrid.cellstyle and its all good now. still if there is any other way to do it,pls post.chiku coder

1 Answers

0
votes

I use a DataGridTemplateColumn to place an image of a trash can on every row of the grid. You could put a Button with an image as its content, instead of an Image in this example. When the user clicks on the image it will delete the row from the observable collection. You will need to add xmlns:i="clr-amespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform" in order to use the interaction. But you can set the styles of the textblock, the cell and the image/button as in the Xaml below. Hope this helps.

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