C# 为什么绑定TextBlock的值在绑定到的属性更改并调用事件PropertyChanged后不能更改?
我正在尝试在实现IPropertyChanged接口的ViewModel类中的TextBlock和属性之间创建绑定。实际上,我的属性会更改,事件会调用,但TextBlock的值不会更改 这是我的XAML代码C# 为什么绑定TextBlock的值在绑定到的属性更改并调用事件PropertyChanged后不能更改?,c#,wpf,xaml,binding,desktop,C#,Wpf,Xaml,Binding,Desktop,我正在尝试在实现IPropertyChanged接口的ViewModel类中的TextBlock和属性之间创建绑定。实际上,我的属性会更改,事件会调用,但TextBlock的值不会更改 这是我的XAML代码 <Grid> <Grid.ColumnDefinitions> <Co
<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; }
}
public class MatchEntryModel
{
public int Id { get; set; }
public TeamModel CompetingTeam { get; set; }
public double Score { get; set; }
public MatchModel ParentMatch { get; set; }
}
选择匹配更改
<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>
private void MatchesListBox\u选择已更改(对象发送方,
选择ChangedEventArgs(e)
{
_viewModel.SelectedMatch=(MatchesListBox.SelectedItem作为MatchModel);
}
您对INotifyPropertyChanged
的实现是错误的,您已将匹配模型
-对象设置为发送方
,但正确的对象是TournamentViewServiceWModel
,它应报告其属性已更改。通过调用将第一个参数更改为this
:
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedMatch)));
您如何更改选定的匹配项或其某些属性?我看不出有什么变化。如果更改
SelectedMatch.Entries[0].CompetingTeam.TeamName
,则TeamName
是否实现INotifyPropertyChanged
?我更改SelectedMatch
。我添加了一个模型代码,它表示一个SelectedMatch
,以及一个我在其中更改它的代码。团队名称未实现INotifyPropertyChanged我认为如果要更新整个模型,则不需要它。谢谢,这对我很有帮助。不客气。:)下次试着发一封邮件。