【问题标题】:Which elements to show multiple paths显示多条路径的元素
【发布时间】:2014-12-03 07:38:55
【问题描述】:

我想创建一个包含以下元素的 WPF 窗口:

右侧的“添加”按钮用于添加新路径。如果单击该按钮,则会出现一个打开文件对话框,用户可以选择一个文件。文件路径应显示在窗口中。选择文件后,“添加”按钮变成了删除按钮和两个箭头(向下或向上推)。

如果你已经添加了第一条路径并且添加按钮变成了删除和箭头按钮,那么第一行下面应该会出现下一个添加按钮。

哪些元素(数据网格,...)最适合实现这一点?

【问题讨论】:

    标签: c# .net wpf datagrid


    【解决方案1】:

    我不一定将“添加”按钮转换为其他按钮,在我看来它可以更容易完成:

    1. 使用 Grid 将 FilePath 项与“确定”和“取消”按钮分开
    2. 使用StackpanelItemsControl 和“添加”按钮堆叠在一起。
    3. 使用 ItemTemplate 属性创建 FilePath 项的布局,可以是带有用于定位按钮的列的 Grid

    XAML 中的基本结构:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition
                Height="Auto" />
        </Grid.RowDefinitions>
        <StackPanel>
            <ItemsControl> <!-- Control to display a collection of FilePath items -->
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid> <!-- Template for FilePath item -->
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition
                                    Width="Auto" />
                                <ColumnDefinition
                                    Width="Auto" />
                                <ColumnDefinition
                                    Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBox /> <!-- FilePath textbox -->
                            <Button
                                Grid.Column="1"
                                Content="Del" /> <!-- Delete button -->
                            <Button
                                Grid.Column="2"
                                Content="Up" /> <!-- Up button -->
                            <Button
                                Grid.Column="3"
                                Content="Down" /> <!-- Down button-->
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <Button
                Content="Add"
                HorizontalAlignment="Right" /> <!-- Add button -->
        </StackPanel>
        <StackPanel
            Grid.Row="1"
            Orientation="Horizontal"
            HorizontalAlignment="Right">
            <Button
                Content="Ok" /> <!-- Ok button -->
            <Button
                Content="Cancel" /> <!-- Cancel button -->
        </StackPanel>
    </Grid>
    

    当然,您必须自己添加边距、更详细的定位、样式和绑定。

    【讨论】:

    • 但我还需要 SelectedItem。 ItemsControl 不支持吧?
    • 否定,在这种情况下,您应该使用ListView,但这也会为您提供您可能不想要的新功能。如果您需要SelectedItem 来识别单击了哪个按钮的项目,则有更好的实现。例如,当使用 MVVM 时,您可以将点击项目的CommandParameter 发送到您的 ViewModel。但这实际上是一个完全不同的问题。
    • 如果你使用的是 MVVM,这个话题可能会让你很感兴趣:stackoverflow.com/questions/1939907/…
    • 抱歉,我刚刚意识到,我的第一条评论是ListBox
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2020-01-28
    • 2019-09-12
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多