C#数据源到列表框

时间:2016-12-08 21:52:27

标签: c# wpf

我有一个sql表包含一行名为“isim”的行我希望isim行显示在列表框中

SqlConnection con = new SqlConnection("Data Source=-------");
            string aaa;
            con.Open();
            string sqlStr1 = "SELECT kurum.isim FROM kurum";
SqlDataAdapter adapter = new SqlDataAdapter(sqlStr1, con);
          
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
          
            List1.DataContext = dataSet.Tables["kurum"];
            List1.Items.Refresh();

<Window x:Class="WpfApplication1.kurum"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="kurum" Height="300" Width="300">
    <Grid>
        <!--<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="100" Margin="10,93,0,0" VerticalAlignment="Top" Width="100"/>-->

        <ListBox Name="List1" ItemsSource="{Binding}" Margin="22,84,27,23" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding isim}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</Window>

列表框始终为空,任何帮助将不胜感激

1 个答案:

答案 0 :(得分:-1)

您可以使用:

<ListBox ItemsSource="{Binding MyList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Selected, ValidatesOnDataErrors=True}">