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>

WPF 中,一个控件只能绑定到一个 DataContext,但是可以通过使用嵌套的控件来绑定多个 DataContext。例如,可以使用一个控件作为另一个控件的子元素,然后将不同的 DataContext 分别绑定到这两个控件。 具体来说,可以使用一个 ItemsControl 或一个 ContentControl 作为主控件,然后在其内部嵌套其他控件,每个控件都可以绑定到不同的 DataContext。然后,在主控件的 DataTemplate 中,可以使用绑定语法来绑定子控件的 DataContext。 例如,以下代码演示了如何在一个 ListBox 中绑定多个 DataContext,其中每个 ListBoxItem 都绑定到不同的数据对象: ```xml <ListBox ItemsSource="{Binding Items}"> <ListBox.ItemTemplate> <DataTemplate> <ListBoxItem> <StackPanel> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding Description}" /> <Button Content="Edit" Command="{Binding EditCommand}" /> </StackPanel> </ListBoxItem> </DataTemplate> </ListBox.ItemTemplate> </ListBox> ``` 在这个例子中,ListBox 的 ItemsSource 属性绑定到一个名为 "Items" 的集合,该集合包含多个对象,每个对象都有自己的 Name、Description 和 EditCommand 属性。在 DataTemplate 中,我们使用嵌套的 ListBoxItem 控件来显示每个对象的属性,并将它们分别绑定到它们自己的 DataContext。由于 ListBoxItem 是嵌套在 ListBox 内部的,所以它们可以继承 ListBox 的 DataContext,而我们只需要为每个 ListBoxItem 指定一个新的 DataContext,就可以绑定多个 DataContext
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值