gpt4 book ai didi

c# - WPF MVVM 将 TextBlock 的文本绑定(bind)到 ObservableCollection 成员

转载 作者:行者123 更新时间:2023-12-03 10:33:03 31 4
gpt4 key购买 nike

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

输入用户名和密码后,用户名应出现在 TextBlock 中。 (仅用于演示目的)就目前而言,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>();
}
}

最佳答案

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

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

关于c# - WPF MVVM 将 TextBlock 的文本绑定(bind)到 ObservableCollection 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47722672/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com