简体   繁体   English

DataGrid绑定不适用于DataView

[英]DataGrid binding not working with DataView

I'm trying to show my database in a DataGrid in WPF, but I just can't find a way to make it work. 我试图在WPF的DataGrid中显示数据库,但我找不到能使它正常工作的方法。 I'm using DataTable and DataView to get the data into the DataGrid, but it always ends up with XamlParseException even though the DataView is filled with the info from the database. 我正在使用DataTable和DataView将数据获取到DataGrid中,但是即使DataView中充满了来自数据库的信息,它也总是以XamlParseException结尾。 I am using Visual Studio 2015. 我正在使用Visual Studio 2015。

Anyone has a clue about where the problem could be? 任何人都知道问题可能在哪里?

This is part of my WPF with the DataGrid (only with the id parameter for now): 这是我使用DataGrid的WPF的一部分(目前仅使用id参数):

`    
xmlns:vm="clr-namespace:FilmovaDatabaze.ViewModels"
<Window.Resources>
        <vm:MainVm x:Key="MainVm"/>
</Window.Resources>

<DataGrid ItemsSource="{Binding Source={StaticResource MainVm}, Path=DataView}">
            <DataGridTextColumn Header="ID" Binding="{Binding id}"/>
        </DataGrid>`

And this is part of the code in ViewModel: 这是ViewModel代码的一部分:

private DataView _dataView;
private DataTable _data;

public DataView DataView { get { return _dataView; } set { _dataView = value; this.ChangeProperty("DataView"); } }

public MainVM(){
            _enabledChanges = true;
            _data = new DataTable();
            _data = Db.GetTable("SELECT * FROM movies JOIN directors ON directors.id = movies.director_id", "id ASC");
            _dataView = new DataView(_data);}

Finally I've found the solution. 最后,我找到了解决方案。 There is a missing layer in the XAML. XAML中缺少一层。 It has to look like this: 它必须看起来像这样:

<DataGrid ItemsSource="{Binding Source={StaticResource MainVm}, Path=DataView}">
<DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding id}"/>
</Datagrid.Columns>
        </DataGrid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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