Cześć próbuję w swojej aplikacji WPF wykorzystać bindowanie modelu danych.
Kod WPF wygląda następująco:
<Window x:Class="KPIR.V2.Modules.Dictionaries.DocumentTypeDictionary"
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:KPIR.V2.Modules.Dictionaries"
mc:Ignorable="d"
Title="Rodzaje dokumentów" Height="300" Width="400" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBox x:Name="tbDocumentType" Text="{Binding DocumentType,Mode=TwoWay}" Height="20" Margin="5, 0" TextChanged="tbDocumentType_TextChanged"/>
<Button x:Name="btnSave" Content="Zapisz" Grid.Column="1" Height="25" Width="90" Click="btnSave_Click"/>
<ListBox x:Name="lbDocumentTyped" Grid.Row="1" />
<StackPanel Grid.Row="1" Grid.Column="1" Margin="5, 0">
<Button x:Name="btnEdit" Content="Edytuj" Height="25"/>
<Button x:Name="btnDelete" Content="Usuń" Height="25" Margin="0, 5, 0, 0"/>
</StackPanel>
</Grid>
</Window>
Kod okna:
using KPIR.V2.Extentions;
using KPIR.V2.Logic;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
namespace KPIR.V2.Modules.Dictionaries
{
/// <summary>
/// Logika interakcji dla klasy DocumentTypeDictionary.xaml
/// </summary>
public partial class DocumentTypeDictionary : Window
{
private AccountingDocumentTypeModel _documentType;
public DocumentTypeDictionary()
{
InitializeComponent();
_documentType = new AccountingDocumentTypeModel();
DataContext = _documentType;
UpdateStatment();
}
private void UpdateStatment(List<string> types = null)
{
using (var con = new KPIRContext())
{
if (types == null)
lbDocumentTyped.ItemsSource = con.AccountingDocumentTypes.Select(x => x.DocumentType).ToList();
else
lbDocumentTyped.ItemsSource = types;
}
}
private void tbDocumentType_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
using (var con = new KPIRContext())
{
if (string.IsNullOrEmpty(tbDocumentType.Text))
UpdateStatment();
else
{
var doc = con.AccountingDocumentTypes.Where(x => x.DocumentType.ToLower().Contains(_documentType.DocumentType.ToLower())).ToList();
if (doc != null)
{
UpdateStatment(doc.Select(x => x.DocumentType).ToList());
}
}
}
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
AccountingDocumentType.SaveDocumentType(_documentType);
UpdateStatment();
}
}
}
Model danych:
public class AccountingDocumentTypeModel
{
[Key]
public int Id { get; set; }
[StringLength(50)]
public string DocumentType { get; set; }
public virtual List<KPIREntryModel> KPIREntries { get; set; }
}
Powyższy kod nie przypisuje do property DocumentType
żadnych wartości. Nie rozumiem co w tym kodzie jest nie tak, skoro w przykładowych kodach działa poprawnie.