Хочу в Text КомбоБокса выводить строку с именами всех отмеченных объектов класса Person. Прибиндил, создал конвертер. Конвертер срабатывает только при инициализации, а при изменении/добавлении/удалении элементов не срабатывает. Как это побороть? XAML:
<Window x:Class="WPFTest.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:WPFTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:MyConverter
x:Key="conv" />
</Window.Resources>
<Grid>
<ComboBox Grid.Column="1" MinWidth="150" Height="25"
x:Name="SearchFilterCorpseDeathPlace"
IsTextSearchEnabled="True"
IsTextSearchCaseSensitive="False"
IsEditable="True"
SelectedValuePath="Name"
TextSearch.TextPath="Name"
ItemsSource="{Binding Path=Persons}"
SelectedItem="{Binding Path=Person}"
SelectedValue="{Binding Path=Person.Name, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
Foreground="Black"
Text="{Binding Path=Persons, Converter={StaticResource conv}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox
x:Name="SearchFilterCorpseDeathPlaceCheckBox"
Content="{Binding Name}"
IsChecked="{Binding IsChecked}" >
</CheckBox>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
Viewmodel:
public class Person : INotifyPropertyChanged
{
public string Name { get; set; }
public int Age { get; set; }
private bool _ischecked;
public bool IsChecked { get { return _ischecked; } set { _ischecked = value; OnPropertyChanged(); } }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
private ObservableCollection<Person> _person;
public ObservableCollection<Person> Persons { get { return _person; } set { _person = value; OnPropertyChanged(); } }
public MainViewModel()
{
Persons = new ObservableCollection<Person>() {
new Person(){Name="Ivan", Age=28, IsChecked=false},
new Person(){Name="Egor", Age=28, IsChecked=false},
new Person(){Name="Stas", Age=28, IsChecked=false},
new Person(){Name="Igor", Age=28, IsChecked=false}
};
}
}
Converter:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace WPFTest
{
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string result = null;
var collection = (IEnumerable<Person>)value;
foreach (Person p in collection)
{
if (p.IsChecked)
{
if (result == null)
{
result = p.Name;
}
else
{
result = result + string.Format("," + "p.Name");
}
}
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}