c# - 将文本框绑定(bind)到 Listview GridViewColumn

标签 c# wpf xaml mvvm data-binding

我希望将单个文本框绑定(bind)到 gridviewcolumn 中的值。
但是,在编辑文本框时,我希望它也可以编辑和更新我选择的 GridView 列行。

XAML

<ListView x:Name="ExampleLV" ItemsSource="{Binding Data.Collection}">    
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ColumnName1" DisplayMemberBinding="{Binding Value[0], Mode=TwoWay}"/>
            <GridViewColumn Header="ColumnName2" DisplayMemberBinding="{Binding Value[1], Mode=TwoWay}"/>  
        </GridView>
    </ListView.View>
</ListView>

<TextBox Name="ExampleTB1" Text="{Binding ElementName=ExampleLV, Path=SelectedItem.Values[0]}"/>
<TextBox Name="ExampleTB2" Text="{Binding ElementName=ExampleLV, Path=SelectedItem.Values[1]}"/>

C# - 集合映射到字符串数组
Collection.Add(new ListViewItem() { Values = new string[] { "abc", "123" } }); 

ListViewItem 是我构建的自定义类。
public class ListViewItem : INotifyPropertyChanged
    {
        private string[] _values;

        #region Properties

        public string[] Values
        {
            get { return _values; }
            set
            {
                if (value == _values) return;
                _values = value;
                OnPropertyChanged();
            }
        }

        #endregion

        public event PropertyChangedEventHandler PropertyChanged;//[NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

我遇到的问题是文本框的值正在更新,但它们没有更新 listviewItem。但是,正在存储的值正在更新,而不是 GridViewColumn。

我也希望这与组合框一起使用。请帮忙 :)

最佳答案

一些东西..

您正在更新集合中的字符串,因此您需要在集合项上实现 INotify。

就像是:

public class ValueViewModel : INotifyPropertyChanged
{
    private string _value;

    public string Value
    {
        get { return _values; }
        set
        {
            if (_value == value) return;

            _value = value;
            OnPropertyChanged();
        }
    }
}

更新您的 ListViewItem ViewModel 以使用新的 ValueViewModel 类型。
public class ListViewItem : INotifyPropertyChanged
{
    private ValueViewModel[] _values;

    #region Properties

    public ValueViewModel[] Values
    {
        get { return _values; }
        set
        {
            if (value == _values) return;
            _values = value;
            OnPropertyChanged();
        }
    }

    #endregion

    public event PropertyChangedEventHandler PropertyChanged;//[NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

最后,更新您的绑定(bind):
<ListView x:Name="ExampleLV" ItemsSource="{Binding Data.Collection}"> 

    <ListView.View>
        <GridView>
            <GridViewColumn Header="ColumnName1" DisplayMemberBinding="{Binding Value[0].Value, Mode=TwoWay}"/>
            <GridViewColumn Header="ColumnName2" DisplayMemberBinding="{Binding Value[1].Value, Mode=TwoWay}"/>  
        </GridView>
    </ListView.View> </ListView>

<TextBox Name="ExampleTB1" Text="{Binding ElementName=ExampleLV, Path=SelectedItem.Values[0].Value}"/> <TextBox Name="ExampleTB2" Text="{Binding ElementName=ExampleLV, Path=SelectedItem.Values[1].Value}"/>

关于c# - 将文本框绑定(bind)到 Listview GridViewColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42842800/

相关文章:

c# - 在 WPF 中实现拖放后无法选中或取消选中网格列中的复选框

c# - 在 Windows8 中编写 C#/XAML 与 C++/XAML WinRT 应用程序的优缺点是什么?

c# - 我怎样才能让数组月份显示在控制台中?

c# - 将 token 传递给 WCF 服务

c# - 使用 dotnet restore 的 Docker 导致错误 : GSSAPI operation failed - An unsupported mechanism was requested

c# - 检查文本框不为空

c# - WPF 将 DataGrid 组合框所选项目绑定(bind)到所选 DataGridRow 的数据上下文

wpf DataGrid 在失去焦点时失去其行选择

c# - ComboBox 显示错误的字符串列表

wpf - WPF中的自定义UserControl/ ListView 控件