c# - WPF C#将数据从单独的类文件填充到组合框中
问题描述
我在这里有三个文件,它们填充数据库中的数据并将其分配给 xaml 视图。
这是我的 user.cs 文件,其中包含用户功能:
public List<User> getUserList()
{
var user = new List<User>();
string query;
query = "select * from users";
da = new MySqlDataAdapter(query, db.GetConnection());
da.Fill(dt);
reader = db.QueryCommand(query);
ComboBox cb = new ComboBox();
while (reader.Read())
{
user.Add(new User()
{
UserId = reader[0].ToString(),
UserName = reader[1].ToString(),
UserCreatedDate = reader[5].ToString(),
UserEmail = reader[6].ToString(),
UserFirstName = reader[7].ToString(),
UserLastName = reader[8].ToString(),
UserRole = reader[3].ToString()
});
}
reader.Close();
return user;
}
这是我的 viewusers.cs 文件:
private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
TextBox txtFirstName = (TextBox)GetChildren(userDataGrid).First(x => x.Name == "txtFirstName");
TextBlock txtBlockId = (TextBlock)GetChildren(userDataGrid).First(x => x.Name == "txtBlockId");
ComboBox cbUserRole = (ComboBox)GetChildren(userDataGrid).First(x => x.Name == "cbUserRole");
string firstName = txtFirstName.Text;
string id = txtBlockId.Text;
string userRole = cbUserRole.SelectedItem.ToString();
}
这是我的 viewusers.xaml 文件:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border BorderThickness="0" Background="BlanchedAlmond" Padding="10">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="12" Text="User Role: " VerticalAlignment="Center" />
<ComboBox x:Name="cbUserRole" FontSize="16" Foreground="MidnightBlue" Text="{Binding UserRole}" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Button x:Name="btnUpdate" Content="Update" VerticalAlignment="Center" HorizontalAlignment="Right" Click="btnUpdate_Click"/>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
现在,问题是我无法将用户角色填充到组合框。我怎么解决这个问题?由于视图文件中不存在组合框,因此我无法从用户类文件中找到组合框。
解决方案
对于 MVVM 模式,我发送了我的代码。我希望它对你有用。
.xaml 视图文件:
<Grid Style="{DynamicResource Dropdown_grid}" Grid.Column="0" Grid.Row="1">
<StackPanel Grid.Column="0" HorizontalAlignment="Left" Style="{DynamicResource Dropdown_stakpanel}">
<TextBlock HorizontalAlignment="Left" Name="FeesGroup" TextWrapping="Wrap" FontWeight="Bold" Text="Select Fees Group *" VerticalAlignment="Top"/>
<ComboBox x:Name="drpfeesgroup" MinHeight="30" HorizontalAlignment="Left" Background="Black" SelectedValuePath="Tag" Tag="{Binding id}" MinWidth="200" ItemsSource="{Binding Path=Persons}" SelectedItem="{Binding Path=SPerson}">
</ComboBox>
</StackPanel>
</Grid>
.xaml.cs 文件代码:如果要在页面加载事件中显示,请调用构造函数。
var ds = feesMaster_ViewModel.Dropdown_Feesgroup();
if (ds.Tables[0].Rows.Count > 0)
{
drpfeesgroup.ItemsSource = ds.Tables[0].DefaultView;
drpfeesgroup.DisplayMemberPath = ds.Tables[0].Columns["name"].ToString();
drpfeesgroup.SelectedValuePath = ds.Tables[0].Columns["id"].ToString();
}
feeMaster_ViewModel.ViewModel.cs 文件:(创建绑定数据网格的方法)
public DataSet Dropdown_Feesgroup()
{
var ds1 = new DataSet();
using (con = new MySqlConnection(ConfigurationManager.ConnectionStrings["schoolmanagment_connectionString"].ToString()))
{
try
{
con.Open();
cmd = new MySqlCommand("select id,name from fee_groups", con);
da = new MySqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds, "fee_groups");
if (ds.Tables[0].Rows.Count > 0)
{
ds1 = ds;
}
}
catch (Exception ex)
{
var a = ex.ToString();
throw;
}
finally { con.Close(); }
return ds;
}
}
我希望,我的这段代码能帮助你解决你的问题。我会尽力解决你的问题。
推荐阅读
- php - 用于编辑数据库的表单操作
- linux - 为什么 os.OpenFile 不会创建 777 文件
- android - 如何在本机反应中创建自定义通知布局?
- javascript - 如何将用户外键设置为当前用户 django ajax
- python - kivymd MDbottomnavigation on_tab_press
- excel - VBA 去掉逗号
- javascript - Three.js:如何翻转轴使Z轴变为X轴
- java - 如何使用其他分区键查询 dynamoDB
- ajax - 为什么服务器不响应某些设备中的某些 ajax 查询?
- laravel - 邮件发送 laravel 7