Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

c# - Why binded TextBlock`s value can be not changed after the property to which it is bounded changed and event PropertyChanged invoked?

I'm trying to create a bind between a TextBlock and a property at the ViewModel class which implements IPropertyChanged interface. Actually, my property changes, event invokes, but TextBlock`s value does not change.

Here is my XAML code

                            <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>

View model

    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)));
            }
        }
   

Match model

    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 change

private void MatchesListBox_SelectionChanged(object sender,
     SelectionChangedEventArgs e)
{
    _viewModel.SelectedMatch = (MatchesListBox.SelectedItem as MatchModel);
}
question from:https://stackoverflow.com/questions/65845992/why-binded-textblocks-value-can-be-not-changed-after-the-property-to-which-it-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your implementation of INotifyPropertyChanged is wrong, you have set as sender the MatchModel-object, but the right one is of TournamentViewerViewModel, it should report, that its property has changed. Change the first parameter to this by Invoke:

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedMatch)));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...