显示列表不显示在 WPF 窗体上

Displaying List does not display on WPF form

我正在尝试显示带有动态列表的表单,但 windows 没有显示任何内容。我的class如下

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

}

windowclass如下

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

首先,您应该研究使用自动属性并像这样更改您的 class :

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

关于您的数据无法访问的问题,这是因为 window 中未设置数据上下文。在构造函数中,您可以像这样将数据上下文分配给 window class :

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 public

按照建议,您应该先将 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>