0

我有 2 个页面(用户和用户列表)当我在用户页面上添加新用户然后将页面更改为用户列表页面时,用户列表页面上ComboBoxUser不会自动更新。有什么想法可以解决这个问题吗?

这是我的代码 User.cs

public class User : INotifyPropertyChanged
{
    string _firstName;
    public string FirstName {
        get { return _firstName; }
        set { 
            _firstName = value;
            OnPropertyChanged("FirstName");
            OnPropertyChanged("FullName");
        }
    }

    string _lastName;
    public string LastName {
        get { return _lastName; }
        set { 
            _lastName = value;
            OnPropertyChanged("LastName");
            OnPropertyChanged("FullName");
        }
    }

    public string FullName
    {
        get { return string.Format("{0} {1}", FirstName, LastName); }
    }

    int _gender;
    public int Gender {
        get { return _gender; }
        set { 
            _gender = value;
            OnPropertyChanged("Gender");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

UserListingViewModel.cs

public class UserListingViewModel : INotifyPropertyChanged
{
    public UserListingViewModel()
    {
        LoadDataUser();
    }

    public void LoadDataUser()
    {
        ComboUserData = new ObservableCollection<User>();

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[
            "dLondre.Properties.Settings.CSLONDRE"].ConnectionString);

        string str = "";
        str += "SELECT userid,firstname + ' ' + lastname as name FROM [user] ";

        SqlCommand com = con.CreateCommand();
        com.CommandText = str;

        try
        {
            con.Open();
            SqlDataReader sdr = com.ExecuteReader();
            while (sdr.Read())
            {
                ComboUserData.Add(new User { UserID = (string)sdr["userid"], FirstName = (string)sdr["name"]});
            }

            com.Dispose();
            con.Close();
            con.Dispose();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }


    ObservableCollection<User> _comboUserData;
    public ObservableCollection<User> ComboUserData
    {
        get
        {
            if (_comboUserData == null)
                _comboUserData = new ObservableCollection<User>();
            return _comboUserData;
        }
        set
        {
            if (value != _comboUserData)
                _comboUserData = value;
            OnPropertyChanged("ComboUserData");
        }
    }

    User _user;
    public User SelectedUserID
    {
        get { return _user; }
        set
        {
            _user = value;
            OnPropertyChanged("SelectedUserID");
        }
    }
}

这是我的 XAML

                <ComboBox x:Name="DDUserId" 
                          Width="140" 
                          DisplayMemberPath="FirstName" 
                          IsSynchronizedWithCurrentItem="True" 
                          ItemsSource="{Binding ComboUserData, Mode=TwoWay}" 
                          SelectedItem="{Binding SelectedUserID}">
                </ComboBox>

主窗口 XAML

    <mui:LinkGroup DisplayName="welcome">
        <mui:LinkGroup.Links>
            <mui:Link DisplayName="Login" Source="/Views/Login.xaml" />
            <mui:Link DisplayName="Register User" Source="/Views/UserView.xaml" />
            <mui:Link DisplayName="Manage Group" Source="/Views/UserGroupView.xaml" />
            <mui:Link DisplayName="List User" Source="/Views/UserListingView.xaml" />
        </mui:LinkGroup.Links>
    </mui:LinkGroup>
4

3 回答 3

0

1) 添加第二个用户后,在进入用户列表页面之前检查是否正在保存到数据库中。

2)如果新用户被添加到数据库中,则将调试器放在 LoadDataUser 方法上并移动到 UserList 页面,查看 ComboBox 是否能够获取新添加的用户。

由于您在不同的页面中加载用户,所以我认为通知组合框不会有问题,它应该根据连接加载用户。

于 2013-11-12T11:48:55.247 回答
0

要自动更新数据绑定:

  1. ViewModel必须实施INotifyPropertyChanged
  2. 您的绑定属性NotifyOnSourceUpdatedNotifyOnTargetUpdated必须设置为true
于 2013-11-12T09:36:27.387 回答
0

最后我找到了解决方案,也许它只是把戏,但它有效..

我只是关注此链接中的文章 http://blog.cylewitruk.com/2010/10/mvvm-using-a-timer-in-your-viewmodel/ [^]

这是代码:

private ShowUser showuser;
private void InvalidateSampleData(object state, EventArgs e)
{
    showuser = new ShowUser();
    Users = showuser.GetUser();
}




public class ShowUser : INotifyPropertyChanged
{
    public ObservableCollection<User> GetUser()
    {
        ObservableCollection<User> users = new ObservableCollection<User>();

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[
            "dLondre.Properties.Settings.CSLONDRE"].ConnectionString);

        string str = "";
        str += "SELECT userid,firstname + ' ' + lastname as name FROM [user] ";

        SqlCommand com = con.CreateCommand();
        com.CommandText = str;

        try
        {
            con.Open();
            SqlDataReader sdr = com.ExecuteReader();
            while (sdr.Read())
            {
                users.Add(new User { UserID = (string)sdr["userid"], FirstName = (string)sdr["name"] });
            }

            com.Dispose();
            con.Close();
            con.Dispose();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        return users;
    }

    #region Methods
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}

private ObservableCollection<User> _users;
public ObservableCollection<User> Users
{
    get { return _users; }
    set
    {
        if (value != _users)
            _users = value;
        OnPropertyChanged("Users");
    }
}

User _user;
public User SelectedUserID
{
    get { return _user; }
    set
    {
        _user = value;
        OnPropertyChanged("SelectedUserID");
        //MessageBox.Show("Do your Task with " + value.UserID);
    }
}

感谢您对这个线程的任何帮助和建议,我真的很感激......现在我可以平静地死去......;)

于 2013-11-18T05:11:35.503 回答