简体   繁体   English

列表框对齐方式WPF中的复选框

[英]Checkboxes inside listbox alignment WPF

I added checkboxes to my listbox but they are not aligned as I want to. 我将复选框添加到了列表框中,但是它们并没有按照我的意愿对齐。 Here is my XAML : 这是我的XAML

<ListBox ItemsSource="{Binding Path= Reminders}" Grid.Row="2" Height="250" Width="250" Name="reminderListBox" HorizontalAlignment="Left" SelectedItem="{Binding Path=Reminder, UpdateSourceTrigger=PropertyChanged}" >                                   
   <ListBox.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal">                                               
            <Image Grid.Column="0" Source="/WPFPanErpLite;component/Images/bullet.png" />
            <TextBlock Grid.Column="1" Text="{Binding Text}" FontSize="15"  Foreground="#003366" />
            <CheckBox Grid.Column="2" Name="IsDone" HorizontalAlignment="Right" />                                                
         </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

Checkboxes are shown but problem is that they "stick" to content of my textblock. 显示了复选框,但问题是它们“粘”在我的文本块的内容上。 I want to move my checkboxes to right edge of my listbox, and want them to be aligned like when you add checkbox column to datagrid or listview. 我想将复选框移动到列表框的右侧,并希望它们像将复选框列添加到datagrid或listview一样对齐。 How to manage this? 如何处理?

UPDATE: 更新:

<ListBox ItemsSource="{Binding Path= Reminders}" Grid.Row="2" Height="250" Width="250" Name="reminderListBox" HorizontalAlignment="Left" SelectedItem="{Binding Path=Reminder, UpdateSourceTrigger=PropertyChanged}" >
                                <Style TargetType="ListBoxItem">
                                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                                </Style>
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <DockPanel>
                                            <Image DockPanel.Dock="Left" Source="/WPFPanErpLite;component/Images/bullet.png" />                                                
                                            <CheckBox DockPanel.Dock="Right"  Name="IsDone" HorizontalAlignment="Right" />
                                            <TextBlock Text="{Binding Text}" FontSize="15"  Foreground="#003366" />
                                        </DockPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>

Your layout is wrong. 您的布局错误。 a StackPanel Orientation="Horizontal" will NOT stretch horizontally to it's container. StackPanel Orientation="Horizontal"不会水平延伸到其容器。

Change that by a DockPanel : 通过DockPanel更改:

 <DockPanel>                                               
     <Image DockPanel.Dock="Left" Source="/WPFPanErpLite;component/Images/bullet.png" />
     <CheckBox DockPanel.Dock="Right" />                                                
     <TextBlock Text="{Binding Text}" FontSize="15"  Foreground="#003366" />
 </DockPanel>

Also make sure your ListBoxItem s are stretched: 还要确保您的ListBoxItem被拉伸:

Edit: 编辑:

I meant that you set this style as the ListBox.ItemContainerStyle , like so: 我的意思是将这种样式设置为ListBox.ItemContainerStyle ,如下所示:

<ListBox>
    <ListBox.ItemContainerStyle>
       <Style TargetType="ListBoxItem">
           <Setter Property="HorizontalAlignment" Value="Stretch"/>
           <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
       </Style>
    </ListBox.ItemContainerStyle>

    <!-- ... -->
</ListBox>

Otherwise, the ListBox will "think" that you're trying to set the Style as an Item inside it. 否则,ListBox将“认为”您正在尝试将Style设置为其中的Item

Set Margin 设置保证金

<CheckBox Grid.Column="2" Name="IsDone" Margin="10,0,0,0"/>

Or 要么

<Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="20"/>
                    </Grid.ColumnDefinitions>
                    <Image Grid.Column="0" Source="/WPFPanErpLite;component/Images/bullet.png" />
                    <TextBlock Grid.Column="1" Text="{Binding OwnerId}" FontSize="15"  Foreground="#003366" />
                    <CheckBox Grid.Column="2" Name="IsDone" HorizontalAlignment="Right" Background="Red"/>
                </Grid>

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

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