WPF ComboBox selection causes selection in second ComboBox - c#

I'm binding two comboboxes to the same listviewcollection. The problem is that selecting a value in one combobox, causes the other combobox selected item to change to the exact value of the first combobox. They are coupled and I want them to be independent of each other.
MyListViewCollection is like this
modelsView = new ListCollectionView(MainVM.All_Models);
All_Models is an Observable collection of custom objects like this
public ObservableCollection<MLModel> All_Models { get; set; } = new ObservableCollection<MLModel>() { };
I bind two ComboBoxes to modelsview like this
<ComboBox ItemsSource="{Binding Path=modelsView}" SelectedItem="{Binding SelectedModel_Right}" SelectionChanged="RightSideModelSelection">
<ComboBox ItemsSource="{Binding Path=modelsView}" SelectedItem="{Binding SelectedModel_Left}" SelectionChanged="LeftSideModelSelection">
So everything works fine, the comboboxes contain the identical lists of items from the models view which is what I want.
I definitely have bound the selected item to two separate properties in the view model, and those are
private MLModel _selectedModel_left;
public MLModel SelectedModel_Left
{
get { return _selectedModel_left; }
set
{
SetProperty(ref _selectedModel_left, value);
}
}
private MLModel _selectedModel_right;
public MLModel SelectedModel_Right
{
get { return _selectedModel_right; }
set
{
SetProperty(ref _selectedModel_right, value);
}
}
The only thing I can think of is that they are both referencing the same object in the collection, but I'm not exactly sure how it causes this behavior.
Also the desired behavior is that each combobox be able to select and display a different item from the same collection.
Any explanation would be helpful as well as the recommended way to decouple the two comboboxes selection from each other.
EDIT: As requested here is the code to create a minimal working example
MainWindow.xaml
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Vertical">
<ComboBox ItemsSource="{Binding Path=modelsView}" SelectedItem="{Binding SelectedModel_Left}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox ItemsSource="{Binding Path=modelsView}" SelectedItem="{Binding SelectedModel_Right}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
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;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
}
MLModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication3
{
public class MLModel
{
public string Name { get; set; }
public string Type { get; set; }
}
}
ViewModel.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace WpfApplication3
{
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ViewModel()
{
modelsView = new ListCollectionView(All_Models);
//modelsView.Filter = modelsFilter;
}
public ListCollectionView modelsView { get; set; }
public ObservableCollection<MLModel> All_Models { get; set; } = new ObservableCollection<MLModel>() {
new MLModel() { Name = "One", Type = "TypeOne" },
new MLModel() {Name = "Two", Type = "TypeTwo" },
new MLModel() {Name = "Three", Type = "TypeThree" }
};
private MLModel _selectedModel_left;
public MLModel SelectedModel_Left
{
get { return _selectedModel_left; }
set
{
this._selectedModel_left = value;
NotifyPropertyChanged();
}
}
private MLModel _selectedModel_right;
public MLModel SelectedModel_Right
{
get { return _selectedModel_right; }
set
{
this._selectedModel_right = value;
NotifyPropertyChanged();
}
}
}
}

From the documentation:
If the target is an ItemsControl, the current item is synchronized with the selected item.
You are sharing the same ListCollectionView between both ComboBox objects. This means that the current selected item is updated in the view, and replicated in any other ItemsControl where that same view is used.
If you don't want this behavior, you need to give each ComboBox its own ListCollectionView object to bind to.
Alternatively, you can set the IsSynchronizedWithCurrentItem property of each ComboBox to false.

Related

WPF C# get Object properties without adding extra variables into the viewmodel

