簡體   English   中英

WPF綁定到ObservableCollection元素的屬性

[英]WPF Binding to Property of Element of ObservableCollection

我正在使用WPF中的DataGrid,並且嘗試執行一些數據綁定,該綁定比我以前要復雜一些。 我有一個類的ObservableCollection,它也實現了一個子類的ObservableCollection。 我試圖將CheckBox的IsChecked屬性綁定到該子類上的值,無論何時嘗試,我似乎都無法使其正常工作。 希望我只是缺少一些簡單的東西。

在我的主程序中,我具有以下內容,並且可以很好地檢測“ MyDevice”類中“ SomeProperty”的更改:

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

我的班級定義如下:

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.MyInputs是一個字段 ,而不是屬性,因此綁定系統無法通過反射找到它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM