WPF MVVM 将 TextBlock 的文本绑定到 ObservableCollection 成员
WPF MVVM Binding Text of TextBlock to ObservableCollection Member
我想将 ObservableCollection 的值绑定到 WindowMain 中 TextBlock 的 "Text"。 WindowMain 的 DataContext 指向它的 ViewModel(WindowMainVM.cs)。 INotifyPropertyChanged 保存在 ObservableObject 中。我有一个名为 OPEmpName.cs 的 ObservableCollection 的单独模型 class。我已经梳理 Google 好几天了,还没有找到可行的解决方案。
输入用户名和密码后,用户名应出现在文本块中。 (仅用于演示目的)就目前而言,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>();
}
}
您似乎正在搜索 returns 一个列表,但您只有一个项目绑定到该列表。您将 TextBlock 设置为“{Binding OPEmpNameList.Name}”,但 OPEmpNameList 是一个列表。您应该将结果放入容器中,例如:
<ListBox ItemsSource="{Binding OEmpNameList}" DisplayMemberPath="Name" />
我想将 ObservableCollection 的值绑定到 WindowMain 中 TextBlock 的 "Text"。 WindowMain 的 DataContext 指向它的 ViewModel(WindowMainVM.cs)。 INotifyPropertyChanged 保存在 ObservableObject 中。我有一个名为 OPEmpName.cs 的 ObservableCollection 的单独模型 class。我已经梳理 Google 好几天了,还没有找到可行的解决方案。
输入用户名和密码后,用户名应出现在文本块中。 (仅用于演示目的)就目前而言,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>();
}
}
您似乎正在搜索 returns 一个列表,但您只有一个项目绑定到该列表。您将 TextBlock 设置为“{Binding OPEmpNameList.Name}”,但 OPEmpNameList 是一个列表。您应该将结果放入容器中,例如:
<ListBox ItemsSource="{Binding OEmpNameList}" DisplayMemberPath="Name" />