I have a group class which contains a list of items and a header:
public class MyGroup {
public MyGroup(string _header){
header = _header;
}
protected string header = "";
public string Header
{
get { return header; }
}
protected List<MyGroupItem> item = new List<MyGroupItem>();
public List<MyGroupItem> Item
{
get { return item; }
}
}
public class MyGroupItem {
public MyGroupItem(string _name, double _multiplier){
name = _name;
multiplier = _multiplier;
}
protected double multiplier = 1.0;
protected string name = "";
public string Name
{
get { return name; }
}
}
So far so good. In my main class, I have an observable collection of groups and I populate it like this:
protected ObservableCollection<MyGroup> groups = new ObservableCollection<MyGroup>();
public ObservableCollection<MyGroup> Groups
{
get { return groups; }
}
protected MyGroupItem currentItem;
public MyGroupItem CurrentItem
{
get { return currentItem; }
set
{
if (currentItem== value) return;
currentItem= value;
NotifyPropertyChanged("CurrentItem");
}
}
....
var GroupA = new MyGroup("Group A");
GroupA.MyGroupItem.Add("Item 1", 1.0);
Groups.Add(GroupA);
currentItem = GroupA.MyGroupItem[0];
All of the above simply shows how I've setup my classes and observable lists. Now, I switch over to the xaml.
<ItemsControl ItemsSource="{Binding Path=Groups}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="Transparent" ClipToBounds="True" Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:MyGroup}">
<StackPanel>
<TextBlock Text="{Binding Path=Header}"></TextBlock>
<ListView ItemsSource="{Binding Path=MyGroupItem}" SelectedItem="{Binding Path=DataContext.CurrentItem, ElementName=ControlRoot}">
<ListView.ItemTemplate>
<DataTemplate DataType="local:MyGroupItem">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
So, basically I have a ItemControl that displays multiple lists of items. The control should place the header for the name of the group, and then show a list view of the particular items within that group. The layout works perfectly... however, the issue comes when I deal with the Selected Item. Basically, the list view allows me to select an item within one of the multiple groups... meaning that I may have multiple items selected at any given time. For example, lets say that I select the first item in Group A. But, then I change my selection to the second item in Group B. Because Group B is a separate list, it allows me to activate that item... but it doesn't deselect the item in Group A. What I'd like is that this multi-list group to act as a single list. Is this possible? Do I need to setup a separate SelectionChanged event? And if so, how would I go about making sure when the selection is changed that it clears the selected items from all lists and only shows the correct one that the user just selected?
You should handle this in your view model classes.
If you add a property to hold the selected item of each group to the MyGroup class and implement the INotifyPropertyChanged interface, you could handle the CollectionChanged event of the Groups collection in the view model class to set the CurrentItem property and at the same time clear the SelectedItem property of the other groups by setting it to null in this event handler.
Here is an example for you.
MyGroup.cs:
public class MyGroup : INotifyPropertyChanged
{
public MyGroup(string _header)
{
header = _header;
}
protected string header = "";
public string Header
{
get { return header; }
}
protected List<MyGroupItem> item = new List<MyGroupItem>();
public List<MyGroupItem> Item
{
get { return item; }
}
private MyGroupItem _item;
public MyGroupItem SelectedItem
{
get { return _item; }
set { _item = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
MyGroupItem.cs:
public class MyGroupItem
{
public MyGroupItem(string _name, double _multiplier)
{
name = _name;
multiplier = _multiplier;
}
protected double multiplier = 1.0;
protected string name = "";
public string Name
{
get { return name; }
}
}
View Model:
public class Window1ViewModel : INotifyPropertyChanged
{
public Window1ViewModel()
{
groups.CollectionChanged += (s, e) =>
{
if (e.NewItems != null)
{
foreach (object item in e.NewItems)
{
(item as INotifyPropertyChanged).PropertyChanged
+= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
if (e.OldItems != null)
{
foreach (object item in e.OldItems)
{
(item as INotifyPropertyChanged).PropertyChanged
-= new PropertyChangedEventHandler(item_PropertyChanged);
}
};
};
var GroupA = new MyGroup("Group A");
GroupA.Item.Add(new MyGroupItem("Item 1", 1.0));
GroupA.Item.Add(new MyGroupItem("Item 2", 1.0));
GroupA.Item.Add(new MyGroupItem("Item 3", 1.0));
Groups.Add(GroupA);
var GroupB = new MyGroup("Group B");
GroupB.Item.Add(new MyGroupItem("Item 1", 1.0));
GroupB.Item.Add(new MyGroupItem("Item 2", 1.0));
GroupB.Item.Add(new MyGroupItem("Item 3", 1.0));
Groups.Add(GroupB);
currentItem = GroupA.Item[0];
}
private bool _handle = true;
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (!_handle)
return;
MyGroup group = sender as MyGroup;
CurrentItem = group.SelectedItem;
//clear the selection in the other groups:
_handle = false;
foreach (MyGroup g in Groups)
if (g != group)
g.SelectedItem = null;
_handle = true;
}
protected ObservableCollection<MyGroup> groups = new ObservableCollection<MyGroup>();
public ObservableCollection<MyGroup> Groups
{
get { return groups; }
}
protected MyGroupItem currentItem;
public MyGroupItem CurrentItem
{
get { return currentItem; }
set
{
if (currentItem == value) return;
currentItem = value;
NotifyPropertyChanged("CurrentItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
View:
<ItemsControl ItemsSource="{Binding Path=Groups}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="Transparent" ClipToBounds="True" Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:MyGroup}">
<StackPanel>
<TextBlock Text="{Binding Path=Header}"></TextBlock>
<ListView ItemsSource="{Binding Path=Item}"
SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate DataType="local:MyGroupItem">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Related
I'm pretty new to programming with WPF and C# and I have a question regarding the possibility to automatically check all the CheckBoxes in a Listbox. I'm developing a plugin for Autodesk Revit and, after having listed all the names of the rooms in a list box, I want to check them all using the button "Check All"
I've read the thread at this page but still, I'm not able to make it work. May someone help me with my code?
Here is what I've done:
XAML:
<ListBox x:Name='roomlist'
SelectionMode='Multiple'>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked='{Binding IsChecked}'
Content="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.InputBindings>
<KeyBinding Command="ApplicationCommands.SelectAll"
Modifiers="Ctrl"
Key="A" />
</ListBox.InputBindings>
<ListBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.SelectAll" />
</ListBox.CommandBindings>
</ListBox>
C#
public partial class RoomsDistance_Form : Window
{
UIDocument _uidoc;
Document _doc;
public RoomsDistance_Form(Document doc, UIDocument uidoc)
{
InitializeComponent();
FilteredElementCollector collector = new FilteredElementCollector(doc)
.WhereElementIsNotElementType()
.OfCategory(BuiltInCategory.OST_Rooms);
List<String> myRooms = new List<String>();
foreach (var c in collector)
{
myRooms.Add(c.Name);
}
myRooms.Sort();
roomlist.ItemsSource = myRooms;
}
private void checkAllBtn_Click(object sender, RoutedEventArgs e)
{
foreach (CheckBox item in roomlist.Items.OfType<CheckBox>())
{
item.IsChecked = true;
}
}
public class Authority : INotifyPropertyChanged
{
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Thank you very much for your help!
In the thread you are linking to, they are setting the "IsChecked" on the data object (Authority), not the CheckBox control itself.
foreach (var a in authorityList)
{
a.IsChecked = true;
}
You have a binding to IsChecked that will update the Checkbox control when NotifyPropertyChanged() is called.
After having lost my mind in the effort i solved my problem by avoiding the Listbox.. I simply added single CheckBoxes in the StackPanel.
XAML:
<ScrollViewer Margin='10,45,10,100'
BorderThickness='1'>
<StackPanel x:Name='stack'
Grid.Column='0'></StackPanel>
</ScrollViewer>
C#:
foreach (var x in myRooms)
{
CheckBox chk = new CheckBox();
chk.Content = x;
stack.Children.Add(chk);
}
Not what i was looking for but now it works and that's the point.
Thank you for your help!
I usually use CheckBoxList in the following way:
In xaml:
<ListBox ItemsSource="{Binding ListBoxItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> //+some dimensional properties
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In xaml.cs:
public partial class MyWindow : Window
{
public ViewModel ViewModel {get; set; }
public MyWindow(ViewModel viewModel)
{
//keep all the mess in ViewModel, this way your xaml.cs will not end up with 1k lines
ViewModel = viewModel;
DataContext = ViewModel;
InitializeComponent();
}
void BtnClick_SelectAll(object sender, RoutedEventArgs e)
{
ViewModel.CheckAll();
}
}
ViewModel preparation:
public class ViewModel
{
public List<ListBoxItem> ListBoxItems { get; set; }
//InitializeViewModel()...
//UpdateViewModel()...
//other things....
public void CheckAll()
{
foreach (var item in ListBoxItems)
{
item.IsSelected = true;
}
}
public class ListBoxItem : INotifyPropertyChanged
{
public string Name { get; set; }
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (_isSelected != value)
{
_isSelected = value;
OnPropertyChanged(nameof(IsSelected));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
I've got ComboBox and ListBox of CheckBoxes. Depending on SelectedItem of ComboBox ItemSource of ListBox must change. I made a sample to make thing easier. Here is the code:
ViewModel
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace Test
{
class Data
{
public long Id;
public object Value;
public override string ToString()
{
return Value.ToString();
}
}
class CheckedData: INotifyPropertyChanged
{
private Data myData;
public Data MyData
{
get { return myData; }
set
{
if (myData == value)
return;
myData = value;
RaisePropertyChanged(nameof(MyData));
}
}
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
RaisePropertyChanged(nameof(IsChecked));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
class BindingObject: INotifyPropertyChanged
{
private ObservableCollection<Data> dataList = new ObservableCollection<Data>();
public ObservableCollection<Data> DataList
{
get { return dataList; }
set
{
dataList = value;
RaisePropertyChanged(nameof(DataList));
}
}
private Data selectedItem;
public Data SelectedItem
{
get { return selectedItem; }
set
{
if (value == selectedItem)
return;
selectedItem = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
class ViewModel: INotifyPropertyChanged
{
public ViewModel()
{
var tmp = new Data() {Id = 1, Value = "Cat"};
Obj.DataList.Add(tmp);
Obj.SelectedItem = tmp;
Obj.DataList.Add(new Data() {Id = 2, Value = "Dog"});
Mapping[1] = new ObservableCollection<CheckedData>()
{
new CheckedData() {IsChecked = true, MyData = new Data() {Id = 1, Value = "Maine coon"}},
new CheckedData() {IsChecked = true, MyData = new Data() {Id = 2, Value = "Siberian"}}
};
}
private BindingObject obj = new BindingObject();
public BindingObject Obj
{
get { return obj; }
set
{
if (obj == value)
return;
obj = value;
RaisePropertyChanged(nameof(Obj));
}
}
private Dictionary<long, ObservableCollection<CheckedData>> mapping = new Dictionary<long, ObservableCollection<CheckedData>>();
public Dictionary<long, ObservableCollection<CheckedData>> Mapping
{
get { return mapping; }
set
{
if (mapping == value)
return;
mapping = value;
RaisePropertyChanged(nameof(Mapping));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
View
<ComboBox x:Name="comboBox" ItemsSource="{Binding Path=Obj.DataList}" SelectedItem="{Binding Path=Obj.SelectedItem, Mode=TwoWay}"/>
<ListBox x:Name="listBox" Height="100" ItemsSource="{Binding Path=Mapping[Obj.SelectedItem.Id]}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding Path=MyData.Value}" Margin="0,5,5,0"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
That is what I thought should work. ComboBox is okay, but ListBox ItemSource binding doesn't work. Only if I bind directly to list like this:
ViewModel
private ObservableCollection<CheckedData> test = new ObservableCollection<CheckedData>()
{
new CheckedData() {IsChecked = true, MyData = new Data() {Id = 1, Value = "Maine coon"}},
new CheckedData() {IsChecked = false, MyData = new Data() {Id = 2, Value = "Siberian"}}
};
public ObservableCollection<CheckedData> Test
{
get { return test; }
set
{
test = value;
RaisePropertyChanged(nameof(Test));
}
}
View
<ListBox x:Name="listBox" Height="100" ItemsSource="{Binding Path=Test}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding Path=MyData.Value}" Margin="0,5,5,0"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Everything starts working.. Except Content binding, because I can't go deeper than 1 level of property path. So I have to override ToString() method in Data.
What should I fix to make everything work? Am I able to bind ItemSource like this? Why can't I go deeper than 1 lvl property binding in CheckBox?
Am I able to bind ItemSource like this?
No, this kind of bindings are not supported in XAML:
Binding Path=Mapping[Obj.SelectedItem.Id].
You must replace Obj.SelectedItem.Id with a constant key value like 1 or bind to some other property that returns the collection of items.
Everything starts working.. Except Content binding
You can only bind to public properties so Value must be a property and not a field:
class Data
{
public long Id { get; set; }
public object Value { get; set; }
}
You can achive this easy with:
public ObservableCollection<CheckedData> SelectedData
{
get
{
return Mapping[Obj.SelectedItem.Id];
}
}
And into
public Data SelectedItem
{
get { return selectedItem; }
set
{
if (value == selectedItem)
return;
selectedItem = value;
RaisePropertyChanged(nameof(SelectedData)); // add this.
}
}
Now, in XAML, you can easy:
<ListBox x:Name="listBox" Height="100" ItemsSource="{Binding Path=Obj.SelectedData}">
I have a List of Lists and display it with nested ListBoxes:
MainWindow.xaml.cs
using System.Collections.Generic;
namespace WPF_Sandbox
{
public partial class MainWindow
{
public IEnumerable<IEnumerable<string>> ListOfStringLists { get; set; } = new[] { new[] { "a", "b" }, new[] { "c", "d" } };
public MainWindow()
{
InitializeComponent();
DoSomethingButton.Click += (sender, e) =>
{
// do something with all selected items
};
}
}
}
MainWindow.xaml
<Window x:Class="WPF_Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
x:Name="ThisControl">
<StackPanel>
<ListBox ItemsSource="{Binding ListOfStringLists, ElementName=ThisControl}">
<ListBox.ItemTemplate>
<ItemContainerTemplate>
<ListBox ItemsSource="{Binding}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<ItemContainerTemplate>
<TextBlock Text="{Binding}" />
</ItemContainerTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ItemContainerTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="DoSomethingButton" Content="DoSomething" />
</StackPanel>
</Window>
How can I get all selected items across all ListBoxes?
I found a few solutions getting one selected item but could not figure out how to do applie those in my scenario.
I have an idea on how to do this by wrapping the string arrays but I would prefer not doing this.
I would just add an event handler to the inner ListBox like so if not doing things the MVVM way:
<ListBox ItemsSource="{Binding}" SelectionMode="Multiple" SelectionChanged="ListBox_SelectionChanged">
Then in your code behind implement the ListBox_SelectionChanged like so:
public List<string> FlatStringList = new List<string>();
private void ListBox_SelectionChanged(object sender,System.Windows.Controls.SelectionChangedEventArgs e)
{
FlatStringList.AddRange(e.AddedItems.Cast<string>());
foreach(string s in e.RemovedItems)
{
FlatStringList.Remove(s);
}
}
This is assuming you don't mind storing the selected strings in a flat list. Then you could implement your DoSomething button click event handler to do something with the FlatStringList.
Hope that helps.
The easiest way would be to iterate through the items in the ListBoxes:
private void DoSomethingButton_Click(object sender, RoutedEventArgs e)
{
List<string> selectedStrings = new List<string>();
foreach (IEnumerable<string> array in outerListBox.Items.OfType<IEnumerable<string>>())
{
ListBoxItem lbi = outerListBox.ItemContainerGenerator.ContainerFromItem(array) as ListBoxItem;
if (lbi != null)
{
ListBox innerListBox = GetChildOfType<ListBox>(lbi);
if (innerListBox != null)
{
foreach (string selectedString in innerListBox.SelectedItems.OfType<string>())
selectedStrings.Add(selectedString);
}
}
}
}
private static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null)
return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null)
return result;
}
return null;
}
Note that the ListBoxItem may be virtualized away if you have a lot of inner IEnumerable<string>. You will then have to force the generation of the containers or disable UI virtualization:
WPF ListView virtualization. How to disable ListView virtualization?
This may affect the performance negatively so if this is an issue you should probably consider binding to an IEnumerable<YourType> and bind the SelectedItems property of the inner ListBox to a property of a YourType using a behaviour.
Since the SelectedItems property of a ListBox is read-only you can't bind to it directly: https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/.
Why don't you create a wrapper (as you said):
public class MyString : INotifyPropertyChanged
{
public MyString(string value) { Value = value; }
string _value;
public string Value { get { return _value; } set { _value = value; RaisePropertyChanged("Value"); } }
bool _isSelected;
public bool IsSelected { get { return _isSelected; } set { _isSelected = value; RaisePropertyChanged("IsSelected"); } }
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string propname)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
}
}
Bind the IsSelected property of the ListBoxItems:
<StackPanel>
<ListBox ItemsSource="{Binding ListOfStringLists, ElementName=ThisControl}">
<ListBox.ItemTemplate>
<ItemContainerTemplate>
<ListBox ItemsSource="{Binding}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<ItemContainerTemplate>
<TextBlock Text="{Binding Value}" />
</ItemContainerTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</ItemContainerTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="DoSomethingButton" Content="DoSomething" />
</StackPanel>
and you are already done:
public IEnumerable<IEnumerable<MyString>> ListOfStringLists { get; set; } = new[] { new[] { new MyString("a"), new MyString("b") { IsSelected = true } }, new[] { new MyString("c"), new MyString("d") } };
public MainWindow()
{
this.InitializeComponent();
DoSomethingButton.Click += (sender, e) =>
{
foreach (var i in ListOfStringLists)
foreach (var j in i)
{
if (j.IsSelected)
{
// ....
}
}
};
}
I have a Listbox
<ListBox Name="lstbox">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Name="text" Background="White" Foreground="Black" Width="400"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Here is c# code
List<string> lst = new List<string>();
// Constructor
public MainPage()
{
for (int i = 0; i < 100; i++)
{
lst.Add("a"+i.ToString());
}
lstbox.ItemsSource = lst;
}
I want that user input values in textboxes inside the list box. And the values are display in the textboxes permanently. But When I'm entering a value in a textbox, it is showing the value in other textboxes also. Also when I'm scrolling the list Value entered in text box is lost. Please Help
I can replicate the same weird behavior when i try it too. I suggest you try turning your list into some sort of model that implements the INotifyPropertyChanged interface. It seems like you want changes made from the UI (textboxes) reflected ion the collection as well, hence this is a better/cleaner approach IMHO.
Xaml
<ListBox Name="lstbox">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Name, Mode=TwoWay}" Background="White" Foreground="Black" Width="400"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code behind
public partial class MainPage : PhoneApplicationPage
{
private readonly ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
// Constructor
public MainPage()
{
InitializeComponent();
for (int i = 0; i < 100; i++)
{
customers.Add(new Customer { Name =" Customer " + i });
}
lstbox.ItemsSource = customers;
}
}
public class Customer : INotifyPropertyChanged
{
private string name;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public string Name
{
get { return this.name; }
set
{
if (value != this.name)
{
this.name = value;
OnPropertyChanged();
}
}
}
}
Today I am stucked in very basic concept again. What is the mistake I am doing.
I have XAML like
<ComboBox ItemsSource="{Binding MyItems}" Height="40" Width="200" SelectedIndex="0"
SelectedItem="{Binding MySelectedItem}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Text="{Binding MySelectedItem.Name}"/>
<Button Content="Test" Grid.Row="1" Click="Button_Click_1"/>
My ModelView looks like
public class MainViewModel : DependencyObject,INotifyPropertyChanged
{
private MyModel mySelectedItem;
public MyModel MySelectedItem
{
get
{
return mySelectedItem;
}
set
{
if (value != mySelectedItem)
{
mySelectedItem = value;
RaisePropertyChange("MySelectedItem");
}
}
}
public IList<MyModel> MyItems
{
get
{
return new List<MyModel>() {new MyModel(){Name="A"},
new MyModel(){Name="B"},
new MyModel(){Name="C"}};
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChange(string name)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
One MyItems property and one SelectedItem property
and Click handler like
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Random r = new Random();
int icnt = r.Next(0,3);
model.MySelectedItem = model.MyItems[icnt];
}
I found that TextBlock.Text is updating but Combobox selected item is not updating. I try to dig out the reason and found that if I execute code below
MyModel prevItem = model.MyItems.Where((m) => m.Name.Equals("A")).FirstOrDefault();
MyModel newItem = model.MyItems.Where((m) => m.Name.Equals("A")).FirstOrDefault();
bool result = prevItem.Equals(newItem);
The value is always false. But why, why I am getting the new reference to same object from collection.
How can resolve this issue.
Thanks
you are getting a new reference because each time the binding mechanism will ask for MyItems you will create a new list.
try creating it once and use observable collection
You need to modify your MyItems code. You are getting new list every time. Try this out.
private List<MyModel> _myItems;
public IList<MyModel> MyItems
{
get
{
if (_myItems == null)
{
myItems = new List<MyModel>();
myItems.Add(new MyModel() { Name = "A" });
myItems.Add(new MyModel() { Name = "B" });
myItems.Add(new MyModel() { Name = "C" });
}
return _myItems}
}
}