簡體   English   中英

ListBox不刷新?

[英]ListBox doesn't refresh?

我在使用列表框的Visual Studio(wpf)上遇到了問題。 如果我想從數據庫插入或刪除一些數據,那么它們正在工作,但如果我點擊menuitem一次,列表框將會刷新。 我認為這是由於加載方法,但為什么不刷新數據呢?

這是我的XAML代碼:

  <UserControl x:Class="WpfApplication1.AutorenBearbeiten"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="300" Loaded="UserControl_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="28*" />
            <ColumnDefinition Width="36*" />
        </Grid.ColumnDefinitions>
        <TextBlock Text="Medien" Grid.ColumnSpan="2" 
                   FontSize="16" />





        <ListBox x:Name="box" Grid.Row="1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=at_nachname}" />



                </DataTemplate>

            </ListBox.ItemTemplate>
        </ListBox>

        <StackPanel DataContext="{Binding ElementName=box,Path=SelectedItem}" Grid.Column="1" Grid.Row="1" >
            <TextBlock Text="Autoren_id" />
            <TextBox  Text="{Binding Path=at_id}"  MaxLength="5"/>
            <TextBlock Text="Vorname"  />
            <TextBox   Text="{Binding Path=at_vorname}"   MaxLength="30"/>
            <TextBlock Text="Nachname" />
            <TextBox  Text="{Binding Path=at_nachname}"  MaxLength="30"/>
            <TextBlock Text="Geburtsdatum"  />
            <TextBox   MaxLength="30"  Text="{Binding Path=at_gebDatum, StringFormat=dd.MM.yyyy}" />
            <Button Name="speichern" Height="23" Margin="4" Click="speichern_Click">Änderungen speichern</Button>
            <Button Name="loeschen" Height="23" Margin="4" Click="loeschen_Click">Löschen</Button>

            <StackPanel DataContext="{Binding ElementName=box}" Grid.Column="1" Grid.Row="1" >
                <TextBlock Text="Autoren_id" />
                <TextBox x:Name="id"  MaxLength="5"/>
                <TextBlock Text="Vorname" />
                <TextBox  x:Name="vorname"   MaxLength="30"/>
                <TextBlock Text="Nachname" />
                <TextBox x:Name="nachname"   MaxLength="30"/>
                <TextBlock Text="Geburtsdatum" />
                <TextBox  x:Name="datum" MaxLength="30"/>
                <Button x:Name="neubutton"  Height="23" Margin="4" Click="neu_Click">Neu</Button>
                <TextBlock Name="submitfehler" FontWeight="Bold" Foreground="Red" />
            </StackPanel>

        </StackPanel>

    </Grid>
</UserControl>

這是xaml.cs文件:

    using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for AutorenBearbeiten.xaml
    /// </summary>
    public partial class AutorenBearbeiten : UserControl
    {
        libraryEntities6 db = new libraryEntities6();
        public AutorenBearbeiten()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            var erg = db.a_autor;

            erg.Load();
            box.ItemsSource = erg.Local.OrderBy(m => m.at_id);

            box.ItemsSource =
               (from m in db.a_autor
                orderby m.at_id
                select m).ToList();


    }

        private void speichern_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                db.SaveChanges();
            }
            catch(Exception e1)
            {
                submitfehler.Text = e1.Message;
            }
        }

        private void loeschen_Click(object sender, RoutedEventArgs e)
        {

            a_autor am = (a_autor)box.SelectedItem;
            if (am != null)
            {

                db.a_autor.Remove(am);
                db.SaveChanges();
                box.Items.Refresh();
            }

        }

        private void neu_Click(object sender, RoutedEventArgs e)
        {


            a_autor autor = new a_autor();
            autor.at_id = id.Text;
            autor.at_vorname = vorname.Text;
            autor.at_nachname = nachname.Text;
            autor.at_gebDatum = Convert.ToDateTime(datum.Text);


            //s1.s_k_klasse = liklassen.SelectedValue.ToString() setzt die Klasse via foreign key
            //db.schuelers.AddObject(s1);


            db.a_autor.Add(autor);
            box.Items.Refresh();



            /*
            ((klassen))liklassen.SelectedItem).schuelers.Add(s1); //setzt die klasse durch zuweisen zum nav.Property
            lischueler.Items.Refresh(); //nötig weil das navigational seit ER 5 nicht observable ist
            */

        }
    }
}

窗口中的圖片如下:

窗口

您應該使用MVVM模式而不是代碼隱藏並使用屬性更改。 谷歌的第一個結果:

https://www.codeproject.com/Articles/819294/WPF-MVVM-step-by-step-Basics-to-Advance-Level

我希望它對你有所幫助。

胡安

您可以創建ObservableCollection而不是綁定到List ObservableCollection實現了INotifyPropertyChanged因此它可以在容器中發生更改時發送通知。

另外,我建議你試試

public void RefreshListBox()
{
     box.ItemsSource =
               (from m in db.a_autor
                orderby m.at_id
                select m).ToList();

}

並在db.SaveChanges()之后調用它

ListBox和數據庫之間沒有神奇的連接,因此當您調用DbContext的Add或Remove方法時,ListBox不會受到影響。

你應該做的是將ListBox的ItemsSource屬性設置為ObservableCollection<a_autor> ,然后調用此方法的Add / Remove方法,同時調用DbContext的Add / Remove方法:

System.Collections.ObjectModel.ObservableCollection<a_autor> _sourceCollection;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    var erg = db.a_autor;
    erg.Load();

    _sourceCollection = new System.Collections.ObjectModel.ObservableCollection<a_autor>((from m in db.a_autor
                                                                                          orderby m.at_id
                                                                                          select m).ToList());
    box.ItemsSource = _sourceCollection;
}

private void loeschen_Click(object sender, RoutedEventArgs e)
{
    a_autor am = (a_autor)box.SelectedItem;
    if (am != null)
    {
        _sourceCollection.Remove(am);
        db.a_autor.Remove(am);
        db.SaveChanges();
        box.Items.Refresh();
    }

}

private void neu_Click(object sender, RoutedEventArgs e)
{
    a_autor autor = new a_autor();
    autor.at_id = id.Text;
    autor.at_vorname = vorname.Text;
    autor.at_nachname = nachname.Text;
    autor.at_gebDatum = Convert.ToDateTime(datum.Text);

    _sourceCollection.Add(autor);
    db.a_autor.Add(autor);
    box.Items.Refresh();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM