带有DockPanel对齐的ListBox

时间:2013-07-10 15:19:03

标签: c# wpf listbox dockpanel

我有一个listbox我每行传递5个项目。 Xml文件:

                        <ListBox x:Name="DatabaseBox">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                            </Style>
                        </ListBox.ItemContainerStyle>
                            <ListBox.ItemTemplate>
                        <DataTemplate>
                                <DockPanel">
                                    <Image x:Name="ToggleFavoriteImage" Width="15" Height="15" Tag="{Binding Tag}" Source="{Binding ImageSource}" MouseLeftButtonDown="ToggleFavoriteImage_MouseLeftButtonDown"/>
                                    <TextBlock x:Name="ListBoxSName" Width="220" Text="{Binding Name}" Margin="30,0,0,0" FontSize="11" MouseLeftButtonDown="ListBoxSName_MouseLeftButtonDown"/>
                                    <Button x:Name="ListBoxSCoyntry" Height="17" Content="{Binding City}" FontSize="11" Click="ListBoxSCity_Click"/>
                                    <Button x:Name="ListBoxSCity"  Height="17" Content="{Binding Genre}" FontSize="11" Click="ListBoxSGenre_Click"/>
                                    <Button x:Name="ListBoxSGenre" Height="17" Content="{Binding Country}" FontSize="11" Click="ListBoxSCountry_Click"/>
                                </DockPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

                    </ListBox>

我想要做的是让ImageTextBlock停靠在左边,其他所有其他人并排停靠。 我为所有DockPanel.Dock ="Rigth"尝试了Buttons但事情变得非常混乱。 有想法的人吗?

2 个答案:

答案 0 :(得分:4)

将每个可视组包装在堆栈面板中,并将对接应用到堆栈面板,如下所示:

<DockPanel>
    <StackPanel DockPanel.Dock="Left">
         <Image x:Name="ToggleFavoriteImage" Width="15" Height="15" Tag="{Binding Tag}" Source="{Binding ImageSource}" MouseLeftButtonDown="ToggleFavoriteImage_MouseLeftButtonDown"/>
         <TextBlock x:Name="ListBoxSName" Width="220" Text="{Binding Name}" Margin="30,0,0,0" FontSize="11" MouseLeftButtonDown="ListBoxSName_MouseLeftButtonDown"/>
    </StackPanel>
    <StackPanel DockPanel.Dock="Right">
         <Button x:Name="ListBoxSCoyntry" Height="17" Content="{Binding City}" FontSize="11" Click="ListBoxSCity_Click"/>
         <Button x:Name="ListBoxSCity"  Height="17" Content="{Binding Genre}" FontSize="11" Click="ListBoxSGenre_Click"/>
         <Button x:Name="ListBoxSGenre" Height="17" Content="{Binding Country}" FontSize="11" Click="ListBoxSCountry_Click"/>
    </StackPanel>
</DockPanel>

答案 1 :(得分:0)

使用GridDockPanel不是一个好的控制。此外Grids还有size-sharing,这通常对项目控件有用。