itemsSource DataContext 同步属性绑定

 如果需要实时监视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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值