2017-10-13 68 views
0

因此,我的应用程序工作正常,但我觉得即将开始使用新窗口来混淆应用程序。 我现在正在做的是,即时填充我的ListView与我从使用StreamReader类获得的列表。 我也在使用一个我将所有数据绑定在一起的类。当我双击它时,如何更改我的listview项目值?

当双击一个ListViewItem时会发生什么情况是一个新窗口打开,并且属性&的值已经填写完毕,您可以通过这种方式进行更改。 但我想要做的是摆脱打开的新窗口,只需双击,即可更改ListView中的值。 我的选择是什么以及实现这个目标的正确方法是什么?

XAML

  </ListView.View> 
      <ListView.ItemContainerStyle> 
       <Style TargetType="ListViewItem"> 
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" /> 
       </Style> 
      </ListView.ItemContainerStyle> 
     </ListView> 

CS

private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      if (sender is ListViewItem item && item.IsSelected) 
      { 
       var SelectedServerProperties = ((ServerProperties)lvServerProperties.SelectedItem); 
       Properties.Settings.Default.ServerProperty = SelectedServerProperties.Property; 
       Properties.Settings.Default.ServerPropertyValue = SelectedServerProperties.Value; 

       PropertyChangerWindow pcw = new PropertyChangerWindow(); 
       pcw.Show(); 
      } 
     } 

PropertyWindow

public partial class PropertyChangerWindow : Window 
    { 
     public PropertyChangerWindow() 
     { 
      InitializeComponent(); 
      tbProperty.Text = Properties.Settings.Default.ServerProperty; 
      tbValue.Text = Properties.Settings.Default.ServerPropertyValue; 
     } 

    } 

个ServerProperties类

public class ServerProperties 
{ 
    public string Property { get; set; } 
    public string Value { get; set; } 
} 

回答

1

如果用DataGrid更换ListView,您将能够通过简单地双击它进入细胞的编辑模式:

<DataGrid> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Property" Binding="{Binding Property}" /> 
     <DataGridTextColumn Header="Value" Binding="{Binding Value}" /> 
    </DataGrid.Columns> 
</DataGrid> 

DataGrid提供此功能开箱即用。

+0

令人惊叹!快速的问题,有没有办法摆脱出现的虚线箭头?我需要点击模板perchaps吗? https://i.imgur.com/iTIBIA4.gifv –

+0

只需将DataGrid的CanUserSortColumns属性设置为false即可。 – mm8

+0

啊,就是这样!如果它没有太多要求,你碰巧知道为什么当双击一个项目时,为什么我不能获得EditItem对于这个视图是不允许的?或者我应该发布一个关于该问题的新问题? –

相关问题