c# - 列表框对齐 WPF 内的复选框

标签 c# wpf xaml checkbox listbox

我在我的列表框中添加了复选框,但它们没有按我想要的那样对齐。这是我的 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>

显示了复选框,但问题是它们“粘附”在我的文本 block 的内容上。我想将我的复选框移动到列表框的右边缘,并希望它们像将复选框列添加到数据网格或 ListView 时一样对齐。如何管理?

更新:

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

最佳答案

你的布局是错误的。 StackPanel Orientation="Horizo​​ntal" 不会水平拉伸(stretch)到它的容器。

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

还要确保您的 ListBoxItem 已拉伸(stretch):

编辑:

我的意思是您将此样式设置为 ListBox.ItemContainerStyle,如下所示:

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

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

否则,ListBox 会“认为”您试图将 Style 设置为其中的 Item

关于c# - 列表框对齐 WPF 内的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20617916/

相关文章:

c# - Entity Framework 如何遍历一个太大的集合?

c# - Prism 区域在内容区域中没有变化 - WPF

c# - RibbonControl 正在拉伸(stretch)图像

c# - 将模板选择器中的 DataTemplate 设置为动态资源

c# - wpf 按字母顺序排序组合框项目

c# - 基于角色的安全性 asp.net mvc

c# - 无法通过 ManagementObjectCollection 进行 foreach,引发 "This method is not implemented in any class"异常

c# - 从内部派生的公共(public)接口(interface)?

c# - TabControl WPF MVVM中的延迟加载(仍然)

c# - Preview Mouse Down 事件在第一次调用时返回带有空引用 CurrentColumn 的发件人