在尝试插入时,Selected为空

时间:2018-04-26 08:18:10

标签: c# sql wpf xaml

我试图从我的xaml窗口插入新数据,我使用SelectedBier绑定数据并使用InserComand将其传递给我的ViewModel,然后填写selectedBieren。但它不起作用,当我调试时,程序立即填满选定的贝伦,其中包含任何文本(因为我没有填写任何内容)。

这是我的代码:

class InventarisViewModel : BaseViewModel
{

    private ObservableCollection<Bier> bier;
    public ObservableCollection<Bier> Bier
    {
        get
        {
            return bier;
        }
        set
        {
            bier = value;
        }
    }

    public InventarisViewModel()
    {
        ReadBieren();
        Messenger.Default.Register<Bier>(this, OnBierReceived);
        KoppelenCommands();

    }
    private void KoppelenCommands()
    {
        InsertCommand = new BaseCommand(InsertBier);
    }
    BierDataService bierDS = new BierDataService();


    public void ReadBieren()
    {
        Bier = new ObservableCollection<Bier>(bierDS.GetBier());
    }

    private void OnBierReceived(Bier bier)
    {
        SelectedBier = bier;
    }

    private Bier selectedBier;
    public Bier SelectedBier
    {
        get
        {
            return selectedBier;
        }
        set
        {
            selectedBier = value;
            NotifyPropertyChanged();
        }
    }
    public ICommand InsertCommand { get; set; }
    private void InsertBier()
    {
        //BierDataService ds = new BierDataService();
        //ds.UpdateBier(SelectedBier);
        //Messenger.Default.Send<UpdateFinishedMessage>(new UpdateFinishedMessage("Completed"));
        if(selectedBier!=null)
        { 
        Bier bier = new Bier
        {
            brouwerijId = selectedBier.brouwerijId,
            soortId = selectedBier.soortId,
            alcoholpercentage = selectedBier.alcoholpercentage,
            naam = selectedBier.naam,
            aantal = selectedBier.aantal
        };


        bierDS.InsertBier(bier);
        ReadBieren();
        }
    }



}

这是我的XAML代码:

<Window x:Class="WPFinventaris.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFinventaris.View"
    DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=InventarisViewModel}"
    mc:Ignorable="d"
    Title="Inventaris" Height="493.621" Width="603.834">

<StackPanel Margin="0,0,0,-21" >

    <ComboBox Name="comboReeks" ItemsSource="{Binding Bier}"  SelectedItem="{Binding SelectedSoort}"  Margin="10">
    </ComboBox>
    <DataGrid   Name="dataGridAlbums"  Margin="10" Background="White" ItemsSource="{Binding Bier}"  SelectedItem="{Binding SelectedAlbum}" AutoGenerateColumns="False"  AlternatingRowBackground="Gainsboro"  AlternationCount="2" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="id" Binding="{Binding id}" Width="60" />
            <DataGridTextColumn Header="Naam" Binding="{Binding naam}"  Width="100"/>
            <DataGridTextColumn Header="Alcoholpercentage" Binding="{Binding alcoholpercentage,StringFormat={}{0}%}" Width="auto"/>
            <DataGridTextColumn Header="Aantal" Binding="{Binding aantal}"  Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
    <Grid Margin="10" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="Auto" MinWidth="35" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Content="Nummer:"/>
        <Label Grid.Row="0" Grid.Column="2" Content="Titel:" HorizontalAlignment="Right" Width="35"/>
        <TextBox Grid.Column="1" Grid.Row="0" Margin="3" Text="{Binding SelectedAlbum.Nummer, Mode=TwoWay}"   />
        <TextBox Grid.Column="3" Grid.Row="0" Margin="3" Text="{Binding SelectedAlbum.Titel, Mode=TwoWay}"/>
        <Button Grid.Column="3" Grid.Row="1" HorizontalAlignment="Center" 
        MinWidth="80" Margin="112,3" Content="Verwijderen" Command="{Binding DeleteAlbumCommand}" Width="80"  />
        <!--<Button Grid.Column="3" Grid.Row="2" HorizontalAlignment="Right" 
        MinWidth="80" Margin="3" Content="Toevoegen"  Click="Button_Click"  />-->
    </Grid>

    <Grid Grid.Column="1" Margin="20" Height="192" >
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="30" ></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="auto" MinHeight="23"></RowDefinition>
            <RowDefinition Height="21"></RowDefinition>
            <RowDefinition Height="auto" MinHeight="58"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>


        <TextBlock Grid.Row="1" Text="Naam:" FontWeight="Bold" />
        <TextBlock Grid.Row="2" Text="SoortId:" FontWeight="Bold" />
        <TextBlock Grid.Row="3" Text="BrouwerijId:" FontWeight="Bold" />
        <TextBlock Grid.Row="4" Text="Alcohol %:" FontWeight="Bold" />
        <TextBlock Grid.Row="5" Text="Aantal:" FontWeight="Bold" />
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding SelectedBier.Naam}" Margin="0,0,0,10"/>
        <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding SelectedBier.SoortId}" Margin="0,0,0,10"/>
        <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding SelectedBier.BrouwerijId}" Margin="0,0,0,10" />
        <TextBox Grid.Row="4" Grid.Column="1" Text="{Binding SelectedBier.Alcoholpercentage}" Margin="0,0,0,10" />
        <TextBox Grid.Row="5" Grid.Column="1" Text="{Binding SelectedBier.Aantal}" />

        <Button Grid.Row="6" Content="Bewaren" Command="{Binding InsertCommand}" Margin="10,10,10,0" Height="26" VerticalAlignment="Top"/>

    </Grid>
    <Label Content="Label"/>
</StackPanel>

0 个答案:

没有答案