C# 列表框对齐WPF中的复选框
我将复选框添加到我的列表框中,但它们没有按照我的意愿对齐。这是我的C# 列表框对齐WPF中的复选框,c#,wpf,xaml,checkbox,listbox,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=PropertyChang
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>
复选框会显示出来,但问题是它们“粘住”了我的文本块的内容。我想将复选框移动到列表框的右边缘,并希望它们像向datagrid或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>
设置边距
<CheckBox Grid.Column="2" Name="IsDone" Margin="10,0,0,0"/>
或
您的布局错误。
StackPanel Orientation=“Horizontal”
不会水平延伸到其容器
通过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>
否则,列表框将“认为”您试图将
样式设置为其中的项。-1不符合OP的要求“我想将我的复选框移动到列表框的右边缘”。看到我的答案了。我认为问题是复选框没有空间对齐,所以OP需要给它一些宽度/边距来对齐到右边或左边。不,当然没有。在WPF中硬编码大小几乎总是被认为是不好的做法和不必要的。我用DockPanel更改了StackPanel,但我仍然有同样的问题。@stojdza请确保ListBoxItems被拉伸。还要确保正确设置了DockPanel.Dock属性,并且文本框
是最后一个子项。否则,发布您当前的XAML。抱歉,没有看到您关于拉伸的更新。我更改了代码,但现在,我的文本块的内容不可见,只有图像和复选框,我的复选框仍然没有移到右侧。我用新的xaml更新了我的问题。天哪,我完全错过了那个部分?!谢谢你的回答,这正是我想要的!以前从未使用过DocPanel。
<DockPanel>
<Image DockPanel.Dock="Left" Source="/WPFPanErpLite;component/Images/bullet.png" />
<CheckBox DockPanel.Dock="Right" />
<TextBlock Text="{Binding Text}" FontSize="15" Foreground="#003366" />
</DockPanel>
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<!-- ... -->
</ListBox>