WPF中列表框的按钮对齐问题

发布于 2024-10-15 04:11:27 字数 1658 浏览 2 评论 0原文

我有一个包含像这样的 itemtemplate 的列表框

<ListBox Name="lstCompany" Grid.Column="0" MinWidth="200" Grid.Row="1" HorizontalAlignment="Stretch" Margin="2,2,2,2" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" ItemsSource="{Binding}" SelectionChanged="lstCompany_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <Grid Name="grid1" VerticalAlignment="Top" Margin="2" HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
                        <TextBlock Text="{Binding [0]}" FontWeight="Bold" />
                        <TextBlock Text="{Binding [1]}"></TextBlock>
                        <StackPanel.ToolTip>
                            <ToolTip>asdf</ToolTip>
                        </StackPanel.ToolTip>
                    </StackPanel>

                </Grid>
                <StackPanel VerticalAlignment="Top" HorizontalAlignment="Right" DockPanel.Dock="Right">
                    <Button Content="X" Tag="{Binding [2]}" Width="20" Click="btnRemove_Click" Name="btnRemove1" HorizontalAlignment="Right"></Button>
                </StackPanel>
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

该列表框位于具有拆分器的网格的第一列中。 现在的问题是我无法将按钮与列表框项目的右侧对齐。

I have a listbox containing itemtemplate like this

<ListBox Name="lstCompany" Grid.Column="0" MinWidth="200" Grid.Row="1" HorizontalAlignment="Stretch" Margin="2,2,2,2" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" ItemsSource="{Binding}" SelectionChanged="lstCompany_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <Grid Name="grid1" VerticalAlignment="Top" Margin="2" HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
                        <TextBlock Text="{Binding [0]}" FontWeight="Bold" />
                        <TextBlock Text="{Binding [1]}"></TextBlock>
                        <StackPanel.ToolTip>
                            <ToolTip>asdf</ToolTip>
                        </StackPanel.ToolTip>
                    </StackPanel>

                </Grid>
                <StackPanel VerticalAlignment="Top" HorizontalAlignment="Right" DockPanel.Dock="Right">
                    <Button Content="X" Tag="{Binding [2]}" Width="20" Click="btnRemove_Click" Name="btnRemove1" HorizontalAlignment="Right"></Button>
                </StackPanel>
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

This listbox is in first column of a grid which has a splitter.
Now problem is that I am not able to align the button to right side of listbox item.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-10-22 04:11:27

这里一个Grid就足够了。您只需要一列星号列(用于内容)和一列自动调整大小的列(用于按钮):

<ListBox Name="lstCompany" Grid.Column="0" MinWidth="200"    Grid.Row="1"  HorizontalAlignment="Stretch" Margin="2,2,2,2"  VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch"  ItemsSource="{Binding}" SelectionChanged="lstCompany_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate >
            <Grid   Name="grid1" VerticalAlignment="Top" Margin="2" HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <StackPanel Orientation="Vertical"  HorizontalAlignment="Stretch">
                    <TextBlock  Text="{Binding [0]}" FontWeight="Bold"/>
                    <TextBlock  Text="{Binding [1]}" ></TextBlock>
                    <StackPanel.ToolTip>
                        <ToolTip>asdf</ToolTip>
                    </StackPanel.ToolTip>
                </StackPanel>

                <StackPanel Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Right">
                    <Button Content="X" Tag="{Binding [2]}"  Width="20" Click="btnRemove_Click"  Name="btnRemove1"></Button>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

One Grid is enough here. You only need one star-sized column (for content) and one Auto-sized column (for the button):

<ListBox Name="lstCompany" Grid.Column="0" MinWidth="200"    Grid.Row="1"  HorizontalAlignment="Stretch" Margin="2,2,2,2"  VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch"  ItemsSource="{Binding}" SelectionChanged="lstCompany_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate >
            <Grid   Name="grid1" VerticalAlignment="Top" Margin="2" HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <StackPanel Orientation="Vertical"  HorizontalAlignment="Stretch">
                    <TextBlock  Text="{Binding [0]}" FontWeight="Bold"/>
                    <TextBlock  Text="{Binding [1]}" ></TextBlock>
                    <StackPanel.ToolTip>
                        <ToolTip>asdf</ToolTip>
                    </StackPanel.ToolTip>
                </StackPanel>

                <StackPanel Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Right">
                    <Button Content="X" Tag="{Binding [2]}"  Width="20" Click="btnRemove_Click"  Name="btnRemove1"></Button>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文