Hi,
I would like to change the text alignement in a cell. My first idea is to set the Template for CellValuePresenter and define my own TextBlock. It works but I loose much of the functionality of the XamDataGrid. Editing is no more possible (without coding), selection, ...
So I took a look in your samples an tried this:
<Style TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Foreground" Value="{DynamicResource GeneralForeground}" />
<Setter Property="Background" Value="{DynamicResource GeneralBackground}" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
The Text is centered but the Background color no more fills the whole cell. Then I tried this:
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
Now the background color fills the whole cell but the text is only centered when editing. I think I'm missing something...
Could someone help me?
Thanks,
Daniel
PS: In the preview the code is not easily readable... How shoud l insert code snippets correctly in the forum?
Hi Stefan,
Thanks for your answer. My problem is that the styles are in a ResourceDictionary in another assembly which I references. So I can't add an event handler. Is there another way to do that ?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:igDP="http://infragistics.com/DataPresenter"> <Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="LastFieldCellPresenter"> <Setter Property="Foreground" Value="{DynamicResource GeneralForeground}" /> <Setter Property="Background" Value="{DynamicResource GeneralBackground}" /> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="BorderBrush" Value="{DynamicResource GeneralForeground}" /> <Setter Property="BorderThickness" Value="1,0,0,0" /> </Style> <Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="DefaultCellValuePresenter"> <Setter Property="Foreground" Value="{DynamicResource GeneralForeground}" /> <Setter Property="Background" Value="{DynamicResource GeneralBackground}" /> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="BorderBrush" Value="{DynamicResource GeneralForeground}" /> <Setter Property="BorderThickness" Value="1,0,0,1" /> </Style> <Style TargetType="{x:Type igDP:LabelPresenter}" x:Key="DefaultLabelPresenter"> <Setter Property="Foreground" Value="{DynamicResource GeneralForeground}" /> <Setter Property="Background" Value="{DynamicResource GeneralBackground}" /> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="BorderBrush" Value="{DynamicResource GeneralForeground}" /> <Setter Property="BorderThickness" Value="1,0,0,1" /> </Style> <Style TargetType="{x:Type igDP:CellValuePresenter}" BasedOn="{StaticResource DefaultCellValuePresenter}" /> <Style TargetType="{x:Type igDP:LabelPresenter}" BasedOn="{StaticResource DefaultLabelPresenter}" /> </ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:igDP="http://infragistics.com/DataPresenter">
<Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="LastFieldCellPresenter"> <Setter Property="Foreground" Value="{DynamicResource GeneralForeground}" /> <Setter Property="Background" Value="{DynamicResource GeneralBackground}" /> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="BorderBrush" Value="{DynamicResource GeneralForeground}" /> <Setter Property="BorderThickness" Value="1,0,0,0" /> </Style> <Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="DefaultCellValuePresenter"> <Setter Property="Foreground" Value="{DynamicResource GeneralForeground}" /> <Setter Property="Background" Value="{DynamicResource GeneralBackground}" /> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="BorderBrush" Value="{DynamicResource GeneralForeground}" /> <Setter Property="BorderThickness" Value="1,0,0,1" /> </Style> <Style TargetType="{x:Type igDP:LabelPresenter}" x:Key="DefaultLabelPresenter"> <Setter Property="Foreground" Value="{DynamicResource GeneralForeground}" /> <Setter Property="Background" Value="{DynamicResource GeneralBackground}" /> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="BorderBrush" Value="{DynamicResource GeneralForeground}" /> <Setter Property="BorderThickness" Value="1,0,0,1" /> </Style>
<Style TargetType="{x:Type igDP:CellValuePresenter}" BasedOn="{StaticResource DefaultCellValuePresenter}" /> <Style TargetType="{x:Type igDP:LabelPresenter}" BasedOn="{StaticResource DefaultLabelPresenter}" />
</ResourceDictionary>
Hello,
Thank you for your post. I have been looking into it and I can suggest you create a Style for the LabelPresenter and add EventSetter for its Loaded event like this:
<Style TargetType="{x:Type igDP:LabelPresenter}"> <EventSetter Event="Loaded" Handler="lpLoaded"/> </Style>
And add the following code ion the code behind:
private void lpLoaded(object sender, RoutedEventArgs e) { (Utilities.GetDescendantFromType(sender as DependencyObject, typeof(TextBlock), true) as TextBlock).HorizontalAlignment = HorizontalAlignment.Center; }
Also here you can see how to paste your code in the community:
http://community.infragistics.com/forums/p/28814/327078.aspx
Hope this helps you.
I found a solution:
I tried the same for the LabelPresenter but it doesn't work. Any idea?