使用ObjectDataProvider

时间:2016-09-23 01:31:54

标签: .net wpf xaml listview

大家好,我试图找出我的ListView加载为空的原因。

我已经ObjectDataProvider从我的控制器加载Articulos

中的DataTable列表

TrabajarArticulos.cs

namespace ClasesBase
{
    public class TrabajarArticulos
    {
        public static DataTable TraerArticulos()
        {
            SqlConnection cnn = new SqlConnection(ClasesBase.Properties.Settings.Default.conexion);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Articulo";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cnn;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);           
            return dt;
        }
    }
}

Articulos.xaml

<Window.Resources>
    <ObjectDataProvider x:Key="lista_articulos" ObjectType="{x:Type svc:TrabajarArticulos}" MethodName="TraerArticulos"></ObjectDataProvider>
</Window.Resources>

这用于ListView

中的相同窗口
<ListView Name="ListArt" 
              ItemsSource="{Binding Source={StaticResource ResourceKey=lista_articulos}}"
              Margin="360,61,22,184">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Id" Width="50" DisplayMemberBinding="{Binding Path=Art_Id}"></GridViewColumn>
                    <GridViewColumn Header="Descripcion" Width="100" DisplayMemberBinding="{Binding Path=Art_Descrip}"></GridViewColumn>
                </GridView.Columns>                    
            </GridView>
        </ListView.View>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

但是当它加载时它是空白的(不是空的)Example

我的第一个想法是它无法加载,但是使用PreviewMouseLeftButtonDown进行调试我可以看到我的数组的值。

还尝试使用DataContext代替ItemsSource,但仍然没有结果。

即使我认为我在GridViewColumn上绑定他们错了,但仍然没有运气。

知道我做错了吗?

编辑我注意到如果我手动添加我的对象Articulo,它就会显示出来。现在让我在错误中迷失方向。

ListArt.Items.Add(articulo);

感谢jstreet的反馈,我发现这个问题非常基础。

ColumnName中的所有DataTable都是小写字母而不是大写字母作为我的模型。

所以GridViewColumn应该是这样的:

<GridViewColumn Header="Id" Width="50" DisplayMemberBinding="{Binding Path=art_id}"></GridViewColumn>
<GridViewColumn Header="Descripcion" Width="100" DisplayMemberBinding="{Binding Path=art_descrip}"></GridViewColumn>

1 个答案:

答案 0 :(得分:1)

这实际上取决于您的TrabajarArticulos实施。这是一些工作示例代码:

<强> XAML:

<Window x:Class="WpfApplication286.MainWindow"
    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:WpfApplication286"
    mc:Ignorable="d"
    Title="MainWindow" Height="300" Width="300">
<Window.Resources>

    <ObjectDataProvider x:Key="Source1" 
                        ObjectType="{x:Type local:MyDataTable}">
    </ObjectDataProvider>

</Window.Resources>

<Grid>

    <ListView ItemsSource="{Binding Source={StaticResource ResourceKey=Source1}}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Path=ID}"></GridViewColumn>
                    <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Path=Description}"></GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

</Grid>

<强>数据表:

public class MyDataTable : DataTable
{
    public MyDataTable()
    {
        Columns.Add("ID", typeof(int));
        Columns.Add("Description", typeof(string));

        for (int i = 1001; i <= 1005; i++)
        {
            DataRow row = NewRow();
            row["ID"] = i;
            row["Description"] = string.Format("Data item from table {0}", i);
            Rows.Add(row);
        }
    }
}

enter image description here

编辑:请注意ObjectDataProviderResource。如果你打算在运行时动态填充它......也许你最好只坚持使用标准的MVVM方法。尽管如此,对于我们的测试目的,从本地数据库检索数据仍然以相同的方式工作:

<强> XAML:

<ObjectDataProvider x:Key="Source1" 
                        ObjectType="{x:Type local:MyDataTable}"
                        MethodName="GetDbData">
</ObjectDataProvider>

<强>数据表:

public DataTable GetDbData()
    {
        SqlConnection cnn = new SqlConnection(con);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "SELECT * FROM Production.Product";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = cnn;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        foreach (DataRow dr in dt.Rows)
        {
            DataRow row = NewRow();
            row["ID"] = dr["ProductID"];
            row["Description"] = dr["Name"];
            Rows.Add(row);
        }

        return this;
    }

enter image description here