В общем, написал такой я код:
public class DataType : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
int _value;
public string Name { get; set; }
public int Value
{
get => _value;
set
{
_value = value;
Debug.WriteLine($"{Name} => {Value}");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
}
}
}
<Window x:Class="BindingToSubProperties.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:BindingToSubProperties"
mc:Ignorable="d"
Title="BindingToSubProperties" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" MinWidth="180"/>
</Grid.ColumnDefinitions>
<Label Content="Name"/>
<ComboBox x:Name="cmbName" Grid.Column="1" Margin="3" DisplayMemberPath="Name">
<local:DataType Name="Type1" Value="1"/>
<local:DataType Name="Type2" Value="2"/>
<local:DataType Name="Type3" Value="3"/>
</ComboBox>
<Label Content="Current value" Grid.Row="1"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="3" Text="{Binding ElementName=cmbName, Path=SelectedValue.Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsEnabled="False"/>
<Label Content="New value" Grid.Row="2"/>
<TextBox Grid.Row="2" Grid.Column="1" Margin="3" Text="{Binding ElementName=cmbName, Path=SelectedValue.Value, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"/>
</Grid>
</Window>
Вопрос такой: Почему привязка к cmbName работает в режиме TwoWay, но ни в какую не работает в режиме OneWayToSource?