我在列表框中添加了复选框,但它们没有按照我的意愿对齐。这是我的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>
答案 0 :(得分:2)
你的布局错了。 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>
还要确保ListBoxItem
被拉伸:
修改强>
我的意思是您将此样式设置为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
。
答案 1 :(得分:-1)
设定保证金
<CheckBox Grid.Column="2" Name="IsDone" Margin="10,0,0,0"/>
或者
<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>