Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:将文本框绑定到SelectedValue_C#_.net_Data Binding_List_Listbox - Fatal编程技术网

C#:将文本框绑定到SelectedValue

C#:将文本框绑定到SelectedValue,c#,.net,data-binding,list,listbox,C#,.net,Data Binding,List,Listbox,我有一个绑定到列表框的对象列表。然后我想将SelectedValue的属性绑定到各种文本框。不过,这种行为非常古怪 绑定用作ListBox的DisplayMember的名称(字符串)时,它不会更新ListBox,如果我尝试刷新TextChanged事件上的绑定,它不会更新,直到选择发生更改,然后切换选择时出现问题 当绑定余额(一个小数点)时,它会更改所有数据(或者,可能是在我更改选择时应用了更改,但它实际上是在更改数据,而不仅仅是不更新数据) 为了清楚起见,我使用的是C#.NET而不是ASP。假

我有一个绑定到列表框的对象列表。然后我想将SelectedValue的属性绑定到各种文本框。不过,这种行为非常古怪

绑定用作ListBox的DisplayMember的名称(字符串)时,它不会更新ListBox,如果我尝试刷新TextChanged事件上的绑定,它不会更新,直到选择发生更改,然后切换选择时出现问题

当绑定余额(一个小数点)时,它会更改所有数据(或者,可能是在我更改选择时应用了更改,但它实际上是在更改数据,而不仅仅是不更新数据)


为了清楚起见,我使用的是C#.NET而不是ASP。

假设使用WPF,一个快速示例:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <ListBox x:Name="list" 
                 ItemsSource="{Binding}"
                 DisplayMemberPath="Name"/>
        <TextBox Text="{Binding ElementName=list, Path=SelectedItem.Name}"
                 Grid.Row="1"/>
        <TextBox Text="{Binding ElementName=list, Path=SelectedItem.Val}"
                 Grid.Row="2" />
    </Grid>
</Window>

namespace WpfApplication1 {
    public class Thing : INotifyPropertyChanged {
        private string _name;
        private double _val;

        public string Name {
            get { return _name; }
            set {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        public double Val {
            get { return _val; }
            set {
                _val = value;
                OnPropertyChanged("Val");
            }
        }

        protected void OnPropertyChanged(string propertyName) {
            PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if (propertyChanged != null) {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();

            DataContext = new List<Thing> { new Thing { Name = "A", Val = 1.0 }, new Thing { Name = "B", Val = 2.0 } };
        }
    }
}

命名空间WpfApplication1{
公共类事物:INotifyPropertyChanged{
私有字符串\u名称;
私人双价;
公共字符串名{
获取{return\u name;}
设置{
_名称=值;
不动产变更(“名称”);
}
}
公共双Val{
获取{return\u val;}
设置{
_val=值;
不动产变更(“Val”);
}
}
受保护的无效OnPropertyChanged(字符串propertyName){
PropertyChangedEventHandler propertyChanged=this.propertyChanged;
if(propertyChanged!=null){
propertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共事件属性更改事件处理程序属性更改;
}
公共部分类主窗口:窗口{
公共主窗口(){
初始化组件();
DataContext=newlist{newthing{Name=“A”,Val=1.0},newthing{Name=“B”,Val=2.0};
}
}
}

我在找到了解决方案。基本上,我需要使用BindingList集合,而不仅仅是List。

您能发布一些代码吗?您的列表是否包含INotifyPropertyChanged派生对象?还有:Silverlight、WinForms或WPF?我不知道我在使用什么(Silverlight、WinForms、WPF)。我想是WinForms。下面是如何绑定nameTxb.DataBindings.Add(“Text”,accountsLsb,“SelectedValue.Name”);而且它不会更新列表框。它会更新实际数据,但您必须切换选择以使其更新列表框中的名称。