ListBox
はオブジェクトのリストにバインドされており、ListBox
の各アイテムにはコンバーターでバインドされたTextBlock
がありますが、オブジェクトのプロパティが変更されたときに、それらを更新することができません。TextBlock
コンバータにバインドされていない場合次のようにPropertyChangedイベントを取得するには、コンバーターを使用するバインディングを使用しますか?
ウィンドウのXmaIである:
<Window x:Class="BindingPropertyChanged.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:BindingPropertyChanged"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="600">
<Window.Resources>
<local:LocationToTextConverter x:Key="LocationToTextConverter" />
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<ListBox
Grid.Row="2"
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="NameText"
Text="{Binding Converter={StaticResource LocationToTextConverter}, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock x:Name="DescriptionText"
Grid.Row="1" Text="{Binding Description, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Label HorizontalAlignment="Right" Content="Place" />
<ComboBox Grid.Column="1"
ItemsSource="{Binding Places}"
SelectedValue="{Binding SelectedItem.PlaceId}"
SelectedValuePath="Id"
DisplayMemberPath="Name"
IsEditable="False"/>
<Label Grid.Row="1" HorizontalAlignment="Right" Content="Description" />
<TextBox Grid.Row="1" Grid.Column="1"
Text="{Binding SelectedItem.Description,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnNotifyDataErrors=True}"
VerticalAlignment="Center"/>
</Grid>
</Grid>
</Window>
左側に上述ListBox
であり、右側にListBox
で選択した項目をオーサリングするためのTextBox
とComboBox
あります。 ListBox.ItemTemplate
では、右にTextBox
のテキストを変更するとDescriptionText
が更新される可能性がありますが、NameText
はバインディングでコンバーターを使用しているため、更新できません。
public class Repository
{
public static ICollection<Place> _places;
public static ICollection<Place> Places
{
get { return _places; }
}
public static ICollection<Location> _locations;
public static ICollection<Location> Locations
{
get { return _locations; }
}
static Repository()
{
_places = new List<Place>();
_places.Add(new Place() { Id = 1, Name = "Downtown Center" });
_places.Add(new Place() { Id = 2, Name = "Headquarter" });
_locations = new List<Location>();
_locations.Add(new Location() { PlaceId = 1, Description = "Room 101" });
_locations.Add(new Location() { PlaceId = 2, Description = "B06" });
}
}
、ウィンドウのDataContext
に設定ViewModel
クラス:
public class DomainBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberNameAttribute] string propertyName = "None")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Location : DomainBase
{
private int _placeId;
private string _description;
public int PlaceId
{
get { return _placeId; }
set
{
if (_placeId == value) return;
_placeId = value;
OnPropertyChanged();
}
}
public string Description
{
get { return _description; }
set
{
if (_description == value) return;
_description = value;
OnPropertyChanged();
}
}
}
public class Place : DomainBase
{
public int Id { get; set; }
public string Name { get; set; }
}
は、ダミーデータのRepository
クラスがあります:
次のコードは、ドメインモデルのクラスです。
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<Location> _items = new ObservableCollection<Location>(Repository.Locations);
private Location _selectedItem;
public ObservableCollection<Location> Items
{
get
{
return _items;
}
private set
{
if (_items == value) return;
_items = value;
OnPropertyChanged();
OnPropertyChanged("SelectedItem");
}
}
public Location SelectedItem
{
get
{
return _selectedItem;
}
set
{
if (_selectedItem == value) return;
_selectedItem = value;
OnPropertyChanged();
}
}
public ObservableCollection<Place> Places
{
get
{
return new ObservableCollection<Place>(Repository.Places);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "None")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
最後に、th E変換器:
class LocationToTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string result = "";
Location location = value as Location;
if (location != null)
{
Place place = Repository.Places.Single(o => o.Id == location.PlaceId);
if (place != null)
{
string placeName = place.Name;
if (!String.IsNullOrWhiteSpace(placeName))
{
result = placeName + ", ";
}
}
result += location.Description;
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
私がしたいことはListBox
でのDescription
「Location
とのName
」Place
を表示することであると私はDescription
さんLocation
選択を変更する場合、ListBox
での表示名を変更する必要があります即座に同様に。誰もそれを達成する方法を知っていますか?
「Location」オブジェクト自体が変更されていないため、「Location」は「PropertyChanged」を呼び出さないという問題があると思います。 –