显示有关数据绑定列表框的错误

时间:2016-04-14 04:32:38

标签: c# wpf ms-access data-binding listbox

允许我先描述错误。

1。显示顺序错误

列表框将以第二个开头显示项目,第一个显示最后一个而不是四个。最后一项也有同样的问题。

first item missing

last 3 or 4 items order mess

2。行缺少错误

当我将列表框与一个行数超过400的数据表绑定时,第一个项目将会丢失,最后一个项目也将丢失。

然后在此处发布代码

1。使用accdb存储数据

这里是以oledb方式从访问数据库获取数据的c#代码。我希望我做对了......

public partial class MainWindow : Window
{
    DataSet DsMain = new DataSet();
    OleDbConnection Conn = new OleDbConnection();
    OleDbDataAdapter Dpter = new OleDbDataAdapter();

    public MainWindow()
    {
        InitializeComponent();

        string StartPath = System.Windows.Forms.Application.StartupPath + @"\AltiumDatabase.accdb";
        Conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + StartPath);

        try
        {
            Conn.Open();

            string strcmd = "select ID,TaskType, Status,StatusColor,PN,Type,Owner,Checker,Deadline,StartDate,Tag from TRRecord";
            OleDbCommand cmd = new OleDbCommand(strcmd, Conn);
            Dpter.SelectCommand = cmd;

            Dpter.Fill(DsMain, "TRRecord");

            LbxMain.DataContext = DsMain;//bind to the listbox
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            Conn.Close();
        }
    }
}

2。自定义列表框构建

我希望我能得到所有有约束力的declears ...

<ListBox Name="LbxMain" Margin="20" ItemsSource="{Binding Path=TRRecord}" HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="1" BorderBrush="Gray" Margin="5">
                    <Grid Background="{Binding Path=StatusColor}">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="200"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=ID}"/>
                        <TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding Path=PN}"/>
                        <TextBlock Grid.Row="2" Grid.Column="0" Text="{Binding Path=Type}"/>
                        <TextBlock Grid.Row="3" Grid.Column="0" Text="{Binding Path=TaskType}"/>
                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Status}"/>
                    </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我应该使用INotifyPorpertyChanged还是ObservableCollection?我只是在填充表后绑定数据,所以没有任何变化,那么它们真的重要吗? 如果有人帮忙,我真的很感激。并感谢大家阅读这篇文章。

错误......我的英语不太好。所以,如果某个地方我没有清楚地描述,请告诉我。

1 个答案:

答案 0 :(得分:0)

我认为你应该尝试下一种绑定方式。

LbxMain.DataContext = DsMain.Tables["TRRecord"];

另外在您的XAML中:

<ListBox Name="LbxMain" Margin="20" ItemsSource="{Binding }" HorizontalContentAlignment="Stretch"> ... </ListBox>

问候。