2016-05-12 84 views
0

我有一个使用WPF DataGrid的应用程序,允许用户填写各种排序。其中的一部分允许用户选择每个项目的扣分数量。这些值似乎显示正常,但我想默认情况下选择0(截至目前,什么都没有),所以如果一切正常,用户不必手动进行选择。我尝试将XAML中的SelectedItem和SelectedValues设置为0,但没有成功。我怎么能做到这一点?C#WPF Datagrid - 设置数据绑定组合框的默认选定值

我的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; 






     } 




    } 
} 

太谢谢你了!

回答

2

你应该设置选定的指数

<ComboBox ItemsSource="{Binding Score}" SelectedIndex="0" /> 
+0

谢谢!以防万一它帮助其他人,对于我的特定场景,我还需要对列表进行排序,以便选择0(而不是最高值)。再次感谢。 – KellyMarchewa

1

基于你如何建立你的分数列表中,您还可以使用点的值是您选择的索引,而无需更改列表顺序

<ComboBox ItemsSource="{Binding Score}" SelectedIndex="{Binding Points}">