Created
March 27, 2015 22:06
-
-
Save kaustubh321/e17b6a7508099ba111bf to your computer and use it in GitHub Desktop.
dynamically binf object list to listbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
using System.Collections.ObjectModel; | |
using Xceed.Wpf.Toolkit; | |
using System.Runtime.CompilerServices; | |
using System.ComponentModel; | |
namespace listbox | |
{ | |
public partial class MainWindow : Window | |
{ | |
public static List<string>vml= new List<string>{"sagar","kautubh","gaurav","abhi"}; | |
public class User | |
{ | |
public string filename { get; set; } | |
public List<CheckVM> temp = new List<CheckVM>(); | |
public string Filename | |
{ | |
get { return filename; } | |
set { this.filename = value; } | |
} | |
public User(string fname) | |
{ | |
filename = fname; | |
for (int i = 0; i < vml.Count; i++) | |
{ | |
temp.Add(new CheckVM(vml[i])); | |
} | |
} | |
} | |
public class CheckVM : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
public string vmname; | |
public string VmName | |
{ | |
get | |
{ | |
return this.vmname; | |
} | |
set | |
{ | |
if (value != this.vmname) | |
{ | |
this.vmname = value; | |
NotifyPropertyChanged(); | |
} | |
} | |
} | |
public bool ischecked; | |
public bool IsCheck | |
{ | |
get | |
{ | |
return this.ischecked; | |
} | |
set | |
{ | |
if (value != this.ischecked) | |
{ | |
this.ischecked = value; | |
NotifyPropertyChanged(); | |
} | |
} | |
} | |
public CheckVM(string name) | |
{ | |
this.vmname = name; | |
this.ischecked = false; | |
} | |
public CheckVM(string name,bool val) | |
{ | |
this.vmname = name; | |
this.ischecked= val; | |
} | |
public CheckVM() | |
{ | |
// TODO: Complete member initialization | |
} | |
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") | |
{ | |
if (PropertyChanged != null) | |
{ | |
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
} | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
this.DataContext = this; | |
listbox1.Items.Add("Morgan Stanley"); | |
listbox1.Items.Add("Nvidia"); | |
listbox1.Items.Add("ADP"); | |
listbox1.Items.Add("Sokrati"); | |
listbox1.Items.Add("Clarice"); | |
} | |
public void show1(User u) | |
{ | |
if (listbox3.Items.Count > 0) | |
{ | |
listbox3.Items.Clear(); | |
} | |
foreach (CheckVM v in u.temp) | |
{ | |
listbox3.Items.Add(new CheckVM() { vmname = v.vmname,ischecked=v.ischecked}); | |
} | |
} | |
private void cb_check(object sender, RoutedEventArgs e) | |
{ | |
} | |
private void CheckBox_Unchecked(object sender, RoutedEventArgs e) | |
{ | |
CheckBox a = sender as CheckBox; | |
bool x = (bool)a.IsChecked; | |
//int i= | |
} | |
private void plus_popup(object sender, SelectionChangedEventArgs args) | |
{ | |
User x = ((sender as ListBox).SelectedItem as User); | |
show1(x); | |
if (listbox3.Visibility.Equals(Visibility.Hidden)) | |
{ | |
listbox3.Visibility = Visibility.Visible; | |
} | |
} | |
private void Button_Click_1(object sender, RoutedEventArgs e) | |
{ | |
foreach (string item in listbox1.SelectedItems) | |
{ | |
User u = new User(item); | |
listbox2.Items.Add(u); | |
} | |
listbox1.SelectedItems.Clear(); | |
foreach (User item2 in listbox2.Items) | |
{ | |
listbox1.Items.Remove(item2.filename); | |
} | |
} | |
private void Button_Click_2(object sender, RoutedEventArgs e) | |
{ | |
REMOVE(); | |
} | |
private void REMOVE() | |
{ | |
List<string> temp = new List<string>(); | |
foreach (User item3 in listbox2.SelectedItems) | |
{ | |
listbox1.Items.Add(item3.filename); | |
temp.Add(item3.filename); | |
} | |
listbox2.SelectedItems.Clear(); | |
List<int>num =new List <int>(); | |
int cnt = 0; | |
foreach(User u in listbox2.Items) | |
{ | |
foreach (string item in temp) | |
{ | |
if (item == u.filename) | |
{ | |
num.Add(cnt); | |
break; | |
} | |
} | |
cnt++; | |
} | |
for(int i=num.Count -1;i>=0;i--) | |
{ | |
listbox2.Items.RemoveAt(num[i]); | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="listbox.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" | |
Title="MainWindow" Height="350" Width="743"> | |
<Window.Resources> | |
<DataTemplate x:Key="UserTemplate" > | |
<StackPanel Orientation="Horizontal"> | |
<TextBlock Text="{Binding Path=filename}" Width="100"/> | |
</StackPanel> | |
</DataTemplate> | |
<DataTemplate x:Key="UserTemplate1" > | |
<StackPanel Orientation="Horizontal"> | |
<CheckBox Name="cb" IsThreeState="False" Content="{ Binding vmname}" Unchecked="CheckBox_Unchecked" Checked="cb_check" IsChecked="{Binding Path=ischecked, Mode=TwoWay }" /> | |
</StackPanel> | |
</DataTemplate> | |
</Window.Resources> | |
<Grid HorizontalAlignment="Left" Width="733" RenderTransformOrigin="0.514,0.556"> | |
<ListBox Name="listbox3" Visibility="Hidden" Height="134" Width="156" Margin="515,82,62,104" ItemsSource="{Binding ElementName=listbox2, Path=SelectedItem, BindsDirectlyToSource=True }" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource UserTemplate1}" /> | |
<ListBox Name="listbox2" SelectionChanged="plus_popup" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource UserTemplate}" | |
Margin="268,82,0,0" HorizontalAlignment="Left" Height="188" VerticalAlignment="Top" Width="169" SelectionMode="Single" RenderTransformOrigin="0.517,0.505"/> | |
<ListBox x:Name="listbox1" HorizontalAlignment="Left" Height="188" Margin="10,82,0,0" VerticalAlignment="Top" Width="144" SelectionMode="Multiple"/> | |
<Button Content=">>" HorizontalAlignment="Left" Margin="195,127,0,0" VerticalAlignment="Top" Width="37" Height="28" Click="Button_Click_1" FontWeight="Bold" FontSize="16"/> | |
<Button Content="<<" HorizontalAlignment="Left" Margin="195,197,0,0" VerticalAlignment="Top" Width="37" Height="31" Click="Button_Click_2" FontWeight="Bold" FontSize="16" RenderTransformOrigin="0.5,0.5"> | |
<Button.RenderTransform> | |
<TransformGroup> | |
<ScaleTransform/> | |
<SkewTransform AngleX="-1.591"/> | |
<RotateTransform/> | |
<TranslateTransform X="-0.472"/> | |
</TransformGroup> | |
</Button.RenderTransform> | |
</Button> | |
<Label Content="List 1" HorizontalAlignment="Left" Margin="50,51,0,0" VerticalAlignment="Top"/> | |
<Label Content="List 2" HorizontalAlignment="Left" Margin="302,51,0,0" VerticalAlignment="Top"/> | |
</Grid> | |
</Window> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment