Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all!!

I´m mired in my WPF app.
I need to create a .cs class (Configuration.cs) with three properties (Name,PropA and PropB).
In my UI I want to show all the Names in the Configuration instances that I had in a listBox.

When I select an Item in the listBox, the properties PropA and PropB must shown in two different TextBoxes. Furthermore, I want these textBoxes are editable and when the user changes the value and submit the changes with a button, the appropriate Configuration instance is updated.

How can I do this? How must my Configuartion.cs class be? How I have to define the properties in this class?

Thanks for all.
Posted

here is your configurations class..

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dispacher
{
   public class Configurations
    {
        public String Name { get; set; }

        public string PropA { get; set; }

        public string PropB { get; set; }
    }



    
}


Here is your ViewModel

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows;
using System.Diagnostics;
using System.ComponentModel;

namespace Dispacher
{
   public class DisplayViewModel
    {

        public ObservableCollection<configurations> SelectedPeople { get; private set; }
        public List<configurations> AvailablePeople { get; private set; }
        public DelegateCommand Submitcommand { get; private set; }

        public string OutPut { get; set; }

               
        public DisplayViewModel()
        {
            // Constructor which will generate data for view
            AvailablePeople = new List<configurations> {
                new Configurations { Name="Basavraj", PropA = "John", PropB = "Doe" }, 
                new Configurations {Name="Mallikarjun", PropA = "Michael", PropB = "Jones" }, 
                new Configurations {Name="Swami", PropA = "Jane", PropB = "Smith" }, };

            Submitcommand = new DelegateCommand(modifiedData);
                
        }

        //here you can get modified data object
       //Put breakpoint to see modified data
        private void modifiedData()
        {
            Predicate<configurations> pred = item => item.PropA != "John" || item.PropA != "Michael" || item.PropA != "Jane";
            //modified data
            var modifieddata = AvailablePeople.Find(pred);

            if (modifieddata != null)
                OutPut = "PropA :=" + modifieddata.PropA + ",PropB :=" + modifieddata.PropB;
        }

        public class DelegateCommand : ICommand
        {

            private Action _executeMethod;

            public DelegateCommand(Action executeMethod)
            {

                _executeMethod = executeMethod;

            }

            public bool CanExecute(object parameter)
            {

                return true;

            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {

                _executeMethod.Invoke();

            }

        }
    }
}
</configurations></configurations></configurations></configurations>




here is your view

HTML
<window x:class="Dispacher.Window1" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Sample="clr-namespace:Dispacher"
        Title="Window1" Height="500" Width="500">

    <window.resources>
        <objectdataprovider>
            x:Key="Viewmodel"     
            ObjectType="{x:Type Sample:DisplayViewModel}"/>
    </objectdataprovider></window.resources>
    <grid>

        <dockpanel datacontext="{StaticResource Viewmodel}">
            <listbox name="list" dockpanel.dock="Left" width="130">
                      ItemsSource="{Binding AvailablePeople}"       
                      DisplayMemberPath="Name" 
                      SelectedValuePath="ListBox"/>
            <textbox text="{Binding ElementName=list, Path=SelectedItem.PropA}" height="50" width="100" />
            <textbox text="{Binding ElementName=list, Path=SelectedItem.PropB}" height="50" width="100" />
            <button width="20" height="50">
                    Command="{Binding Submitcommand}"  
                    CommandParameter="{Binding AvailablePeople, ElementName=list}"
                    Content=">>"/>


        </button></listbox></dockpanel>
    </grid>
</window>






Please mark accepted if got solution
 
Share this answer
 
Comments
torin86 10-Sep-13 6:02am    
Thanks! It works perfectly!
Bye.
I didn't understand the first part about properties but about the second part to change the value of a list box you can use:

C#
int a = ListBoxName.SelectedIndex;


to get the index of the selected item then you should get the items in an array by:

C#
string[] array;
int i = 0;
int b = ListBoxName.Items.Count;
while (i < b)
{
     array[i] = ListBoxName.Items[i];
     i++;
}


then edit your considered item by a text box as entered value:
C#
array[a] = textBox.Text;

at last the edition is completed and you should get back the new list box value from array:
C#
i = 0;
while (i > b);
{
ListBoxName.Items.Add(array[i]);
i++;
}

and it's done.
i hope so to help you about first part but you should explain more.
Doostl
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900