如果需要实时监视LIst的变化,需要用ObservableCollection<Class1>
数据绑定概述 - WPF .NET Framework | Microsoft Docs
public class Class1: INotifyPropertyChanged
{
public static ObservableCollection<Class1> list=new ObservableCollection<Class1>();
public event PropertyChangedEventHandler? PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public ObservableCollection<Class1> List
{
get
{
return list;
}
set
{
if (value != list)
{
list = value;
NotifyPropertyChanged();
}
}
}
public int id;
public int ID
{
get
{
return id;
}
set
{
if (value != id)
{
id = value;
NotifyPropertyChanged();
}
}
}
}
public class Class1: INotifyPropertyChanged
{
public static List<Class1> list=new List<Class1>();
public event PropertyChangedEventHandler? PropertyChanged;
private void OnNotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public List<Class1> List
{
get
{
return list;
}
set
{
if (value != list)
{
list = value;
OnNotifyPropertyChanged();
}
}
}
public int id;
public int ID
{
get
{
return id;
}
set
{
if (value != id)
{
id = value;
OnNotifyPropertyChanged();
}
}
}
}
<Window x:Class="WpfAppFOrTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local1="clr-namespace:WpfAppFOrTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local1:Class1 x:Key="incomeDataSource"/>
<DataTemplate x:Key="template1">
<TextBlock Text="{Binding}" FontSize="12" FontWeight="Bold" TextWrapping="Wrap"></TextBlock>
</DataTemplate>
</Window.Resources>
<!--<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Padding" Value="8"/>
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0,6,0,0"/>
</Style>
</Grid.Resources>
<Grid.DataContext>
<Binding Source="{StaticResource incomeDataSource}"/>
</Grid.DataContext>-->
<!--<Grid>
<ContentControl Name="contCtrl" ContentTemplate="{StaticResource template1}"
Content="This is the content of the content control."/>
</Grid>-->
<Grid VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<DataGrid x:Name="showProductDG" DataContext="{Binding Source={StaticResource incomeDataSource}}" ItemsSource="{Binding List}" VerticalAlignment="Stretch" AllowDrop="True" SelectionMode="Single" CanUserAddRows="False" Grid.Row="1" AlternationCount="2" AutoGenerateColumns="False" IsReadOnly="True" Grid.ColumnSpan="2" Margin="0,5,0,5" >
<DataGrid.Columns>
<DataGridTextColumn Header="ID"
Width="*"
Binding="{Binding ID, Mode=TwoWay}" />
<!--<DataGridTextColumn Header="阻值区间"
Width="3*"
Binding="{Binding ResistanceRange}" />-->
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>