Xamarin.forms 如何在viewModel中将复选框的值设置为true?

Xamarin.forms 如何在viewModel中将复选框的值设置为true?,xamarin.forms,Xamarin.forms,我希望默认选中一个复选框。我试过这个,但不起作用。我曾尝试在xaml中将IsChecked设置为true,但它没有受到影响 public bool FilterAlphabetically { set { NotifyPropertyChanged(); _filterAlphabetically = value; _filterAlphabetically = tr

我希望默认选中一个复选框。我试过这个,但不起作用。我曾尝试在xaml中将IsChecked设置为true,但它没有受到影响

public bool FilterAlphabetically
        {
            set
            {
                NotifyPropertyChanged();
                _filterAlphabetically = value;
 _filterAlphabetically = true;

                if (FilterAlphabetically)
                {
                    UserDecision.Add(1);
                    FilterNotListened = false;
                }
            }
            get => _filterAlphabetically;
        }

确保将
IsChecked
绑定到您的属性:

<CheckBox ... IsChecked="{Binding FilterAlphabetically}" ... />

您的复选框是否绑定到此属性?您是否正在使用INotifyPropertyChanged?您正在将该值初始化为true吗?bools将默认为false。您可能需要调用
NotifyPropertyChanged()方法,至少设置了新值一次。好吧,但我如何将其设置为true,因为默认值“it not working”告诉我们很少。请查看“”和“”及其所有链接页。@Thetman我已经找到了一个在下面发布并被接受的解决方案,很抱歉我没有回复您的评论。但我确认复选框未被选中是的,我已被选中=“{Binding FilterAlphabetically,Mode=TwoWay}”,如果我在VM中将其设置为true,则不会被选中。我正在使用iNotify在哪里初始化它?我所拥有的只是我发布的内容加上私人文件的过滤;你可以像我在回答中所说的那样初始化它,或者在你声明它时将private filteralphabeticaly设置为true谢谢,我将private设置为true
// bools are false by default
public bool FilterAlphabetically { ... } = true;