2017-08-21 41 views
0

我有一個datagrid,顯示有關期末開始和結束的值。爲此,我創建了一個包含2個文本框的列模板,但在最後一行中,我將這些值的差異顯示爲百分比。我想要做的是將最後一行中的第一個文本塊居中,因爲在該行中只有一個值。WPF C#Datagrid更改最後一行佈局

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

謝謝。

+0

這是處理辦法的變通方法。你可以使用觸發器。最常用的技術是數據模板選擇器 – Ramankingdom

回答

1

這是解決辦法,以觸發打 (1)創建第三個屬性。當顯示其他兩個將不會顯示。保持它爲空,直到它沒有被計算。我暫時把它命名爲differencofvalues。給出適當的名稱並加以約束

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

我用我需要的配置創建了另一個屬性。由於我沒有其他屬性的值,所以它很合適。

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