2012-01-27 5 views
4

Как центрировать WPF CheckBox внутри GridViewColumn?Как центрировать WPF CheckBox внутри GridViewColumn?

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

ОБНОВЛЕНИЕ:

Это решение отлично работает, но он разрушает Expression Dark тему для клеток ... (я не знаю, знают, как это исправить ...)

<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

+0

следуйте ответам H.B. - вам не хватает свойства BasedOn – hyp

ответ

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

Вы решаете работу, но уничтожает тему темных выражений. Я собираюсь обновить свой пост, чтобы поставить снимок экрана, что происходит. –

+0

@DmitryBoyko: Вы не могли угадать, чтобы добавить 'BasedOn' в' ListViewItem'? –

+0

Я не большой мастер WPF :) Пожалуйста, расскажите, где я должен добавить его для Expression Dark Theme? –

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> 
Смежные вопросы