显示列表不会显示在WPF表单上

时间:2015-01-13 15:14:11

标签: c# wpf

我正在尝试显示带有动态列表的表单,但窗口不显示任何内容。我的课程如下

class ManagementFunctionModel
{
    #region members
    int _RangeLeft;
    int _RangeTop;
    int _RangeRight;
    int _RangeBottom;
    #endregion

    #region properties
    public int RangeLeft
    {
        get { return _RangeLeft; }
        set { _RangeLeft = value; }
    }

public int RangeTop
{
    get { return _RangeTop; }
    set { _RangeTop = value; }
}

public int RangeRight
{
    get { return _RangeRight; }
    set { _RangeRight = value; }
}

public int RangeBottom
{
    get { return _RangeBottom; }
    set { _RangeBottom = value; }
}

#endregion

}

窗口类如下

public partial class TestWindow : Window
{

    ObservableCollection<ManagementFunctionModel> mMngModelList = new   ObservableCollection<ManagementFunctionModel>();


    public TestWindow()
    {
        mMngModelList.Add(new ManagementFunctionModel() { RangeLeft = 4 });

        InitializeComponent();
    }
}

xaml在

之下
<Window x:Class="Rules_Editor.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:Rules_Editor"        


    Title="ManagementFunctions" Height="342" Width="545">

<Grid Height="234" Width="461">
    <ListView x:Name="lstNames" ItemsSource="{Binding ManagementFunctionModel}" Margin="32,56,182,43" Grid.Column="1" Grid.Row="0">
        <ListView.View>
            <GridView x:Name="grdNames">
                <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding RangeLeft}"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>    

</Window>

请帮我找一下为什么我在GUI表单上看不到任何内容?我希望看到我在构造函数中添加的一个数字4

2 个答案:

答案 0 :(得分:1)

首先,您应该考虑使用自动属性并更改您的类:

class ManagementFunctionModel
{
    public int RangeLeft { get; set; }
    public int RangeTop { get; set; }
    public int RangeRight { get; set; }
    public int RangeBottom { get; set; }
}

关于您的数据无法访问的问题,因为未在窗口中设置datacontext。在构造函数中,您可以将数据上下文分配给窗口类,如下所示:

public ObservableCollection<ManagementFunctionModel> mMngModelList{ get; private set; }

public TestWindow()
{
    mMngModelList = new ObservableCollection<ManagementFunctionModel>();
    mMngModelList.Add(new ManagementFunctionModel() { RangeLeft = 4 });

    InitializeComponent();

    this.DataContext = this;
}

更新: 您目前拥有的绑定:

ItemsSource="{Binding ManagementFunctionModel}"

应该成为

ItemsSource="{Binding mMngModelList}"

确保将mMngModelList设为公开

答案 1 :(得分:0)

根据建议,您应首先将POCO更改为具有自动属性。如果您要在POCO中拥有动态数据(属性在运行时更改),那么您将需要实现INotifyPropertyChanged,但是现在,这将有效:

public class ManagementFunctionModel
{
    public int RangeLeft { get; set;}
    public int RangeTop { get; set;}
    public int RangeRight { get; set;}
    public int RangeBottom { get; set;}
}

接下来,在你的代码中,你想添加DataContext,以及你将被绑定的列表。由于您要绑定,因此列表必须是属性。

public partial class TestWindow : Window
{
    public ObservableCollection<ManagementFunctionModel> MyList { get; private set; }

    public TestWindow()
    {
        InitializeComponent();

        MyList = new ObservableCollection<ManagementFunctionModel>();
        MyList.Add(new ManagementFunctionModel() { RangeLeft = 4 });

        DataContext = this;
    }
}

最后,您需要更新XAML中的绑定语法以使用新的列表属性。

<Grid Height="234" Width="461">
    <ListView x:Name="lstNames" ItemsSource="{Binding MyList}" Margin="32,56,182,43" Grid.Column="1" Grid.Row="0">
        <ListView.View>
            <GridView x:Name="grdNames">
                <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding RangeLeft}"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>