为什么绑定的 TextBlock 的值在它绑定到的 属性 发生更改并且事件 PropertyChanged 被调用后无法更改?
Why binded TextBlock`s value can be not changed after the property to which it is bounded changed and event PropertyChanged invoked?
我正在尝试在实现 IPropertyChanged 接口的 ViewModel class 上创建 TextBlock 和 属性 之间的绑定。实际上,我的 属性 改变了,事件调用了,但是 TextBlock 的值没有改变。
这是我的XAML代码
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Width="120"
Text="{Binding SelectedMatch.Entries[0].CompetingTeam.TeamName, Mode=TwoWay, Converter={StaticResource TeamNameConverter}}"/>
<TextBox Grid.Column="1"
Text="{Binding SelectedMatch.Entries[0].Score}"
Width="50"
FontFamily="Montserrat"/>
</Grid>
查看模型
public class TournamentViewerViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private MatchModel _selectedMatch;
public MatchModel SelectedMatch
{
get => _selectedMatch;
set
{
_selectedMatch = value;
PropertyChanged?.Invoke(SelectedMatch, new PropertyChangedEventArgs(nameof(SelectedMatch)));
}
}
匹配模型
public class MatchModel
{
public int Id { get; set; }
public List<MatchEntryModel> Entries { get; set; } = new List<MatchEntryModel>();
public TeamModel Winner { get; set; }
public int RoundNumber { get; set; }
}
MatchEntryModel
public class MatchEntryModel
{
public int Id { get; set; }
public TeamModel CompetingTeam { get; set; }
public double Score { get; set; }
public MatchModel ParentMatch { get; set; }
}
SelectedMatch 更改
private void MatchesListBox_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
_viewModel.SelectedMatch = (MatchesListBox.SelectedItem as MatchModel);
}
您对 INotifyPropertyChanged
的实现是错误的,您已将 MatchModel
对象设置为 sender
,但正确的是 TournamentViewerViewModel
,它应该报告,它的 属性 已经改变。通过 Invoke
:
将第一个参数更改为 this
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedMatch)));
我正在尝试在实现 IPropertyChanged 接口的 ViewModel class 上创建 TextBlock 和 属性 之间的绑定。实际上,我的 属性 改变了,事件调用了,但是 TextBlock 的值没有改变。
这是我的XAML代码
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Width="120"
Text="{Binding SelectedMatch.Entries[0].CompetingTeam.TeamName, Mode=TwoWay, Converter={StaticResource TeamNameConverter}}"/>
<TextBox Grid.Column="1"
Text="{Binding SelectedMatch.Entries[0].Score}"
Width="50"
FontFamily="Montserrat"/>
</Grid>
查看模型
public class TournamentViewerViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private MatchModel _selectedMatch;
public MatchModel SelectedMatch
{
get => _selectedMatch;
set
{
_selectedMatch = value;
PropertyChanged?.Invoke(SelectedMatch, new PropertyChangedEventArgs(nameof(SelectedMatch)));
}
}
匹配模型
public class MatchModel
{
public int Id { get; set; }
public List<MatchEntryModel> Entries { get; set; } = new List<MatchEntryModel>();
public TeamModel Winner { get; set; }
public int RoundNumber { get; set; }
}
MatchEntryModel
public class MatchEntryModel
{
public int Id { get; set; }
public TeamModel CompetingTeam { get; set; }
public double Score { get; set; }
public MatchModel ParentMatch { get; set; }
}
SelectedMatch 更改
private void MatchesListBox_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
_viewModel.SelectedMatch = (MatchesListBox.SelectedItem as MatchModel);
}
您对 INotifyPropertyChanged
的实现是错误的,您已将 MatchModel
对象设置为 sender
,但正确的是 TournamentViewerViewModel
,它应该报告,它的 属性 已经改变。通过 Invoke
:
this
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedMatch)));