简体   繁体   中英

MVVM WPF Data Binding issue

I am new to MVVM and WPF, I have the below model which I want to show it to user using the data binding (Code for viewmodel and xaml is also provided below). But I am not sure what is missing, because the list of users are not being shown on UI at all. Can anyone tell me which I am missing from my code?!

If instead Class of Users, I use a List from class User:

  private List<User> _UsersList;
        public List<User> users
        {
            get { return _UsersList; }
            set { _UsersList = value; }
        }

, then the biding works!, but if I use Users class, the binding does not work!

Model:

 public class User : ObservableObject
 {

        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public bool IsPremiumUser { get; set; }
        public string selectedItem { get; set; }
        public string SelectedValue
        {
            get { return selectedItem; }
            set
            {
                selectedItem = value;
                //  suser.age = Convert.ToInt32(value);
                RaisePropertyChangedEvent("SelectedValue");
            }

        }
        public int[] myItem { get; set; }
        public int[] UserItems
        {
            get { return myItem; }
            set { myItem = value; }
        }
        private SelectedUser suser = new SelectedUser();
        public int selected { get; set; }
        public int Selected
        {
            get { return selected; }
            set
            {
                selected = value;
                suser.age = Convert.ToInt32(value);
                RaisePropertyChangedEvent("Selected");
            }

        }
    }
   public class Users : ObservableObject
    {
        private List<User> mylist = new List<User>();
        private List<User> list
        {
            get { return mylist; }
            set
            {
                mylist = value;

            }
        }
        public void Add(User user)
        {
            this.list.Add(user);
        }

        public User GetById(int id)
        {
            return this.list.First(u => u.ID == id);
        }

    }

ViewModel:

 public class ViewModel : ObservableObject
 {

        public ViewModel()
        {
            users = new Users();
            for (int i = 0; i < 100000; ++i)
            {
                int[] numbers;
                numbers = new int[3] { 1, 2, 3 };
                var user = new User { ID = i, Name = "Name " + i.ToString(), Age = 20 + i, UserItems = numbers, SelectedValue = "0" };
                if (i == 2 || i == 4)
                {
                    user.IsPremiumUser = true;
                }
                users.Add(user);
            }

        }
        private Users _UsersList;
        public Users users
        {
            get { return _UsersList; }
            set { _UsersList = value; }
        }

        private int _currenuser;
        public int CurrentUser
        {
            get
            {
                return _currenuser;
            }
            set
            {
                _currenuser = value;

                RaisePropertyChangedEvent("CurrentUser");
            }
        }
    }

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Icon="C:\Users\maninx2\Documents\Visual Studio 2013\Projects\SampleFormMaker1\WpfApplication1\abbott_point_of_care_HuT_icon.ico"
        Title="MainWindow" Height="700" Width="1249.129">
    <Window.Resources>
        <DataTemplate x:Key="NormalUserDataTemplate">
            <StackPanel>
                <TextBlock Text="{Binding Name}" />
                <TextBox Text="{Binding SelectedValue, UpdateSourceTrigger=PropertyChanged}" Width="300" />

            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="PremiumUserDataTemplate">
            <StackPanel Background="LightBlue">
                <ComboBox  SelectedIndex="{Binding SelectedValue, UpdateSourceTrigger=PropertyChanged}" Text="{Binding Name}"  Width="300" ItemsSource="{Binding UserItems}"/>

            </StackPanel>
        </DataTemplate>
        <local:PremiumUserDataTemplateSelector x:Key="myPremiumUserDataTemplateSelector" />
    </Window.Resources>
    <TabControl Name="TabControl1">
        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Visibility" Value="Collapsed"/>
            </Style>
        </TabControl.ItemContainerStyle>
        <TabItem Header="General">
            <ScrollViewer VerticalScrollBarVisibility="Auto">
                <StackPanel Height="800" Orientation="Horizontal" >
                    <ListView x:Name="myListView" ItemsSource="{Binding users,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplateSelector="{StaticResource myPremiumUserDataTemplateSelector}" SelectedIndex="{Binding CurrentUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                    </ListView>
                    <ListView x:Name="myListView1" ItemsSource="{Binding users,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"   HorizontalAlignment="Stretch" >
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Width="140" Header="Selected Values"
                 DisplayMemberBinding="{Binding SelectedValue}"  />

                            </GridView>
                        </ListView.View>
                    </ListView>
                    <Button Content="Next"
        HorizontalAlignment="Left"
        Margin="10,10,0,0"
        VerticalAlignment="Top"
        Width="75"
        Click="Button_Click"/>
                </StackPanel>

            </ScrollViewer>

        </TabItem>
        <TabItem Header="Second Tab">
            <StackPanel>
                <TextBlock Name="lbl1" Text="{Binding CurrentUser, UpdateSourceTrigger=PropertyChanged}"/>

            <Button Content="Previous"
        HorizontalAlignment="Left"
        Margin="10,10,0,0"
        VerticalAlignment="Top"
        Width="75"
        Click="Button_Click1"/>
            </StackPanel>
        </TabItem>

    </TabControl>

</Window>

You are missing inheriting the Users from ObservableCollection. So, WPF engine does not know how to get the data from this class.

Update:

@user3033921: You inherited it from ObservableObject not ObservableCollection. So, the thing is if you want this class to be recognized from as a list then you would have to get that class inherited by an ICollection object and if you want that class to be observable it should be implementing both ICollection and INotifyPropertyChange. So, to @BradleyDotNet's point if you dont have any specific reason to create your own type, then just create a object of users with type ObservableCollection. If you have a specific need to have a type Users then you can derive it from ObserableCollection as recommended solution. But keep in mind you do not want to have List in the Users class anymore as your class is now by itself is a list. So, if you have any specific implementation to do like GetByID, then the class would look like:

public class Users :ObservableCollection<User>
{
    public User GetById(int id)
    {
        return this.First(u => u.ID == id);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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