WPF MVVM将TextBlock绑定到ObservableCollection成员的文本

时间:2017-12-08 21:50:50

标签: c# wpf xaml mvvm observablecollection

我想将ObservableCollection的值绑定到" Text" WindowMain中的TextBlock。 WindowMain的DataContext指向其ViewModel(WindowMainVM.cs)。 INotifyPropertyChanged保存在ObservableObject中。我有一个名为OPEmpName.cs的ObservableCollection的独立Model类。我已经梳理谷歌几天了,还没有找到可行的解决方案。

输入用户名和密码后,用户名应出现在TextBlock中。 (仅限演示目的)按原样,文本块在方法触发后仍为空白。我哪里错了?非常感谢你的帮助!

WindowMain.xaml

df.to_excel('df.xlsx', engine='xlsxwriter')

WindowMainVM.cs

    <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>

OPEmpName.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();
            }

        }

    }       
}

1 个答案:

答案 0 :(得分:1)

您似乎正在进行返回列表的搜索,但您只有一个项目绑定到该列表。您将TextBlock设置为&#34; {Binding OPEmpNameList.Name}&#34;,但OPEmpNameList是一个列表。您应该将结果放入容器中,如:

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