WPF MVVM绑定文本block的文本与观察成员



我想将ObservableCollection的值绑定到Windowsmain中文本块的"文本"。WindowMain的DataContext指向其ViewModel(WindowMainvm.cs)。InotifyPropertychanged在可观察的对象中持有。我有一个单独的模型类,用于观察到opempname.cs。我已经梳理了Google几天,但没有找到可行的解决方案。

输入用户名和密码后,用户的名称应显示在文本块中。(仅仅是演示目的)就目前而言,方法射击后,文本块保持空白。我要去哪里?非常感谢您的帮助!

windowmain.xaml

    <Grid>
        <Label x:Name="lblUsername" Content="Username" HorizontalAlignment="Left" Margin="45,57,0,0" VerticalAlignment="Top"/>
        <Label x:Name="lblPassword" Content="Password" HorizontalAlignment="Left" Margin="48,104,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtUsername" Text="{Binding Textbox1Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
             HorizontalAlignment="Left" Height="23" Margin="128,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="txtPassword" Text="{Binding Textbox2Input, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
             HorizontalAlignment="Left" Height="23" Margin="128,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBlock HorizontalAlignment="Left" Height="23" Margin="91,214,0,0" TextWrapping="Wrap" Text="{Binding OPEmpNameList.Name}" VerticalAlignment="Top" Width="120"/>
    </Grid>

windowmainvm.cs

public class WindowMainVM : ObservableObject
{
    private string _textbox1Input;
    private string _textbox2Input;
    private string _name;
    private int _empNum;
    private ObservableCollection<OPEmpName> _opEmpNameList;

    public String Textbox1Input
    {
        get { return _textbox1Input; }
        set { SetProperty(ref _textbox1Input, value, () => Textbox1Input); }
    }
    public String Textbox2Input
    {
        get { return _textbox2Input; }
        set
        {
            SetProperty(ref _textbox2Input, value, () => Textbox2Input);
            if (Textbox2Input != null)
            {
                fillNAME();
            }
        }
    }
    public String NAME
    {
        get { return _name; }
        set { SetProperty(ref _name, value, () => NAME); }
    }
    public int OPEMPNUM
    {
        get { return _empNum; }
        set { SetProperty(ref _empNum, value, () => OPEMPNUM); }
    }
    public ObservableCollection<OPEmpName> OPEmpNameList
    {
        get { return _opEmpNameList; }
        set
        {
            SetProperty(ref _opEmpNameList, value, () => OPEmpNameList);
        }
    }
    public WindowMainVM() : base()
    {
        OPEmpNameList = new ObservableCollection<OPEmpName>();
    }

    private void fillNAME()
    {
        if (Textbox2Input != null)
        {
            using (MySqlConnection con = new MySqlConnection(dbConnectionString))
            {
                OPEmpNameList = new ObservableCollection<OPEmpName>();
                con.Open();
                string Query = "SELECT first_name FROM op_rep_users WHERE username='" + Textbox1Input + "' AND password='" + Textbox2Input + "' ";
                MySqlCommand createCommand = new MySqlCommand(Query, con);
                MySqlDataReader dr = createCommand.ExecuteReader();
                int count = 1;
                while (dr.Read())
                {
                    string Name = dr.GetString(0);
                    OPEmpName name = new OPEmpName(count, Name);
                    OPEmpNameList.Add(name);
                    count++;
                }
                con.Close();
            }
        }
    }       
}

opempname.cs

    public class OPEmpName : ObservableObject
{
    private Int32 _count;
    private String _name;
    private ObservableCollection<OPEmpName> _opEmpNameList;

    public Int32 Count
    {
        get { return _count; }
        set { SetProperty(ref _count, value, () => Count); }
    }
    public String Name
    {
        get { return _name; }
        set { SetProperty(ref _name, value, () => Name); }
    }
    public ObservableCollection<OPEmpName> OPEmpNameList
    {
        get { return _opEmpNameList; }
        set { SetProperty(ref _opEmpNameList, value, () => OPEmpNameList); }
    }
    public OPEmpName() : base()
    {
        Count = 0;
        Name = "";
        OPEmpNameList = new ObservableCollection<OPEmpName>();
    }
    public OPEmpName(int count, string name) : base()
    {
        Count = count;
        Name = name;
        OPEmpNameList = new ObservableCollection<OPEmpName>();
    }
}

您似乎正在进行返回列表的搜索,但是您只有一个绑定到该列表的项目。您将TextBlock设置为" {binding opempnamelist.name}",但是opempnamelist是列表。您应该将结果放入以下容器中:

<ListBox ItemsSource="{Binding OEmpNameList}" DisplayMemberPath="Name" />

相关内容

  • 没有找到相关文章

最新更新