This is an example of what I mean by "WPF Mentality", which differs a lot from the archaic winforms mentality of mashing all together logic and UI:
<Window x:Class="WpfApplication4.Window11"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window11" Height="300" Width="300">
<DockPanel>
<Button DockPanel.Dock="Top" Content="Copy" Command="{Binding CopyCommand}"/>
<UniformGrid Columns="2">
<ListBox ItemsSource="{Binding Items}" SelectionMode="Extended">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<ListBox ItemsSource="{Binding SelectedItems}"/>
</UniformGrid>
</DockPanel>
</Window>
Code Behind:
using System.Linq;
using BaseWPFFramework.MVVM;
using System.Collections.ObjectModel;
namespace WpfApplication4
{
public partial class Window11
{
public Window11()
{
InitializeComponent();
DataContext = new ListViewModel();
}
}
}
ViewModel:
public class ListViewModel: ViewModelBase
{
private ObservableCollection<Selectable<string>> _items;
public ObservableCollection<Selectable<string>> Items
{
get { return _items ?? (_items = new ObservableCollection<Selectable<string>>()); }
}
private ObservableCollection<string> _selectedItems;
public ObservableCollection<string> SelectedItems
{
get { return _selectedItems ?? (_selectedItems = new ObservableCollection<string>()); }
}
private DelegateCommand _copyCommand;
public DelegateCommand CopyCommand
{
get { return _copyCommand ?? (_copyCommand = new DelegateCommand(Copy)); }
}
private void Copy()
{
SelectedItems.Clear();
Items.Where(x => x.IsSelected).Select(x => x.Value).ToList().ForEach(SelectedItems.Add);
}
public ListViewModel()
{
Enumerable.Range(1, 100).Select(x => new Selectable<string>("Item" + x.ToString())).ToList().ForEach(x => Items.Add(x));
}
}
public class Selectable<T>: ViewModelBase
{
private T _value;
public T Value
{
get { return _value; }
set
{
_value = value;
NotifyPropertyChange(() => Value);
}
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
NotifyPropertyChange(() => IsSelected);
}
}
public Selectable(T value)
{
Value = value;
}
public Selectable(T value, bool isSelected): this(value)
{
IsSelected = isSelected;
}
public override string ToString()
{
return Value != null ? Value.ToString() : string.Empty;
}
}
Just copy and paste my code in a File -> New -> WPF Application
and see the results for yourself.
Notice that I'm using a generic solution (Selectable<T>
), so you could use that with any classes you want.
Also, please don't do things like: this.textBox1.Text = whatever
in WPF. WPF encourages the separation of UI and data, and you must understand that UI Is Not Data in order to properly work with WPF. Please leave the winforms mentality behind if you expect good results from WPF.
Instead, either create a proper ViewModel to hold the data shown in your textboxes, or bind the textboxes directly to an instance of the SelectedItems
as in my example.
As an aside, off-topic comment, the normal
way is not the winforms way anymore. All recent (< 10 Years
) technologies (WPF, Silverlight, WinRT) are all XAML-based and encourage the use of MVVM.
This means that the winforms way is now the old
way, not the normal
way.