双击它时如何更改列表视图项的值? [英] How do I change my listview item value when I double click it?

查看:62
本文介绍了双击它时如何更改列表视图项的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的应用程序可以正常工作,但我感觉我开始用新的窗口将应用程序弄得一团糟. 我现在正在做的是,将使用StreamReader类获得的列表填充到ListView中. 我还使用了一个我绑定了所有数据的类.

So my application works and its doing fine, but I feel like im starting to clutter the app with new windows. What I am doing right now is, im populating my ListView with a list that I am getting from using the StreamReader class. I am also using a class that I made that I bind all the data with.

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

And what happends when I double click a ListViewItem is that a new window opens with the property & the value already filled out and you can change it that way. But what I want to do is to get rid of the new window that opens and just be able to change the value in the ListView when I double click. What are my options and what would be the proper way of achieving this?

XAML

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

PropertyWindow

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

    }

ServerProperties类

ServerProperties Class

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

推荐答案

如果将ListView替换为DataGrid,则只需双击它即可进入单元格的编辑模式:

If you replace the ListView with a DataGrid, you will be able to enter the edit mode of a cell by simply double-clicking on it:

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

DataGrid提供了开箱即用的功能.

The DataGrid provides this functionality out of the box.

这篇关于双击它时如何更改列表视图项的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