将ObservableCollection绑定到GridView

时间:2018-03-29 11:19:53

标签: c# wpf

我有ObservableCollection名为LesElem,其中包含

 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);
    }

我希望将其绑定到网格视图

<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>

但我的清单是空的。 一些帮助!请

2 个答案:

答案 0 :(得分:0)

尝试实现代码。这适合我。请尝试参考。我没有完成整个viewmodel的事情,并且添加了await Task.Delay(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 });
    }
}

答案 1 :(得分:-1)

    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
    }