【问题标题】:Binding ListBox and TextBoxes to Datatable将 ListBox 和 TextBoxes 绑定到数据表
【发布时间】:2014-05-13 13:28:22
【问题描述】:

我有这个代码:

public partial class MainWindow : Window {

            SqlConnection conn;
            DataTable dataTable;
            SqlDataAdapter adapter;

            public MainWindow() {
                try {
                    InitializeComponent();
                    this.conn = new SqlConnection("Server=(LocalDB)\\v11.0; Database = usersdb; Integrated Security=True; MultipleActiveResultSets=True");
                    this.conn.Open();

                    this.dataTable = new DataTable();
                    adapter = new SqlDataAdapter("Select Forenames, Surname from users", conn);
                    new SqlCommandBuilder(adapter);
                    adapter.Fill(this.dataTable);


this.DataContext = this.dataTable;

    TextBox textBox= new TextBox { };
                    Binding binding = new Binding("forenames");
                    binding.Mode = BindingMode.TwoWay;
                    binding.ValidatesOnDataErrors = true;
                    binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                    textBox.SetBinding(TextBox.TextProperty, binding);


    }

我在 XAML 中也有这个:

<ListBox ItemsSource="{Binding}" DisplayMemberPath="forenames" Name="listBox" Width="200" DockPanel.Dock="Left" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"/>

如何绑定文本框以在列表中显示当前选中的人?

【问题讨论】:

    标签: wpf binding datatable textbox listbox


    【解决方案1】:

    您好,显示您需要在 VM 中定义的信息

    private DataRow _selectedPerson;
    public DataRow SelectedPerson
    {
        get { return _selectedPerson;}
        set { _selectedPerson = value; OnPropertyChanged("SelectedPerson");}
    }  
    

    你的 get 应该返回带有姓名和姓氏的元素,并且在你的 xaml 中

    <ListBox ... SelectedValue="{Binding SelectedPerson}"/>
    <TextBlock Text="{Binding SelectedPerson, Converter={StaticResource dtRow2Text}}"/>
    

    还有你的Converter

    public class DataRow2String: IValueConveter
    {
        public object Convert(object value, Type targetType, object paramater, CultureInfo culture)
        {
            var dt = value as DataRow;
            return (dt[index with names] +" "+ dt[index with surname]).ToString();
        }
    }
    

    HTH

    【讨论】:

    • 好的,我该如何实现这样的事情: TextBox textb = new TextBox { Text = "Binding Path=Rows[2].ItemArray[2],Mode=Default}" };或者这样:绑定绑定 = 新绑定 { Path=Rows[2]["forenames"]};
    【解决方案2】:

    谢谢,我终于实现了:

    private void selectionChanged(object sender, SelectionChangedEventArgs e) {
                int index = this.listBox.SelectedIndex;
                if (index < 0 || index > this.dataTable.Rows.Count)
                    return;            
                UIElementCollection collection = this.grid.Children;
               foreach (UIElement element in collection)
                   if (element is TextBox) {
                       TextBox t = (TextBox)element;
                       t.DataContext = this.dataTable.DefaultView[index];
                   }
            }
    

    【讨论】:

    • kk 但请注意,当您在集合中有一项时,将不会调用 selectionChanged()。
    猜你喜欢
    • 2014-09-28
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多