我创建了ListView
ComboBox
<ListView x:Name="lstvAttendance" Margin="0,53,0,10">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="0" DisplayMemberBinding="{Binding Path=Id}"/>
<GridViewColumn Header="Emp Code" Width="100" DisplayMemberBinding="{Binding Path=emp_id}"/>
<GridViewColumn Header="Employee Name" Width="160" DisplayMemberBinding="{Binding Path=name}"/>
<GridViewColumn Header="Status" Width="90">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cmbStatus" Width="75" Text="{Binding Path=status}" SelectedItem="{Binding Path=status}">
<ComboBoxItem Content="P"/>
<ComboBoxItem Content="A"/>
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Date" Width="135">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DatePicker x:Name="dtpDate" Width="120" Text="{Binding attendance_date}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Reg Date" Width="140" DisplayMemberBinding="{Binding Path=reg_date}"/>
<GridViewColumn Header="Last Update" Width="140" DisplayMemberBinding="{Binding Path=last_update}"/>
<GridViewColumn Header="Delete" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnRemove" Content="Remove" Width="60" BorderThickness="0" CommandParameter="{Binding}" HorizontalContentAlignment="Right" Style="{StaticResource DelImg}" Cursor="Hand" Foreground="Blue"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
我需要绑定此ComboBox
(ComboBox
有P
或A
)。对于从DataBase绑定这些ListView
,我创建了一个类{{ 1}}。
Attendance
然后:
public class Attendance
{
public string Id { get; set; }
public string emp_id { get; set; }
public string name { get; set; }
public string status { get; set; } //Either P or A
public string attendance_date { get; set; }
public string reg_date { get; set; }
public string last_update { get; set; }
}
我的代码适用于try
{
using (SqlConnection con = new SqlConnection(DBCon.conStr))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(AppConstraints.LIST_VIEW_ATTENDANCE))
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
lstvAttendance.Items.Add(new Attendance()
{
Id = dr["Id"].ToString(),
emp_id = dr["emp_id"].ToString(),
name = dr["name"].ToString(),
attendance_datedr["attendance_date"].ToString(),
status = dr["status"].ToString(),
reg_date = dr["reg_date"].ToString(),
last_update = dr["last_update"].ToString()
});
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
中的其他字段。但ListView
值未选中。我需要帮助来选择与数据库值相关的ComboBox
值。
答案 0 :(得分:1)
首先:创建List
Attendance
而不是将项目添加到lstvAttendance
,然后设置lstvAttendance
&#39; s { {1}}像这样:
ItemsSource
其次:您错过了List<Attendance> lst = new List<Attendance>();
try
{
....
....
while (dr.Read())
{
lst.Add(new Attendance()
{
....
}
}
...
...
lstvAttendance.ItemsSource = lst;
}
catch
{
....
}
的{{1}}属性,应该是这样的:
ItemsSource
答案 1 :(得分:0)
您应该使用SelectedValuePath
绑定然后绑定到viewModel的SelectedValue=status
属性。
绑定到SelectedItem
将始终包含实际选定的项(ComboBoxItem)而不是实际值。
<ComboBox x:Name="cmbStatus" Width="75" SelectedValuePath="{Binding Path=Content}" SelectedValue="{Binding Path=status}">
<ComboBoxItem Content="P"/>
<ComboBoxItem Content="A"/>
</ComboBox>