简体   繁体   English

WPF边框和标题对齐

[英]WPF Border and Header alignment

Got two problems here in my XAML code. 我的XAML代码中有两个问题。

<Grid Margin="20">

        <ListBox x:Name="lbChampToSelect" ItemsSource="{Binding Lst}" HorizontalContentAlignment="Stretch" Grid.IsSharedSizeScope="True">
        <Border Background="FloralWhite" BorderBrush="Silver" BorderThickness="1" CornerRadius="3,3,3,3">                
            <ListBox.Template>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                    <Grid Margin="5,2" DockPanel.Dock="Top" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100" SharedSizeGroup="Col1"></ColumnDefinition>
                            <ColumnDefinition Width="100" SharedSizeGroup="Col1"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Label Grid.Column="0">Libellé</Label>
                        <CheckBox IsChecked="False" Grid.Column="1"/>
                    </Grid>
                    <ItemsPresenter></ItemsPresenter>
                    </DockPanel>
                </ControlTemplate>                    
            </ListBox.Template>

            <ListBox.ItemTemplate>                    
                <DataTemplate>
                    <Grid Margin="5,2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition  Width="100" SharedSizeGroup="Col1"/>
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Margin="0,0,10,0" Text="{Binding Libelle }" Grid.Column="0" />
                        <CheckBox IsChecked="False" Grid.Column="1"/>
                    </Grid>
                </DataTemplate>                    
            </ListBox.ItemTemplate>
        </Border>
    </ListBox>

</Grid>

First : whatever I try, I cannot align the header and the columns containing checkboxes (with a text labelling the checkbox it's worth). 首先:无论我做什么,我都无法使标题和包含复选框的列对齐(带有标记该复选框价值的文字)。 At the end, the header checkbox is supposed to check all the subsequents. 最后,标题复选框应该检查所有后续项。

Second : I simply try to have a border on my entire listbox including the header but I got a xaml error saying the member Template is not accessible or does not exists. 第二:我只是尝试在包括标题的整个列表框上设置边框,但出现xaml错误,提示成员模板不可访问或不存在。

Thx for your answers. 谢谢你的答案。

I'll start with the second question: 我将从第二个问题开始:

Your Border should be within the ControlTemplate (given I have guessed right how you want it to be). 您的边框应该在ControlTemplate中(假设我猜对了,您希望它的样子如何)。

<ControlTemplate>
    <Border Background="FloralWhite" BorderBrush="Silver" BorderThickness="1" CornerRadius="3,3,3,3">

Now on to the first question: There are many things that affect the layouting of the items in the ListBox. 现在转到第一个问题:有很多因素会影响ListBox中项目的布局。 Here's the method I would use to solve the problem. 这是我用来解决问题的方法。

  1. Remove margins from your Grids, I would set them to 0, at least for now. 从您的网格中删除边距,至少现在我将它们设置为0。 You can add layouting later, when you know what affects what. 当您知道什么会影响什么时,您可以稍后添加布局。
  2. Download Snoop 下载监听
  3. Start your application and start snooping it 启动您的应用程序并开始监听

  4. Find your controls in the hierarchy, and try modifying the margin/padding values. 在层次结构中找到控件,然后尝试修改边距/填充值。 That way you'll see what affects layouting. 这样,您将看到影响布局的因素。

  5. Apply new values in code. 在代码中应用新值。

Otherwise, by using a ListView you can have header for items : 否则,通过使用ListView,您可以具有items的标题:

<ListView Margin="10" x:Name="lbChampToSelect" ItemsSource="{Binding Lst}" HorizontalContentAlignment="Stretch" Grid.IsSharedSizeScope="True" BorderBrush="Silver"
                 BorderThickness="1">
<ListView.Resources>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
    <Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
        <Setter Property="HorizontalAlignment" Value="Center"/>
    </Style>
</ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Libelle" Width="120" DisplayMemberBinding="{Binding Libelle}" />
            <GridViewColumn Width="50">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{ Binding IsChecked}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
                <GridViewColumn.Header>
                    <CheckBox/>
                </GridViewColumn.Header>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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