2014-12-03 55 views
0

哪些元素,以显示我要创建具有下列元素的WPF窗口的多条路径

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

如果您已添加第一个路径并将添加按钮转换为删除按钮和箭头按钮,则第一行下方应显示下一个添加按钮。

哪些元素(datagrid,...)最能实现这一点?

回答

1

我不一定会改变“添加”按钮,进入其他的,这是可以做到在我看来,要容易得多:

  1. 使用Grid从“确定”,划分的文件路径的项目“取消“按钮
  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> 

当然,你必须增加利润,更细致的定位,风格和结合自己。

+0

但我还需要SelectedItem。 ItemsControl不支持那个权利? – Struct 2014-12-03 14:04:28

+0

否定的,在这种情况下,你应该使用'ListView',但是这也给了你可能不需要的新特性。如果您需要'SelectedItem'来标识哪个按钮被点击的项目,那么有更好的实现。例如,使用MVVM时,您可以将单击项目的“CommandParameter”发送到ViewModel。但这实际上是一个完全不同的问题。 – Sjeijoet 2014-12-03 14:34:58

+0

如果您使用的是MVVM,那么这个主题可能会因为您而发生变化:http://stackoverflow.com/questions/1939907/binding-to-currentitem-in-a-itemscontrol/1940031#1940031 – Sjeijoet 2014-12-03 14:53:36