添加新项目时,DataGrid不会触发集合设置器

时间:2015-12-30 15:01:35

标签: c# wpf xaml datagrid

我有一个包含两个文本列和一个复选框列的数据网格。每当用户手动输入值和标签输入新行时, CC = ["~/80/Blue/1.png","~/81/Blue/1.png","~/80/Blue/2.png","~/81/Blue/2.png","~/80/Black/1.png","~/81/Black/1.png","~/80/Black/2.png","~/81/Black/2.png"]; 都不会更新(集合的设置者不会触发)

var CC = AA.concat(BB);
CC.sort (function(a,b) {
    var Mask = /\W\w+\W\d\Wpng/;
    var A =[];
    var B = [];
    var s1 = a.toLowerCase();
    var s2 = b.toLowerCase();
    for (var i=0; i<s1.length; i++){
      A.push(s1[i].match(Mask));
    }
    for (var i=0; i<s2.length; i++){
      B.push(s2[i].match(Mask));
    }
    for (var i=0; i<A.length; i++){
      for (var j=0; j<B.length; j++){
        if (A[i] < B[j]) {return -1;};
        if (A[i] > B[j]) {return 1;};
        else {return 0;}
        }
    }

如何使其与物业同步?我应该实施ItemsSource吗?我应该在一个单独的集合类中执行此操作,如下例所示:

http://www.c-sharpcorner.com/UploadFile/yougerthen/C-Sharp-and-xaml-within-a-silverlight-2-context-inotifycollectionchanged-implementation-part-v/

2 个答案:

答案 0 :(得分:0)

您可以尝试在CollectionChanged事件中订阅该集合。调用该方法后,您将获得有关在列表中完成的操作的所有信息。例如,添加或删除元素......

答案 1 :(得分:0)

您可以通过CollectionChanged事件检查它,当添加新行时,集合的计数会发生变化

的Xaml

 <DataGrid HeadersVisibility="Column" RowHeaderWidth="0" Grid.Row="2" Grid.ColumnSpan="2" ItemsSource="{Binding Parameters,Mode=TwoWay}" 
                                      CanUserAddRows="True" AutoGenerateColumns="False" CanUserDeleteRows="True" MinHeight="140" 
                                      IsSynchronizedWithCurrentItem="True">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Key" Binding="{Binding Key,Mode=TwoWay}" Width="*"/>
                    <DataGridTextColumn Header="Value" Binding="{Binding Value,Mode=TwoWay}" Width="*"/>
                    <DataGridCheckBoxColumn Header="Post parameter" Binding="{Binding IsPostParameter,Mode=TwoWay}" Width="Auto" />
                </DataGrid.Columns>
            </DataGrid>

背后的代码

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Parameters = new ObservableCollection<ParamaterClass>();
            Parameters.CollectionChanged += Parameters_CollectionChanged;
            ParamaterClass ParamaterClass = new ParamaterClass();
            ParamaterClass.Key = "Test1Key";
            ParamaterClass.Value = "Test1Value";
            ParamaterClass.IsPostParameter = true;

            Parameters.Add(ParamaterClass);
        }

        void Parameters_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            int count = Parameters.Count;
        }