You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI中如何在ListView内引用ViewModel属性作为转换器参数?

问题原因

ConverterParameter属性不支持数据绑定,当你在XAML中写ConverterParameter={Binding ...}时,系统不会解析这个绑定表达式,而是将Binding对象本身作为参数传递给转换器,这就是为什么你拿到的参数是Microsoft.Maui.Controls.Binding类型,而非预期的Person对象。

解决方案

方案一:使用MultiBinding与IMultiValueConverter

通过MultiBinding可以同时传递多个绑定值给转换器,替代ConverterParameter的作用:

1. 实现IMultiValueConverter

创建新的转换器,实现IMultiValueConverter接口:

public class SelectedItemToBooleanConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        // values[0] 当前列表项的Person对象
        // values[1] ViewModel中的SelectedPerson对象
        if (values.Length == 2 && values[0] is Person currentPerson && values[1] is Person selectedPerson)
        {
            return currentPerson == selectedPerson;
        }
        return false;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

2. 修改XAML中的CheckBox绑定

将普通Binding替换为MultiBinding,同时传入当前项和ViewModel的SelectedPerson:

<CheckBox>
    <CheckBox.IsChecked>
        <MultiBinding Converter="{StaticResource SelectedItemToBooleanConverter}">
            <!-- 当前列表项的Person对象 -->
            <Binding Path="."/>
            <!-- 页面ViewModel中的SelectedPerson -->
            <Binding Source="{x:Reference mainPage}" Path="BindingContext.SelectedPerson"/>
        </MultiBinding>
    </CheckBox.IsChecked>
</CheckBox>

方案二:给Person模型添加IsSelected属性(更简洁)

修改模型和ViewModel,直接通过属性绑定实现选中状态同步:

1. 更新Person模型

添加IsSelected属性:

public class Person : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get => _name;
        set { _name = value; OnPropertyChanged(); }
    }

    private string _email;
    public string Email
    {
        get => _email;
        set { _email = value; OnPropertyChanged(); }
    }

    private bool _isSelected;
    public bool IsSelected
    {
        get => _isSelected;
        set { _isSelected = value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

2. 更新ViewModel的SelectedPerson属性

在SelectedPerson变化时,同步所有Person的IsSelected状态:

private Person _selectedPerson;
public Person SelectedPerson
{
    get => _selectedPerson;
    set
    {
        if (_selectedPerson != value)
        {
            // 取消之前选中项的状态
            if (_selectedPerson != null)
                _selectedPerson.IsSelected = false;
            
            _selectedPerson = value;
            
            // 设置新选中项的状态
            if (_selectedPerson != null)
                _selectedPerson.IsSelected = true;
            
            OnPropertyChanged(nameof(SelectedPerson));
        }
    }
}

3. 简化XAML中的CheckBox绑定

现在CheckBox可以直接绑定到Person的IsSelected属性:

<CheckBox IsChecked="{Binding IsSelected}" />

内容的提问来源于stack exchange,提问作者adamasan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 18:00:00