WFP 将列表 oc 对象绑定到客户控件

本文关键字:客户 控件 绑定 对象 列表 oc WFP | 更新日期: 2023-09-27 18:31:05

我正在尝试将设备对象列表绑定到我正在处理的服装控件。我收到此错误。

不能在类型的"设备"属性上设置"绑定" "卡马拉选择"。只能在依赖项属性上设置"绑定" 的依赖对象。

XML代码

<trainControl:CamaraSelection  Devices="{Binding DeviceList}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

控制代码

    private List<Device> devices = new List<Device>();
    public static readonly DependencyProperty DeviceListProperty =
    DependencyProperty.Register("DeviceList", typeof(List<Device>), typeof(CamaraSelection),
                            new PropertyMetadata(default(ItemCollection), OnDeviceListChanged));

    private static void OnDeviceListChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        var camaraSelection = dependencyObject as CamaraSelection;
        if (camaraSelection != null)
        {
            camaraSelection.OnDeviceListChanged(dependencyPropertyChangedEventArgs);
        }
    }
    private void OnDeviceListChanged(DependencyPropertyChangedEventArgs e)
    {
    }
    public List<Device> Devices
    {
        get { return (List<Device>)GetValue(DeviceListProperty); }
        set { SetValue(DeviceListProperty, value); }
    }

WFP 将列表 oc 对象绑定到客户控件

设置绑定的属性必须是DependencyProperty。在您的情况下,它是Devices属性。DependencyProperty.Register() 方法中的第一个参数必须是属性的名称。代码中的第一个参数是 "DeviceList",但属性的名称是 Devices

public static readonly DependencyProperty DevicesProperty =
DependencyProperty.Register("Devices", typeof(List<Device>), typeof(CamaraSelection),
                        new PropertyMetadata(default(ItemCollection), OnDeviceListChanged));

public List<Device> Devices
{
    get { return (List<Device>)GetValue(DevicesProperty ); }
    set { SetValue(DevicesProperty, value); }
}

类中的"设备"属性必须是依赖项属性,而不是"设备列表"。要绑定到的属性必须是依赖项属性。