I'm curently trying to implemnt a Combobox binding to a list and some labels showing the details of a selected object. The binding to the Combobox works without any problems but I don't want to add extra variables into my ViewModel just to display the name and speed of my car objects.
Is there any way to Map the object variables of the selected combobox item on to the labels without adding additional variables into my ViewModel?
That's the code I'm currently working with
MainWondow.xaml.cs
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;
namespace StackOF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ViewModel viewModel;
public MainWindow()
{
viewModel = new ViewModel();
DataContext = viewModel;
InitializeComponent();
}
}
}
ViewModel.cs
using _02_WPFCarInfo.AutoKlassen;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StackOF
{
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<AutoModel> _autoModels;
private AutoModel selectedModel;
public ObservableCollection<AutoModel> AutoModels
{
get { return _autoModels; }
set
{
_autoModels = value;
OnPropertyChanged();
}
}
public AutoModel SelectedModel
{
get { return selectedModel; }
set
{
selectedModel = value;
OnPropertyChanged();
}
}
public ViewModel()
{
_autoModels = generateCars();
}
private ObservableCollection<AutoModel> generateCars()
{
AutoModel model1 = new AutoModel();
AutoModel model2 = new AutoModel();
AutoModel model3 = new AutoModel();
model1.name = "Mercedes C12 Pro";
model1.speed = "125 km\\h";
model2.name = "Audi A12";
model2.speed = "236 km\\h";
model3.name = "Audi S20 Pro";
model3.speed = "300 km\\h";
ObservableCollection<AutoModel> cars = new ObservableCollection<AutoModel>();
cars.Add(model1);
cars.Add(model2);
cars.Add(model3);
return cars;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
MainWindow.xaml
<Window x:Class="StackOF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StackOF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ComboBox SelectedItem="{Binding SelectedModel}" DisplayMemberPath="name" ItemsSource="{Binding AutoModels}" HorizontalAlignment="Center" Margin="0,164,0,0" VerticalAlignment="Top" Width="212"/>
<Label Content="Name:" HorizontalAlignment="Left" Margin="294,231,0,0" VerticalAlignment="Top"/>
<Label Content="Speed:" HorizontalAlignment="Left" Margin="294,262,0,0" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Center" Margin="0,241,0,0" TextWrapping="Wrap" Text="---" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>
<TextBlock HorizontalAlignment="Center" Margin="0,267,0,0" TextWrapping="Wrap" Text="---" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>
</Grid>
</Window>
AutoModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _02_WPFCarInfo.AutoKlassen
{
public class AutoModel
{
public String name { get; set; }
public String speed { get; set; }
public AutoModel()
{
this.speed = String.Empty;
this.name = String.Empty;
}
public AutoModel(String name, String speed)
{
this.speed = speed;
this.name = name;
}
}
}
I thought about something like :
<TextBlock HorizontalAlignment="Center" Margin="0,241,0,0" TextWrapping="Wrap" Text="{Binding SelectedModel.name}" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>
<TextBlock HorizontalAlignment="Center" Margin="0,267,0,0" TextWrapping="Wrap" Text="{Binding SelectedModel.speed}" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>
But obviously it is not possible to select the variables of "SelectedModel" that way.
Ok nvm it DOES work. It only does not work if you have a textblock with opening and closing tag with text inbetween
This works:
<TextBlock HorizontalAlignment="Center" Margin="0,241,0,0" TextWrapping="Wrap" Text="{Binding SelectedModel.name}" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303"/>
but this doesn't
<TextBlock HorizontalAlignment="Center" Margin="0,241,0,0" TextWrapping="Wrap" Text="{Binding SelectedModel.name}" VerticalAlignment="Top" RenderTransformOrigin="0.287,-1.303">---</TextBlock>

I can't get custom control's selected item

Pager.xaml(View)
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Client.View.Pager">
...
<ListBox x:Name="listBoxEntries"
ItemsSource="{Binding Path=ListCollectionView}"
BorderThickness="0"
Margin="0"
Style="{StaticResource common}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
ItemTemplate="{StaticResource templateTableCategory}"
SelectedItem="{Binding Path=SelectedEntry, Mode=TwoWay}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="{Binding Path=Rows}"
Columns="{Binding Path=Columns}"
IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
...
</Grid>
Pager.xaml.cs(Behind code)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Client.Model;
using Client.ViewModel;
namespace Client.View
{
public partial class Pager
{
public static readonly DependencyProperty SelectedEntryProperty = DependencyProperty.Register("SelectedEntry", typeof(IPagableEntry), typeof(Pager));
...
public IPagableEntry SelectedEntry
{
get { return (DataContext as PagerViewModel).SelectedEntry; }
set { (DataContext as PagerViewModel).SelectedEntry = value; }
}
...
}
}
PagerViewModel.cs
namespace Client.ViewModel
{
public class PagerViewModel : ViewModelBase
{
...
IPagableEntry _selectedEntry;
public IPagableEntry SelectedEntry
{
get
{
return _selectedEntry;
}
set
{
_selectedEntry = value;
OnPropertyChanged("SelectedEntry");
}
}
...
}
}
MainPage.xaml(View)
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Client.View.MainPage"
Style="{StaticResource common}">
<Page.DataContext>
<viewModel:MainPageViewModel/>
</Page.DataContext>
...
<view:Pager x:Name="pagerTableCategories"
Grid.Row="0"
List="{Binding Path=PagerTableCategoriesItems}"
Rows="{Binding Path=PagerTableCategoriesRows}"
Columns="{Binding Path=PagerTableCategoriesColumns}"
SelectedEntry="{Binding Path=SelectedTableCategory, Mode=TwoWay}">
</view:Pager>
...
</Page>
MainPageViewModel.cs
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Client.Model;
namespace Client.ViewModel
{
public class MainPageViewModel : ViewModelBase
{
...
IPagableEntry _selectedTableCategory;
public IPagableEntry SelectedTableCategory
{
get
{
return _selectedTableCategory;
}
set
{
_selectedTableCategory = value;
MessageBox.Show("Got it!");
}
}
...
}
}
I made a custom panel 'Pager' and that's ViewModel.
and I wanna show the pager in my MainPage.
I expect that I select an item, then MainPageViewModel's property SelectTableCategory will change, and an message box show up with string "Got it!"
But it doesn't works...
what's my problem?
ps. I am not very good at English.
I would appreciate your understanding.
Let's work from first principles.
You have a custom control called Pager, which is to be used like this
<view:Pager x:Name="pagerTableCategories"
SelectedEntry="{Binding Path=SelectedTableCategory, Mode=TwoWay}">
</view:Pager>
See how SelectedEntry is assigned a binding? This tells us that Pager must implement SelectedEntry as a dependency property. How do we do this?
public class Pager
{
// Using a DependencyProperty as the backing store for PrePend.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedEntryProperty =
DependencyProperty.Register("SelectedEntry", typeof(object),
typeof(Pager),
new PropertyMetadata(null, OnSelectedEntryChanged));
public object SelectedEntry
{
get { return GetValue(SelectedEntryProperty); }
set { SetValue(SelectedEntryProperty, value); }
}
private static void OnSelectedEntryChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
{
// TODO set the SelectedItem in the ListBox
}
}
Next you need to display the Pager. Your XAML looks ok. You should add an event handler on the ListBox to listen to selection changed event. This way, you can update SelectedEntry when that occurs.
public class Pager
{
// continued...
// *Updated*
private listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.SelectedEntry = (sender as ListBox).SelectedItem;
}
}

adding new row dynamic to datagrid

I am new with WPF and I am trying to add a new to the data grid I created.
The rows I am adding should be added dynamically however I can't see the values of the data in in the data grid.
Here is the xaml:
<Window x:Class="ProtocolAnalyzer.createByProtocol"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="createByProtocol" Height="506" Width="384">
<Grid Margin="0,0,2,4">
<DataGrid x:Name="dataGridTable" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="452" Width="245">
<DataGrid.Columns>
<DataGridTextColumn Header="Field"/>
<DataGridTextColumn Header="Value"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Here is my code:
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.Shapes;
namespace ProtocolAnalyzer
{
/// <summary>
/// Interaction logic for createByProtocol.xaml
/// </summary>
public partial class createByProtocol : Window
{
private ProtocolData.ProtocolData.Protocols _runningProtocol;
public class Data
{
public string Name { get; set; }
public string Value { get; set; }
}
public createByProtocol(ProtocolData.ProtocolData.Protocols protocol)
{
InitializeComponent();
_runningProtocol = protocol;
buildTable();
}
private void buildTable()
{
switch (_runningProtocol)
{
case ProtocolData.ProtocolData.Protocols.ZBM:
dataGridTable.Items.Add("");
dataGridTable.Items[0] = "FFF";
break;
}
}
}
}
EDIT: some general information for "dynamic controls" in wpf/mvvm
if you go the MVVM style you do something like this.
viewmodel
//your data
public ObservableCollection<Customer> MySource {get;set;}
//Command to add a new row
public ICommand AddNewCustomerCommand {get{return _lazyAddCommand.Value;}}
private readonly Lazy<DelegateCommand> _lazyAddCommand;
//ctor
public MyViewmodel()
{
MySource = new ObservableCollection<Customer>();
_lazyAddCommand= new Lazy<DelegateCommand>(() => new DelegateCommand(AddNewCustomerCommandExecute, CanAddNewCustomerCommandExecute));
}
private bool CanAddNewCustomerCommandExecute()
{
return true;//your Conditions goes here
}
private void AddNewCustomerCommandExecute()
{
if (!CanAddNewCustomerCommandExecute())
return;
//Add new Customer
MySource.Add(new Customer());
}
view: use Binding to set the ItemsSource for your Datagrid
<DataGrid ItemsSource="{Binding MySource}">
<DataGrid.Columns>
<DataGridTextColumn Header="Field"/>
<DataGridTextColumn Header="Value"/>
</DataGrid.Columns>
</DataGrid>
thats all. the new row will display as far as your command is invoked through a button or something else
if you have data in "DataTable" that you are trying to assign to datagrid then you can use datagrid.Datasource porperty
If you have a list or an array..
then just use foreach loop and under that loop add you rows.

WPF C# ComboBox with multiple lines and accepting returns?

Is there an easy way to do a multiline combobox in WPF C#? What do I mean by this? I mean a textbox that supports multiple lines and carriage returns; where it word wraps... It needs to have a scrollbar so that if the text in the box is taller than the height of the combobox, the user can scroll down.
In addition, because its a combobox, it needs to have a dropdown button so that the user can quickly swap between groups of text. I've tried googling for this, but I can't find anyone talking about such a combobox.
EDITED FOR COMPLETE WORKING SOLUTION:
XAML:
<Window x:Class="delete.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox Height="30" Width="300" ItemsSource="{Binding items}" SelectedItem="{Binding item}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBox AcceptsReturn="True" TextWrapping="Wrap" Width="250" Height="30" Text="{Binding Name}" VerticalScrollBarVisibility="Auto"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
Code-behind:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
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;
namespace delete
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
Setup();
this.DataContext = this;
}
ObservableCollection<Thang> _items;
public ObservableCollection<Thang> items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged("items");
}
}
private Thang _item;
public Thang item
{
get { return _item; }
set
{
_item = value;
OnPropertyChanged("item");
}
}
public void Setup()
{
items = new ObservableCollection<Thang>();
items.Add(new Thang("1", "One"));
items.Add(new Thang("2", "Two"));
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
public class Thang
{
public Thang(string id, string name)
{
Name = name;
ID = id;
}
public string Name { get; set; }
public string ID { get; set; }
}
}

