views:

158

answers:

1

Hi,

I've got a datagrid with a datatrigger set. When it's value is "UP" I'd like the trigger to only change the background in the 'directionColumn' datagridcolumn only.

What i've got so far updates the entire row. I also tried placing 'targetname' in the setter element but that just gives an error. Please help

Thanks

the xaml:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Name="summaryReportDataGridDATE" RowDetailsVisibilityMode="VisibleWhenSelected" Width="330">
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Direction}" Value="UP">
                                <Setter>
                                    <Setter.Property>Background</Setter.Property>
                                    <Setter.Value>Red</Setter.Value>
                                <!--<Setter Property="Background" Value="Red" TargetName="directionColumn"/>-->
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.Columns>
                    <!--<DataGridTemplateColumn x:Name="dateColumn" Header="Date" Width="100" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <DatePicker SelectedDate="{Binding Path=Date, Mode=OneWay}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>-->

                    <DataGridTextColumn x:Name="dateColumn" Binding="{Binding Path=Date, StringFormat={}{0:dd/MM/yyyy}}" Header="Date"  Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT"/>
                    <DataGridTextColumn x:Name="closeColumn" Binding="{Binding Path=ClosingPrice}" Header="ClosingPrice" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
                    <DataGridTextColumn x:Name="directionColumn" Binding="{Binding Path=Direction}" Header="Direction"  Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
                    <DataGridTextColumn x:Name="resultColumn" Binding="{Binding Path=Result}" Header="Result"  Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT"  />
                </DataGrid.Columns>
            </DataGrid>
+1  A: 

How about a TemplateColumn instead of custom cell style:

    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Name="summaryReportDataGridDATE" RowDetailsVisibilityMode="VisibleWhenSelected" Width="330">                       
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="dateColumn" Binding="{Binding Path=Date, StringFormat={}{0:dd/MM/yyyy}}" Header="Date"  Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT"/>
            <DataGridTextColumn x:Name="closeColumn" Binding="{Binding Path=ClosingPrice}" Header="ClosingPrice" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
            <DataGridTemplateColumn x:Name="directionColumn" Header="Direction"  Width="80" CanUserReorder="False" CanUserSort="False" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="directionColumnTextBlock" Text="{Binding Path=Direction}" Width="80" FontFamily="Arial Rounded MT"/>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=Direction}" Value="UP">
                                <Setter TargetName="directionColumnTextBlock" Property="Background" Value="Red" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>                                                                              
            <DataGridTextColumn x:Name="resultColumn" Binding="{Binding Path=Result}" Header="Result"  Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT"  />
        </DataGrid.Columns>
    </DataGrid>
greedydita
Great! thank you.
Chris