user2837961
user2837961

Reputation: 1555

Displaying List does not display on WPF form

I am trying to display a form with a dynamic list but the windows does not display anything. My class is as under

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

}

The window class is as below

public partial class TestWindow : Window
{

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


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

        InitializeComponent();
    }
}

The xaml is as under

<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>

Please help me to find why I don't see anything on the GUI form? I expect to see one number 4 which I added in the constructor

Upvotes: 0

Views: 226

Answers (2)

Franck
Franck

Reputation: 4440

First you should look into using auto properties and change your class like this :

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

About the issue where your data is not accessible it's because of the datacontext not being set in the window. In the constructor you can assign the data context to the window class like this :

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

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

    InitializeComponent();

    this.DataContext = this;
}

Update: the binding you currently have as :

ItemsSource="{Binding ManagementFunctionModel}"

should become

ItemsSource="{Binding mMngModelList}"

Make sure you make mMngModelList public

Upvotes: 1

d.moncada
d.moncada

Reputation: 17402

As suggested, you should first change your POCO to have automatic properties. If you are going to have dynamic data within the POCO (properties change during run-time) you will then need to implement INotifyPropertyChanged, but for now, this will work:

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

Next, in your code behind, you want to add the DataContext, as well as the the list you will be bound to. Since you are going to bind, the list needs to be a property.

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;
    }
}

Lastly, you need to update your binding syntax in your XAML to use the new list property.

<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>   

Upvotes: 0

Related Questions