1
<Window x:Class="Kvik_Support.KonsulentValg"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="KonsulentValg" Height="150" Width="250"
        Background="LightGray" ResizeMode="NoResize">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10" />
            </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="10" />
            <RowDefinition Height="10*" />
            <RowDefinition Height="10*" />
            <RowDefinition Height="10*" />
            <RowDefinition Height="10" />
        </Grid.RowDefinitions>
        <Label Content="Hvem er du?" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
        <ComboBox Grid.Column="1" Grid.Row="2" Height="25" Name="cmbHvemErDu">

        </ComboBox>
        <Button Grid.Column="1" Grid.Row="3" Height="25" Width="100" Content="OK" Click="Button_Click" />
    </Grid>
</Window>

这是我的窗口的 XAML 代码,我正在尝试用数据库中的项目填充组合框......

我的想法是我可以用以下代码填写项目,但这会导致 XAML 解析异常......

   public partial class KonsulentValg : Window
    {
        public KonsulentValg()
        {
            InitializeComponent();
            try
            {
                SqlConnection lager = new SqlConnection("Data Source=local\\SERVER;Initial Catalog=Kvik-Support;Integrated Security=True");
                lager.Open();
                SqlCommand thisCommand = lager.CreateCommand();
                string command = "SELECT ID, navn FROM Konsulent";
                SqlDataAdapter da = new SqlDataAdapter(command, lager);
                SqlDataReader thisReader = thisCommand.ExecuteReader();
                DataTable datatable = new DataTable();
                da.Fill(datatable);
                foreach (DataRow row in datatable.Rows)
                {
                    string rows = string.Format("{0}:{1}", row.ItemArray[0], row.ItemArray[1]);
                    cmbHvemErDu.Items.Add(rows);
                }
                Console.ReadKey();
                thisReader.Close();
                lager.Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}

但它可能没有工作......

4

1 回答 1

1

您应该考虑使用绑定 itemssource 而不是将项目添加到组合框中。

  this.cmdHvemErDu.ItemsSource = datatable;

或在 xaml

  <ComboBox ItemsSource="{Binding YourDataTableProperty}"/>

不要忘记设置组合框的 DisplayMemeberPath 属性

编辑:如果您想在组合框中显示多于一列的值,您可以使用 ItemTemplate。

    <ComboBox ItemsSource="{Binding YourDataTablePropertyHere}" >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding ColumnName1}" />
                        <TextBlock Text="{Binding ColumnName2}" />
                    </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
于 2012-04-27T07:41:56.047 回答