我有一个使用WPF DataGrid的应用程序,允许用户填写各种各样的内容。其中一部分允许用户选择每个项目扣除的点数。这些值似乎显示正常,但我希望默认选择0(截至目前,没有任何内容),这样如果一切正常,用户就不必手动进行选择。我已经尝试将XAML中的SelectedItem和SelectedValues设置为0但没有成功。我怎么能实现这个目标?
我的XAML
<Window x:Class="hotels.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Grid HorizontalAlignment="Left" Height="100" Margin="150,123,0,0" VerticalAlignment="Top" Width="100" Grid.ColumnSpan="2"/>
<DataGrid x:Name="itemGrid" AutoGenerateColumns="False" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="116,184,0,0" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn IsReadOnly="True" Header="Description" Binding="{Binding Description}" />
<DataGridTextColumn IsReadOnly="True" Header="Points Possible" Binding="{Binding Points}" />
<DataGridTemplateColumn Header="Deductions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Score}" SelectedValue="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Comments">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Comments}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<ListBox x:Name="locationListBox" HorizontalAlignment="Left" Height="100" Margin="93,57,0,0" VerticalAlignment="Top" Width="100" BorderThickness="0,1,1,1"/>
<ListBox x:Name="supervisorListBox" HorizontalAlignment="Left" Height="100" Margin="345,57,0,0" VerticalAlignment="Top" Width="100"/>
<ListBox x:Name="employeeListBox" HorizontalAlignment="Left" Height="100" Margin="223,57,0,0" VerticalAlignment="Top" Width="100"/>
<TextBox x:Name="roomTextBox" HorizontalAlignment="Left" Height="23" Margin="223,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label Content="Room Number:" HorizontalAlignment="Left" Margin="116,7,0,0" VerticalAlignment="Top"/>
<Button x:Name="submitButton" Content="Submit" HorizontalAlignment="Left" Margin="410,289,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
和我的C#
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
DataSet ds = new DataSet();
String query = "SELECT * from dbo.locations";
try { con.Open(); }
catch (SqlException er) { Console.Write(er); }
SqlDataAdapter adapter = new SqlDataAdapter(query, con);
adapter.Fill(ds, "Locations");
query = "SELECT * from dbo.employees";
adapter.Fill(ds, "Employees");
DataTable grid = new DataTable("Grid");
grid.Columns.Add("ID", typeof(int));
grid.Columns.Add("Name", typeof(String));
grid.Columns.Add("Description", typeof(String));
grid.Columns.Add("Points", typeof(Int16));
grid.Columns.Add("Score", typeof(List<int>));
grid.Columns.Add("Comments", typeof(String));
query = "SELECT itemID, name, description, points, category FROM dbo.items";
SqlDataReader reader = new SqlCommand(query, con).ExecuteReader();
while (reader.Read())
{
DataRow row = grid.NewRow();
row["ID"] = reader["itemID"];
row["Name"] = reader["name"];
row["Description"] = reader["description"];
row["Points"] = reader["points"];
int pointsPossible = (int)reader["points"];
List<int> rowList = new List<int>();
for (int i = pointsPossible; i >= 0; i--)
{
rowList.Add(i);
}
row["Score"] = rowList;
grid.Rows.Add(row);
}
ds.Tables.Add(grid);
itemGrid.ItemsSource = ds.Tables["Grid"].DefaultView;
}
}
}
非常感谢你!
答案 0 :(得分:2)
您应该设置所选索引
<ComboBox ItemsSource="{Binding Score}" SelectedIndex="0" />
答案 1 :(得分:1)
根据您构建得分列表的方式,您还可以使用积分值作为所选索引,而无需更改列表顺序
<ComboBox ItemsSource="{Binding Score}" SelectedIndex="{Binding Points}">