简体   繁体   中英

C# WPF Datagrid - Setting Default Selected Value of Data Bound Combo Box

I have an application that uses a WPF DataGrid, to allow users to fill out a survery of sorts. One portion of this allows the user select the amount of points to deduct for every item. The values seem to display fine, but I would like 0 to be selected by default (as of now, nothing is), so that a user does not have to manually make a selection if all is well. I have tried setting the SelectedItem and SelectedValues in the XAML to 0 with no success. How might I achieve this?

My 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>

and my 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;






        }




    }
}

Thank you so much!

您应该设置选定的索引

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

根据您建立分数列表的方式,您也可以使用点的值作为选择的索引,而无需更改列表顺序

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM