【问题标题】:WPF MVVM Binding Text of TextBlock to ObservableCollection MemberWPF MVVM 将 TextBlock 的文本绑定到 ObservableCollection 成员
【发布时间】:2018-05-23 04:33:55
【问题描述】:

我想将 ObservableCollection 的值绑定到 WindowMain 中 TextBlock 的“文本”。 WindowMain 的 DataContext 指向它的 ViewModel(WindowMainVM.cs)。 INotifyPropertyChanged 保存在 ObservableObject 中。我有一个名为 OPEmpName.cs 的 ObservableCollection 的单独模型类。我已经梳理了谷歌几天,并没有找到可行的解决方案。

输入用户名和密码后,用户名应出现在文本块中。 (仅用于演示目的)就目前而言,TextBlock 在方法触发后保持空白。我哪里错了?非常感谢您的帮助!

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 上吗?
  • 你想做什么: public class Name { public string Name {get;set} } List list = new List() { "Name1", "Name2" , "名称3" }; var howDoYouThinkWhatNameWillYouGet = list.Name;

标签: c# wpf xaml mvvm observablecollection


【解决方案1】:

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

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

【讨论】:

  • 它在我使用列表框时有效,但不适用于 TextBlock 建议。想法?你能不将 ObservableCollection 的成员绑定到 TextBlock 吗?
  • 不,你不能。 TextBlock 仅绑定到单个对象。如果您的搜索曾经返回一个对象,请不要将结果粘贴到列表中。
猜你喜欢
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 2011-01-01
  • 2016-08-28
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多