简体   繁体   中英

WPF C# Datagrid change last row layout

I have a datagrid where I show values about the beginning and the end of a period. For that I created a column template with 2 textblocks, but in the last row I show a diference between these values as percentage. What I want to do is to centerlize the first text block in the last row as I have only one value in that row.

<DataGridTemplateColumn>
    <DataGridTemplateColumn.Header>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Officer11Nome}"
                       HorizontalAlignment="Center" 
                       Grid.Column="0"
                       Grid.Row="0"
                       Grid.ColumnSpan="3"
                       />
            <TextBlock Text="Abertura" 
                       HorizontalAlignment="Center" 
                       Grid.Column="0"
                       Grid.Row="1"
                       />

            <TextBlock Text="Fechamento" 
                       HorizontalAlignment="Center" 
                       Grid.Column="2"
                       Grid.Row="1"
                       />
        </Grid>
    </DataGridTemplateColumn.Header>

    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Officer1Abertura}" Margin="2,0,2,0" TextAlignment="Left" Grid.Column="0"/>
                <TextBlock Text="{Binding Officer1Fechamento}" Margin="2,0,2,0" TextAlignment="Right" Grid.Column="2"/>
            </Grid>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Thanks.

This is workaround playing with trigger (1) Create third property . When its shown the other two will not be shown. Keep it null till the time its not calculated. for the time being i name it as differencofvalues. Give appropriate name and bind

<DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Name="textbox1" Text="{Binding Officer1Abertura}" Margin="2,0,0,0" TextAlignment="Center" Width="Auto" />
                    <TextBlock Text="{Binding differenceOfValues}" Margin="0,0,0,0" TextAlignment="Center" Width="Auto">
                        <TextBlock.Style>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="Visibility" Value="Visible"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding differenceOfValues}" Value="{x:Null}">
                                        <Setter Property="Visibility" Value="Collapsed"></Setter>
                                        <Setter Property="{Binding ElementName=textbox1, Path=Visibility}" Value="Visible"></Setter>
                                        <Setter Property="{Binding ElementName=textbox2, Path=Visibility}" Value="Visible"></Setter>
                                    </DataTrigger>
                                    <Trigger Property="Visibility" Value="Visible">
                                        <Setter Property="{Binding ElementName=textbox1, Path=Visibility}" Value="Collapsed"></Setter>
                                        <Setter Property="{Binding ElementName=textbox2, Path=Visibility}" Value="Collapsed"></Setter>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                    <TextBlock Name="textbox2" Text="{Binding Officer1Fechamento}" Margin="10,0,2,0" TextAlignment="Center" Width="Auto" Visibility="Collapsed" />
                </StackPanel>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>

I created another property with the configurations that I needed. Since I dont have values in the others properties it fits well.

<TextBlock Text="{Binding Officer1Percentagem}" TextAlignment="Center" Grid.Column="0" Grid.ColumnSpan="3"/>
<TextBlock Text="{Binding Officer1Abertura}" TextAlignment="Right" Grid.Column="0"/>
<TextBlock Text="{Binding Officer1Fechamento}" TextAlignment="Right" Grid.Column="2"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM