WPF 绑定到 ObservableCollection 元素的 属性

WPF Binding to Property of Element of ObservableCollection

我正在使用 WPF 中的 DataGrid,我正在尝试执行一些数据绑定,它比我习惯的要复杂一些。我有一个 class 的 ObservableCollection,它还实现了一个子 class 的 ObservableCollection。我正在尝试将 CheckBox 的 IsChecked 属性 绑定到该 subclass 上的值,无论我何时尝试,我似乎都无法让它工作。希望我只是遗漏了一些简单的东西。

在我的主程序中有以下内容,它可以很好地检测 "MyDevice" class 上对 "SomeProperty" 的更改:

ObservableCollection<MyDevice> MyDevices = new ObservableCollection<MyDevice>();
DevicesGrid.ItemSource = MyDevices;

我的 class 定义如下:

public class MyDevice : INotifyPropertyChanged
{
    public class Input : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void RaisePropertyChanged([CallerMemberName] string PropertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

        private bool _SyncDetected;
        public bool SyncDetected
        {
            get { return _SyncDetected; }
            set { _SyncDetected = value; RaisePropertyChanged(); }
        }
    }

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

    private bool _SomeProperty;
    public bool SomeProperty
    {
        get { return _SomeProperty; }
        set { _SomeProperty = value; RaisePropertyChanged(); }
    }

    public ObservableCollection<Input> MyInputs = new ObservableCollection<Input>() { new Input(), new Input() };
}

这是我的 XAML:

<DataGrid x:Name="DevicesGrid" Margin="10,80,10,10" AutoGenerateColumns="False">
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
            <Setter Property="ContextMenu" Value="{StaticResource DeviceRowContextMenu}"/>
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Sync/Hotplug" IsReadOnly="True" Width="Auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="2,2,2,2" VerticalAlignment="Center" HorizontalAlignment="Center">
                        <CheckBox Margin="2,2,2,2" IsHitTestVisible="False" IsChecked="{Binding MyInputs[0].SyncDetected}" Content="In1"/>
                        <CheckBox Margin="2,2,2,2" IsHitTestVisible="False" IsChecked="{Binding MyInputs[1].SyncDetected}" Content="In2"/>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>        
    </DataGrid.Columns>
</DataGrid>

我对使用 WPF 非常陌生,因此非常感谢您的帮助。谢谢

这里有问题:

public ObservableCollection<Input> MyInputs = new ObservableCollection<Input>() { new Input(), new Input() };

MyDevice.MyInputsfield, not a property, so the binding system cannot find it through reflection.