[英]WPF C# Populate data into combobox from seperate class file
我这里有三个文件,用于填充数据库中的数据并将其分配给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文件代码:如果要在页面加载事件中显示,请调用In Constructor。
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文件:(用于绑定datagrid的创建方法)
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;
}
}
希望我的本代码将帮助您解决问题。 我会尽力解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.