2012-01-27 115 views
4

如何將GridViewColumn中的WPF CheckBox居中?如何將GridViewColumn中的WPF CheckBox居中?

<GridViewColumn Width="60" Header="Success"> 
    <GridViewColumn.CellTemplate> 
     <DataTemplate> 
       <CheckBox IsChecked="{Binding Path=IsSuccess}">             
       </CheckBox> 
      </DataTemplate> 
    </GridViewColumn.CellTemplate> 
</GridViewColumn> 

更新:

這個解決方案正常工作,但它破壞了表達暗主題爲細胞... (我不知道知道如何解決它...)

<ListView Name="HistoryTable" Width="783" VerticalAlignment="Stretch" VerticalContentAlignment="Top" Height="195"> 
          <ListView.ItemContainerStyle> 
           <Style TargetType="ListViewItem"> 
            <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
           </Style> 
          </ListView.ItemContainerStyle> 
          <ListView.Resources> 
           <Style TargetType="{x:Type CheckBox}" x:Key="DataGridCheckBox"> 
            <Setter Property="HorizontalAlignment" Value="Center" /> 
            <Setter Property="HorizontalContentAlignment" Value="Center" /> 
            <Setter Property="IsEnabled" Value="False" /> 
            <Setter Property="Margin" Value="4" /> 
            <Setter Property="VerticalAlignment" Value="Center" /> 
            <Setter Property="VerticalContentAlignment" Value="Center" /> 
            <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type GridViewColumn}},Path=ActualWidth}" /> 
           </Style> 
          </ListView.Resources> 
          <ListView.View>          
           <GridView> 
            <GridViewColumn DisplayMemberBinding="{Binding Filename}" Width="140" Header="{lex:LocTextExtension Key=Name, Dict=Resources, Assembly=PreShow.Player}" /> 
            <GridViewColumn Width="60" Header="{lex:LocTextExtension Key=Success, Dict=Resources, Assembly=PreShow.Player}"> 
             <GridViewColumn.CellTemplate> 
              <DataTemplate> 
               <DockPanel HorizontalAlignment="Center"> 
                <CheckBox IsChecked="{Binding Path=IsSuccess}" Style="{StaticResource DataGridCheckBox}">             
                </CheckBox> 
               </DockPanel> 
              </DataTemplate> 
             </GridViewColumn.CellTemplate> 
            </GridViewColumn> 

enter image description here

+0

哦,我在這裏混淆了事情。我已經刪除了答案。對不起,噪音。 – Clemens 2012-01-27 13:55:37

+0

遵循H.B.的答案。 - 您錯過了BasedOn屬性 – hyp 2012-01-27 15:02:45

回答

4

這應該是裸骨設置:

<ListView.Resources> 
    <!-- Alternatively use the ItemContainerStyle of course --> 
    <Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}"> 
     <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
    </Style> 

    <!-- Use BasedOn to preserve existing style --> 
    <Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}"> 
     <Setter Property="VerticalAlignment" Value="Center"/> 
     <Setter Property="HorizontalAlignment" Value="Center"/> 
    </Style> 
</ListView.Resources> 
+0

您的解決方案有效,但會破壞表情黑暗主題。我將更新我的帖子來放置截圖發生的事情。 – 2012-01-27 15:09:59

+0

@DmitryBoyko:你不可能猜到要在ListViewItem中添加一個'BasedOn'? – 2012-01-27 15:11:12

+0

我不是WPF的大師:)請告訴我必須在Expression Dark Theme中添加它嗎? – 2012-01-27 15:14:12

6

將新Grid的列寬(在DataTemplate中)綁定到GridView列寬度的寬度。

<GridViewColumn Header="Header="{StaticResource CheckBoxHeaderText}" x:Name="CheckBoxColumn" Width="125"> 
    <GridViewColumn.CellTemplate> 
     <DataTemplate> 
      <Grid Width="{Binding ElementName=CheckBoxColumn, Path=Width}"> 
       <CheckBox HorizontalAlignment="Center" IsChecked="{Binding IsCheckBoxChecked, Mode=OneWay}" /> 
      </Grid> 
     </DataTemplate> 
    </GridViewColumn.CellTemplate> 
</GridViewColumn> 
相關問題