[英]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.