C# WPF绑定到ObservableCollection元素的属性
我正在WPF中使用DataGrid,我正在尝试执行一些数据绑定,它比我以前使用的稍微复杂一些。我有一个类的ObservableCollection,它也实现了一个子类的ObservableCollection。我试图将复选框的IsChecked属性绑定到该子类上的值,无论何时尝试,我似乎都无法让它工作。希望我只是错过了一些简单的东西 在我的主程序中,我有以下内容,它可以很好地检测“MyDevice”类中“SomeProperty”的更改:C# WPF绑定到ObservableCollection元素的属性,c#,wpf,xaml,data-binding,wpfdatagrid,C#,Wpf,Xaml,Data Binding,Wpfdatagrid,我正在WPF中使用DataGrid,我正在尝试执行一些数据绑定,它比我以前使用的稍微复杂一些。我有一个类的ObservableCollection,它也实现了一个子类的ObservableCollection。我试图将复选框的IsChecked属性绑定到该子类上的值,无论何时尝试,我似乎都无法让它工作。希望我只是错过了一些简单的东西 在我的主程序中,我有以下内容,它可以很好地检测“MyDevice”类中“SomeProperty”的更改: ObservableCollection MyDevic
ObservableCollection MyDevices=新的ObservableCollection();
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() };
}
公共类MyDevice:INotifyPropertyChanged
{
公共类输入:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void RaisePropertyChanged([CallerMemberName]字符串PropertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(PropertyName));
}
检测到私有布尔\u同步;
检测到公共布尔同步
{
获取{return\u SyncDetected;}
设置{u SyncDetected=value;RaisePropertyChanged();}
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void RaisePropertyChanged([CallerMemberName]字符串PropertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(PropertyName));
}
私人房产;
公共财产
{
获取{return\u SomeProperty;}
设置{u SomeProperty=value;RaisePropertyChanged();}
}
公共ObservableCollection MyInputs=新ObservableCollection(){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() };
public observetecollection MyInputs=new observetecollection(){new Input(),new Input()};
MyDevice.MyInputs
是一个,而不是一个属性,因此。如果只有两个复选框,为什么需要observablecollection?或者,如果使用observablecollection,则应该使用ItemsCollection来显示复选框为什么不绑定到MyDevices:到底是什么不起作用?我看不出有什么立即的错误(除了它不是真正正确的做事方式)above@TimRutter它不会显示正确的数据,因为它似乎从未更新过。ItemsSource=“{Binding MyDevices}”
和DevicesGrid.ItemsSource=MyDevices之间有什么区别代码>在C#代码中?我会调查收集的项目,但在此期间,你能提供更多关于我所做的“不是真正正确的方式”的建议吗?谢谢。就这样。
public ObservableCollection<Input> MyInputs = new ObservableCollection<Input>() { new Input(), new Input() };