Cześć,
Mam problem i proszę o pomoc bo motam się z tym strasznie
Mam takie dwie klasy:
namespace BK
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public partial class permission : INotifyPropertyChanged
{
private int ID;
private string Name;
public int id { get => ID; set { ID = value; OnPropertyChanged(nameof(id)); } }
public string name { get => Name; set { Name = value; OnPropertyChanged(nameof(name)); } }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
namespace BK
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public partial class user : INotifyPropertyChanged
{
private int ID;
private string Login;
private string Password;
private string FirstName;
private string LastName;
private int? Permissions_id;
private bool? Status;
private DateTime? AddDate;
private DateTime? ModifyDate;
public int id { get => ID; set { ID = value; OnPropertyChanged(nameof(id)); } }
public string login { get => Login; set { Login = value; OnPropertyChanged(nameof(login)); } }
public string password { get => Password; set {Password = value; OnPropertyChanged(nameof(password)); } }
public string firstName { get => FirstName; set { FirstName = value; OnPropertyChanged(nameof(firstName)); } }
public string lastName { get => LastName; set { LastName = value; OnPropertyChanged(nameof(lastName)); } }
public Nullable<int> permissions_id { get => Permissions_id; set{ Permissions_id = value; OnPropertyChanged(nameof(permissions_id)); } }
public Nullable<bool> status { get => Status; set {Status = value; OnPropertyChanged(nameof(status)); } }
public Nullable<System.DateTime> addDate { get => AddDate; set { AddDate = value; OnPropertyChanged(nameof(addDate)); } }
public Nullable<System.DateTime> modifyDate { get => ModifyDate; set { ModifyDate = value; OnPropertyChanged(nameof(modifyDate)); } }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Mam również ViewModel ( mam nadzieje, że udało mi się to poprawnie stworzyć)
namespace BK.ViewModel
{
class UserViewModel : INotifyPropertyChanged
{
private user _user;
private permission _permission;
public event PropertyChangedEventHandler PropertyChanged;
bkEntities _db;
public user user
{
get { return _user; }
set
{
_user = value;
OnPropertyChanged(nameof(user));
}
}
public permission permission { get { return _permission; } set { _permission = value; OnPropertyChanged(nameof(permission)); } }
private ObservableCollection<user> _lstUsers;
private ObservableCollection<permission> _lstPermissions;
public ObservableCollection<user> lstUsers
{
get { return _lstUsers; }
set
{
_lstUsers = value;
OnPropertyChanged(nameof(lstUsers));
}
}
public ObservableCollection<permission> lstPermissions
{
get { return _lstPermissions; }
set
{
_lstPermissions = value;
OnPropertyChanged(nameof(lstPermissions));
}
}
public UserViewModel()
{
_db = new bkEntities();
LoadSetting();
}
private void LoadSetting()
{
lstUsers = new ObservableCollection<user>(_db.users);
lstPermissions = new ObservableCollection<permission>(_db.permissions);
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I widok w którym mam Datagrida
<UserControl x:Class="BK.UserControlUsers"
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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:BK"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<DataGrid x:Name="DBGrid" ItemsSource="{Binding lstUsers}" HorizontalAlignment="Stretch" Height="auto" Margin="5,5,5,5" VerticalAlignment="Top" Width="auto" AutoGenerateColumns="False" MouseDoubleClick="DBGrid_MouseDoubleClick" SelectionMode="Single" SelectionUnit="FullRow" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding id}"/>
<DataGridTextColumn Header="Imię" Binding="{Binding firstName}"/>
<DataGridTextColumn Header="Nazwisko" Binding="{Binding lastName}"/>
<DataGridTextColumn Header="Login" Binding="{Binding login}"/>
<DataGridCheckBoxColumn Header="Status" Binding="{Binding status}"/>
<DataGridTemplateColumn Header="Upr">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cbStatus" Style="{StaticResource MaterialDesignFloatingHintComboBox}" ItemsSource="{Binding lstPermissions}" DisplayMemberPath="name"></ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Data dodania" Binding="{Binding addDate,ConverterCulture=pl-PL}"/>
<DataGridTextColumn Header="Data modyfikacji" Binding="{Binding modifyDate,ConverterCulture=pl-PL}"/
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Grid>
</UserControl>
I niestety nie potrafię w kolumnie upr wyświetlić zamiast ID uprawnień z klasy user, nazwy z klasy permission (na podstawie id z klasy user). Czyli użytkownik ma pole permission_id które jest w relacji z id z klasy permission.
Jest już późno i trochę kombinowania z mną więc proszę o pomoc ewentualnie o wskazówkę jak ogarnąć tego comboxa. Bo jeszcze przede mną edycja w nowym oknie tego wiersza z ustawieniem comboboxa na odpowiedniej wartości.