List<string> is not updated back to ListBox's ItemsSource?

I'm new to WPF. I have a List<string> as a source for my ListBox's ItemsSource. Initially, the ListBox shows all the Items in my List<string> OK. However, after trying adding some string to my List<string>, the ListBox doesn't update the changes. I'm using Binding to bind the data (behind) with the ListBox (view), here is my code:
//Code behind
public MainWindow: Window {
public MainWindow(){
InitializeComponent();
Items = new List<string>(){"1","2","3"};//after loaded, all these values are displayed OK in my ListBox.
DataContext = this;
//Try clicking on a button to add new value
button1.Click += (s,e) => {
Items.Add("4");//But my ListBox stays the same without any update/changes.
};
}
public List<string> Items {get;set;}
}
//XAML
<ListBox ItemsSource={Binding Items}/>
Could you please point out what I'm doing wrong here and give me a solution? Thank you very much in advance.
If you had read the documentation of ItemsSource you would already know what is wrong.
[...]
This example shows how to create and bind to a collection that derives from the ObservableCollection<T> class, which is a collection class that provides notifications when items get added or removed.
you should try ObservableCollection instead because it's
Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Click="Button_Click" Content="Button" HorizontalAlignment="Left" Margin="441,289,0,0" VerticalAlignment="Top" Width="75"/>
<ListBox HorizontalAlignment="Left" ItemsSource="{Binding MyList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="lstbox" Height="296" Margin="21,23,0,0" VerticalAlignment="Top" Width="209"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<string> _myList = new ObservableCollection<string>(new List<string>(){"1","2","3"});
int i = 3;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MyList.Add(i++.ToString());
}
public ObservableCollection<string> MyList
{
get { return _myList; }
set { _myList = value; }
}
}
}

Categories

Resources