Wpf列表框刷新问题

时间:2014-03-04 18:43:08

标签: c# list listbox refresh add

我的列表框有问题,当我添加一个新的人员时,它首先不会出现在列表中,但是在我关闭它并再次打开它之后,它会显示出来。

private void neuespersonal_Click_1(object sender, RoutedEventArgs e)
    {

            Personal p = new Personal();
            p.Vornameh4 = "NeuesPers";
            p.Nachnameq5 = "Nachname";
            Bestellungen b = new Bestellungen();
            b.Bestelldatumh8 = new DateTime(1982, 05, 21);

            p.Personal_Nrs7 = (short)(db.Personals.Max(s2 => s2.Personal_Nrs7) + 1);
            p.Bestellungens.Add(b);

            db.Personals.Add(p);
            db.SaveChanges();
            personal.Items.Refresh();

    }

我正在使用Item.Refresh()但是只有当我关闭程序并再次启动它时它仍然无效

<Window x:Class="BeispielWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="650"
    Loaded="Window_Loaded_1">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="70*"/>
        <ColumnDefinition Width="70*"/>
        <ColumnDefinition Width="70*"/>
    </Grid.ColumnDefinitions>
    <ListBox x:Name="personal" Grid.Column="0"
             DisplayMemberPath="Vornameh4" SelectedValuePath="Personal_Nrs7">        
    </ListBox>

    <ListBox x:Name="Bestellungen" Grid.Column="1"
             ItemsSource="{Binding SelectedItem.Bestellungens, ElementName=personal}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="########-Bestellung-########" />
                    <TextBlock Text="{Binding Path=Bestell_Nrq8}"/>
                    <TextBlock Text="{Binding Path=Bestelldatumh8, StringFormat=dd-MM-yyyy}"/>

                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>



    <StackPanel Grid.Column="2">

        <TextBlock Text="Empfänger" Margin="5 10 5 5"/>
        <TextBox Text="{Binding Path=SelectedItem.Empfaengere2, ElementName=Bestellungen}" Margin="5 0 5 0" />

        <TextBlock Text="Versandfirmen" Margin="5 10 5 5"/>
        <ComboBox x:Name="firmen" DisplayMemberPath="Firmam7" SelectedValuePath="Firmen_Nrl8" SelectedValue="{Binding Path=SelectedItem.VersandUeberp1, ElementName=Bestellungen}" Margin="5 0 5 10" />


        <TextBlock Text="Vorname" Margin="5 10 5 5"/>
        <TextBox Text="{Binding Path=SelectedItem.Vornameh4, ElementName=personal}" Margin="5 0 5 0" />
        <TextBlock Text="Nachname" Margin="5 10 5 5"/>
        <TextBox Text="{Binding Path=SelectedItem.Nachnameq5, ElementName=personal}" Margin="5 0 5 5" />


        <Button x:Name="speichern" Content="Speichern" Click="speichern_Click_1"/>
        <Button x:Name="löschen" Content="Löschen"  Click="löschen_Click_1"/>
        <Button x:Name="neuespersonal" Content="Neues Personal" Click="neuespersonal_Click_1"/>




    </StackPanel>

</Grid>

0 个答案:

没有答案