WPF绑定到标记问题(来自新手) [英] WPF Binding to markup questions (from a newbie)

查看:63
本文介绍了WPF绑定到标记问题(来自新手)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是我的任务

1.当MainWindow加载时,应该从我的数据库中填充PatientsDataGrid。

2.来自网格的文本框(x:名称) =DetailsGrid)应该为空,除了IdTextBox.Text应为零。

3.如果我在IdTextBox.Text中键入数字并按SearchButton,则其他文本框(LastName,FirstName,MiddleName) )应该从数据库中加载相应的记录。同时,PatientsDataGrid还必须将其内容过滤到我从IdTextBox搜索的Id中。

4.如果我在Last / First / Middle-NameTextBox中键入一些条目并按下AddButton,它将被添加到数据库中,以及PatientsDataGrid将被刷新。

5.如果我按下EditButton,数据库中的条目将被更新,PatientDataGrid将被刷新。





但是当我盲目尝试不同的绑定时,结果变得更糟,数据网格没有加载记录,而搜索/添加/更新按钮无法正常运行。我哪里出错?







这是我的标记:



These are my tasks
1. When the MainWindow loads, the PatientsDataGrid should be populated from my database.
2. The textboxes from the Grid (x:Name="DetailsGrid") should be blank, except the IdTextBox.Text should be zero.
3. If I type a number in IdTextBox.Text and press SearchButton, the Other TextBoxes(LastName, FirstName, MiddleName) should be loaded with the corresponding record from the database. At the same time, the PatientsDataGrid must also filter its content into the Id that I searched from IdTextBox.
4. If I type some entries in the Last/First/Middle-NameTextBoxes and press the AddButton, it will be added in the database, as well as the PatientsDataGrid will be refreshed.
5. If I press the EditButton, the entry from the database will be updated the the PatientsDataGrid be refreshed.


But as I blindly try different binding, the result has gone to worse, the datagrid is not loaded with records, and the Search/Add/Update-Buttons are not functioning properly. Where did I go wrong?



Here is my mark up:

<Window x:Class="WpfApplication7.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="500" Width="500"

        WindowStartupLocation="CenterScreen"

        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    
    <Grid x:Name="DetailsGrid" Margin="10" >
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <TextBlock Text="Id" Grid.Row="0" Grid.Column="0"/>
            <TextBlock Text="Last Name" Grid.Row="1" Grid.Column="0"/>
            <TextBlock Text="First Name" Grid.Row="2" Grid.Column="0"/>
            <TextBlock Text="Middle Name" Grid.Row="3" Grid.Column="0"/>

            <TextBox x:Name="IdTextBox" Text="{Binding Id, Mode=TwoWay}" 

                     Grid.Row="0" Grid.Column="1" />
            <TextBox x:Name="LastTextBox" Text="{Binding LastName, Mode=TwoWay}" 

                     Grid.Row="1" Grid.Column="1" />
            <TextBox x:Name="FirstTextBox" Text="{Binding FirstName, Mode=TwoWay}"

                     Grid.Row="2" Grid.Column="1" />
            <TextBox x:Name="MiddleTextBox" Text="{Binding MiddleName, Mode=TwoWay}" 

                     Grid.Row="3" Grid.Column="1" />

            <TextBlock Text="Combo Boxing" Grid.Row="4" Grid.Column="0"  />
            <ComboBox x:Name="TryComboBox" Grid.Row="4" Grid.Column="1" />

            <Button x:Name="SearchButton" Content="Search" Grid.Row="0" Grid.Column="2" 

                    Click="SearchButton_OnClick"/>

            <Button x:Name="AddButton" Content="Add" Grid.Row="5" Grid.Column="0" 

                    Click="AddButton_OnClick"/>
            <Button x:Name="UpdateButton" Content="Update" 

                    Grid.Row="{Binding ElementName=AddButton, Mode=OneWay, Path=(Grid.Row)}" 

                    Grid.Column="1" Click="UpdateButton_OnClick"/>
            <Button x:Name="DeleteButton" Content="Delete" 

                    Grid.Row="{Binding ElementName=AddButton, Mode=OneWay, Path=(Grid.Row)}" 

                    Grid.Column="2" Click="DeleteButton_OnClick"/>
        </Grid>
        
        <DataGrid Name="PatientDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" 

                  Grid.Row="1"

                  IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Binding="{Binding Id}" />
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
                <DataGridTextColumn Header="Middle Name" Binding="{Binding MiddleName}" />
                <DataGridCheckBoxColumn Header="Status" Binding="{Binding StatusId}" 

                                        Visibility="Collapsed"/>
            </DataGrid.Columns>
           
        </DataGrid>
    </Grid>
