I have a test project which bases on :
http://thejoyofcode.com/ViewModels_and_CheckListBoxes.aspx
I want display field of object, not entire object. How to convert this
<ContentPresenter Content="{Binding Value}" Margin="1"/>
to this
<TextBox Text="{Binding Path=Name}"></TextBox>
and still have a object :)
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox Name="list" ItemsSource="{Binding Items}" Margin="4" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Name="check" IsChecked="{Binding IsChecked, Mode=TwoWay}" Margin="3" VerticalAlignment="Center" />
<ContentPresenter Content="{Binding Value}" Margin="1"/>
<!--<TextBox Text="{Binding Path=Name}"></TextBox>--> I want this
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Margin="4" HorizontalAlignment="Left" VerticalAlignment="Bottom" Click="Button_Click">Add new item to list</Button>
<StackPanel Grid.Column="1" Grid.RowSpan="2">
<TextBlock FontWeight="Bold" >Selected Items</TextBlock>
<ItemsControl Margin="4" x:Name="items" ItemsSource="{Binding Path=Items.CheckedItems}" DisplayMemberPath="Value" />
</StackPanel>
</Grid>
</Window>
and Model
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
Items = new CheckableObservableCollection<Car> { new Car("Chevy Camaro"), new Car("Dodge Challenger") };
}
private CheckableObservableCollection<Car> _items;
public CheckableObservableCollection<Car> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged("Items");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler pceh = PropertyChanged;
if (pceh != null)
{
pceh(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Car
{
public string Name { get; set; }
public Car(string name)
{
this.Name = name;
}
}
public class CheckWrapper<T> : INotifyPropertyChanged
{
private readonly CheckableObservableCollection<T> _parent;
public CheckWrapper(CheckableObservableCollection<T> parent)
{
_parent = parent;
}
private T _value;
public T Value
{
get { return _value; }
set
{
_value = value;
OnPropertyChanged("Value");
}
}
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
CheckChanged();
OnPropertyChanged("IsChecked");
}
}
private void CheckChanged()
{
_parent.Refresh();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler pceh = PropertyChanged;
if (pceh != null)
{
pceh(this, new PropertyChangedEventArgs(propertyName));
}
}
and:
public class CheckableObservableCollection<T> : ObservableCollection<CheckWrapper<T>>
{
private ListCollectionView _selected;
public CheckableObservableCollection()
{
_selected = new ListCollectionView(this);
_selected.Filter = delegate(object checkObject) {
return ((CheckWrapper<T>)checkObject).IsChecked;
};
}
public void Add(T item)
{
this.Add(new CheckWrapper<T>(this) { Value = item });
}
public void Remove(T item)
{
this.Remove(item);
}
public ICollectionView CheckedItems
{
get { return _selected; }
}
internal void Refresh()
{
_selected.Refresh();
}
}