Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ObservableCollection绑定到GridView_C#_Wpf - Fatal编程技术网

C# 将ObservableCollection绑定到GridView

C# 将ObservableCollection绑定到GridView,c#,wpf,C#,Wpf,我有一个名为LesElem的ObservableCollection,其中包含 public class ListBoxArticle :INotifyPropertyChanged { private int chap; public int Chap { get { return chap; } set { chap = value; OnPropertyChanged

我有一个名为
LesElem
ObservableCollection
,其中包含

 public class ListBoxArticle :INotifyPropertyChanged
{
    private int chap;

    public int Chap
    {
        get { return chap; }
        set
        {
            chap = value;
            OnPropertyChanged("Chap");
            OnPropertyChanged("Article");
        }
    }

    private string article;

    public string Article
    {
        get { return article; }
        set
        {
            article = value;
            OnPropertyChanged("Article");
            OnPropertyChanged("Chap");
        }
    }

    private bool isChecked;

    public bool IsChecked
    {
        get { return isChecked; }
        set
        {
            isChecked = value;
            OnPropertyChanged("IsChecked");
        }
    }

    private float somme;

    public float Somme
    {
        get { return somme; }
        set
        {
            somme = value;
            OnPropertyChanged("Somme");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertySomme)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertySomme));
    }
}
我是这样填写的

public ObservableCollection<ListBoxArticle> LesElem { get; set; }
List<ListBoxArticle> check = new List<ListBoxArticle>();

    private void SelectArt_Checked(object sender, RoutedEventArgs e)
    {
        //Remplissage des Elements Sélectionnés
        check.AddRange(LesArticles.Where(x => x.IsChecked == true && !check.Contains(x)));
        LesElem = new ObservableCollection<ListBoxArticle>(check);
    }
公共可观测集合LesElem{get;set;} 列表检查=新列表(); 已选中private void SelectArt_(对象发送方、路由目标方) { //元素选择信息 check.AddRange(LesArticles.Where(x=>x.IsChecked==true&&!check.Contains(x)); LesElem=新的可观察收集(检查); } 我想把它绑定到一个网格视图

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <ListView x:Name="ToConfirm" Margin="5,5,5,5" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding LesElem}">
        <ListView.View>
            <GridView x:Name="ListArtChap">
                <GridViewColumn Header="Chap"  DisplayMemberBinding="{Binding Chap}" Width="123"/>
                <GridViewColumn Header="Art"  DisplayMemberBinding="{Binding Article}" Width="123"/>
                <GridViewColumn Header="Montant"  DisplayMemberBinding="{Binding Somme}" Width="123"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

但我的名单是空的。 救命啊!请

列表检查=新建列表();
    List<ListBoxArticle> check = new List<ListBoxArticle>();

    private void SelectArt_Checked(object sender, RoutedEventArgs e)
    {
        //Remplissage des Elements Sélectionnés
        check.AddRange(LesArticles.Where(x => x.IsChecked == true && !check.Contains(x)));
        LesElem = new ObservableCollection<ListBoxArticle>(check);

        OnPropertyChanged("LesElem"); // <-- something like this
    }
已选中private void SelectArt_(对象发送方、路由目标方) { //元素选择信息 check.AddRange(LesArticles.Where(x=>x.IsChecked==true&&!check.Contains(x)); LesElem=新的可观察收集(检查);
OnPropertyChanged(“LesElem”);//正在尝试实现您的代码。这对我来说很有效。请尝试参考这一点。我还没有完成整个viewmodel工作,
等待任务。添加延迟(4000);
是为了检查在集合中某个时间后添加值时是否会反映出来

    ObservableCollection<ListBoxArticle> myList = new ObservableCollection<ListBoxArticle>();
    public MainWindow()
    {
        InitializeComponent();
         MYFun();
    }

    private async Task MYFun()
    {
        myList.Add(new ListBoxArticle { Article = "my", Chap = 1, IsChecked = true, Somme = 1 });
        ToConfirm.ItemsSource = myList;
        await Task.Delay(4000);
        myList.Add(new ListBoxArticle { Article = "my", Chap = 2, IsChecked = true, Somme = 2 });
    }
}
ObservableCollection myList=新的ObservableCollection();
公共主窗口()
{
初始化组件();
MYFun();
}
私有异步任务MYFun()
{
Add(newlistboxarticle{Article=“my”,Chap=1,IsChecked=true,Somme=1});
ToConfirm.ItemsSource=myList;
等待任务。延迟(4000);
Add(newlistboxarticle{Article=“my”,Chap=2,IsChecked=true,Somme=2});
}
}

您的模型是否实现了INotifyPropertyChanged接口?请提供类的代码,以及实例化列表的位置i制作了一个edit@Joker%37提供填充列表的代码ObservableCollection@rokkerboci实际上我有两个窗口,第一个窗口是向我的收藏中添加物品,当我完成后,我点击一个按钮,以便在gridview中显示此集合,以确认我不明白!如果wpf绑定失败(即未指向任何找到的属性),wpf将接受此操作,但在VS中调试时在输出日志中显示第一次意外异常。如果问题与绑定错误有关,则这可能是一个提示。啊,这就是您的问题。。。更新observablecollection-property后,您必须提交一个propertychanged。请如何操作!我已经更新了我的答案。要么触发如图所示的propertychanged。或者您确保obs coll从一开始就存在(以便绑定拾取它),然后在checked-handler中更改集合的内容。这是dataContext的问题吗?因为正如我所说,我有两个窗口,LesElems将填充第一个窗口。如果在两个窗口中都使用LesElems,则应在viewModel中声明。什么是viewModel?