</Window>













Here is my MainWindow code behind:









Here is my MainWindow code behind:

public partial class MainWindow : Window
    {
        PatientMgr patientMgrModel = new PatientMgr();
        public MainWindow()
        {
            InitializeComponent();
            

            patientMgrModel.Entity.Id = 0;
            Refresher();
        }


        public void Refresher()
        {
            DetailsGrid.DataContext = patientMgrModel.Entity;

            patientMgrModel.EntityList = patientMgrModel.RetrieveMany(patientMgrModel.Entity);

            DataContext = patientMgrModel.EntityList;
        }

        private void AddButton_OnClick(object sender, RoutedEventArgs e)
        {
            
            patientMgrModel.Insert(patientMgrModel.Entity);
            Refresher();
        }

        private void UpdateButton_OnClick(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(String.Format("Id:{0} \nLast Name: {1} \nFirst Name: {2} \nMiddleName: {3}",
            //    patientViewModel.Id,
            //    patientViewModel.LastName,
            //    patientViewModel.FirstName,
            //    patientViewModel.MiddleName));

            patientMgrModel.Update(patientMgrModel.Entity);
        }


        private void DeleteButton_OnClick(object sender, RoutedEventArgs e)
        {
            patientMgrModel.Delete(patientMgrModel.Entity);
        }

        private void SearchButton_OnClick(object sender, RoutedEventArgs e)
        {
            patientMgrModel.Entity = patientMgrModel.Retrieve(patientMgrModel.Entity);

            DataContext = patientMgrModel.Entity;
        }
    }





Here is my BusinessLogicLayer:





Here is my BusinessLogicLayer:

public class PatientMgr : INotifyPropertyChanged
    {
        private readonly PatientDb _db;
        private Patient _entity;
        private List<Patient> _entityList;

        public Patient Entity
        {
            get { return _entity; }
            set
            {
                if (Equals(value, _entity)) return;
                _entity = value;
                OnPropertyChanged();
            }
        }

        public List<Patient> EntityList
        {
            get { return _entityList; }
            set
            {
                if (Equals(value, _entityList)) return;
                _entityList = value;
                OnPropertyChanged();
            }
        }

        public PatientMgr()
        {
            _db = new PatientDb();
            Entity = new Patient();
            EntityList = new List<Patient>();
        }

        public Patient Retrieve(Patient parameters)
        {
            return _db.Retrieve(parameters);
        }

        public List<Patient> RetrieveMany(Patient parameters)
        {
            return _db.RetrieveMany(parameters);
        }

        public bool Insert(Patient entity)
        {
            return _db.Insert(entity);
        }

        public bool Update(Patient entity)
        {
            return _db.Update(entity);
        }

        public bool Delete(Patient entity)
        {
            return _db.Delete(entity);
        }


        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }







I also have DataAccessLayer which contains methods that are being called using the BusinessLogicLayer above. It returns just fine so I will not post it here. The problems are stated in my second paragraph. I hope you could help me, I tried studying binding, but I think I needed a pattern first before I can understand most of what stated in different tutorials.




I also have DataAccessLayer which contains methods that are being called using the BusinessLogicLayer above. It returns just fine so I will not post it here. The problems are stated in my second paragraph. I hope you could help me, I tried studying binding, but I think I needed a pattern first before I can understand most of what stated in different tutorials.

推荐答案

List will not update the UI as you have not implemented the InotifyCollectionChanged event.



Use observablecollection here for the your entity list.



And inside the model use the InotifyPropertyChanged interface.
List will not update the UI as you have not implemented the InotifyCollectionChanged event.

Use observablecollection here for the your entity list.

And inside the model use the InotifyPropertyChanged interface.


这篇关于WPF绑定到标记问题(来自新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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