Valerio
Valerio

Reputation: 3615

WPF DataGridTextColumn binding and style

here's the deal: styling the DataGridTextColumn's textblock for a datagrid. I need to format the textblock by it's value, by comparing it from another binded value.

What i want to achieve is something like this:

<Style x:Key="ExpeditionerCellStyle" BasedOn="{StaticResource RightAlignStyle}" TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=BEST}" Value="{RelativeSource Mode=Self}">
            <Setter Property="Background" Value="Green" />
        </DataTrigger>
    </Style.Triggers>
</Style> 

Which is not possible, because the Value of Datatrigger cannot be a relative source.

So i tried with multibinding

<Style x:Key="ExpeditionerCellStyle" BasedOn="{StaticResource RightAlignStyle}" TargetType="{x:Type TextBlock}">
 <Style.Triggers>
  <DataTrigger Value="True">
    <DataTrigger.Binding>
      <MultiBinding Converter="{StaticResource IsValueEqualParameterConverter}">
        <Binding Path="BEST" />
        <Binding RelativeSource="{RelativeSource Mode=Self}" />
      </MultiBinding>
     </DataTrigger.Binding>
    </DataTrigger>
   </Style.Triggers>
</Style>

with no luck: textblock Text property is empty, probably because the style is applied before the actual binding is performed.

I have no more ideas.

Please help me!

Upvotes: 0

Views: 1243

Answers (3)

Valerio
Valerio

Reputation: 3615

I ended modifying a bit my code. Instead of relying on the value of the cell, in the field "BEST" i passed the name of the column. Then using the converter I check Current textblock (as suggested by @Sheridan) -> DatagridCell -> Column's header if it match, i change the style on this textblock.

Thanks everyone!!!

Upvotes: 0

Sheridan
Sheridan

Reputation: 69985

I'm not sure if this will work, but I noticed that you forgot to specify the Binding.Path in your MultiConverter example... try this:

<Style x:Key="ExpeditionerCellStyle" BasedOn="{StaticResource RightAlignStyle}" TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <DataTrigger Value="True">
            <DataTrigger.Binding>
                <MultiBinding Converter="{StaticResource IsValueEqualParameterConverter}">
                    <Binding Path="BEST" />
                    <Binding Path="Text" RelativeSource="{RelativeSource Mode=Self}" />
                </MultiBinding>
            </DataTrigger.Binding>
        </DataTrigger>
    </Style.Triggers>
</Style>

Upvotes: 1

Jimmy.Bystrom
Jimmy.Bystrom

Reputation: 297

If you only want to change the style of the textblock based on the content of the textblock why not just add a bidning to the textblock background and use a converter to check the content of the text?

In other words bind both the text and the background to the same source and use a converter with the background and in the converter match the content and return the correct background style.

If you need something else leave a comment so we better know what the problem is.

Upvotes: 0

Related Questions