So, I have a TabControl binded to a list of projects (each tab is a one project) - that works fine. The content of each tab is a DataGrid with a list of project's employees - that works fine as well. Now, I want to show some information on employee currently selected on DataGrid. Here's some code:
MainWindow.xaml file:
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
<DataTemplate x:Key="ContentTemplate">
<DataGrid ItemsSource="{Binding Employees}" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}" SelectionMode="Extended" SelectionUnit="FullRow" Name="employeesList">
</DataGrid>
</DataTemplate>
</Window.Resources>
and later, I want to test this binding by simply writing it in label:
<Label Name="emp" Content="{Binding SelectedEmployee}"></Label>
and MainWindowViewModel:
public Employee SelectedEmployee { get { return selectedEmployee; }
set
{
if (selectedEmployee != value)
{
selectedEmployee = value;
NotifyPropertyChanged("SelectedEmployee");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
I am kind of a newbie to WPF, I've read some tips but they don't help. The label 'emp' does not show anything. What am I missing?
You are not notifying that your property has changed, Try this
public Employee SelectedEmployee
{
get { return selectedEmployee; }
set
{
if (selectedEmployee != value)
{
selectedEmployee = value;
LastName = value;
NotifyPropertyChanged("SelectedEmployee"); //NotifyPropertyChanged("SelectedItem");
}
}
}
Test:
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication6"
Title="MainWindow" Height="350" Width="763" Name="UI" >
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding ElementName=UI,Path=Employees}" SelectedItem="{Binding ElementName=UI,Path=SelectedEmployee}" SelectionMode="Extended" SelectionUnit="FullRow" Name="employeesList" Margin="0,41,0,0" />
<Label Content="{Binding ElementName=UI,Path=SelectedEmployee.Name}" Height="28" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" Width="288" />
<Label Content="{Binding ElementName=employeesList,Path=SelectedItem.Name}" Height="28" HorizontalAlignment="Left" Name="label2" VerticalAlignment="Top" Width="288" Margin="294,0,0,0" />
</Grid>
</Window>
Code:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();
private Employee _selectedEmployee;
public MainWindow()
{
InitializeComponent();
Employees.Add(new Employee { Name = "sa_ddam213" });
}
public ObservableCollection<Employee> Employees
{
get { return _employees; }
set { _employees = value; }
}
public Employee SelectedEmployee
{
get { return _selectedEmployee; }
set { _selectedEmployee = value; NotifyPropertyChanged("SelectedEmployee"); }
}
/// <summary>
/// Notifies the property changed.
/// </summary>
/// <param name="info">The info.</param>
public void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
public class Employee
{
public string Name { get; set; }
}
This seems to work as expected, or am I missing something?
Related
My goal is have each TabItem linked to a specific viewmodel. Furthermore, after transvering through each TabItem, the user input should not be reset. I am finding solutions for this and came across a potential solution but my testing failed me.
I have searched for answers and chose to do the following as it seems it applies the MVVM concept and it looks neat! However, I have a XAML binding error. I tried to replicate Jakob Christensen's answer provided in this link. I tried to debug and I think the issue is with the type of ObservableCollection that is created. It's an object type.
Thank you for helping!
Error
This is the XAML code for my view
<TabControl>
<TabItem DataContext="{Binding TabList[0]}" x:Name="Tab1" Header="Tab1" Margin="-2,-2,-2,2" >
<Grid>
<TextBox x:Name ="EnterNum1" Margin="300,100,300,300" Text="{Binding test1, Mode =TwoWay}"/>
<Button Name="RunBtn1" Command="{Binding Path=RunBtn1, Mode=TwoWay}" Content="RUN" HorizontalAlignment="Right" Width="180" Height="40" FontSize="18"/>
</Grid>
</TabItem>
<TabItem DataContext="{Binding TabList[1]}" x:Name="Tab2" Header="Tab2" >
<Grid>
<TextBox x:Name ="EnterNum2" Margin="300,100,300,300" Text="{Binding test2, Mode =TwoWay}" Grid.Column="1"/>
<Button Name="RunBtn2" Command="{Binding Path=RunBtn2, Mode=TwoWay}" Content="RUN" HorizontalAlignment="Right" Width="180" Height="40" FontSize="18"/>
</Grid>
</TabItem>
</TabControl>
This is the XAML.cs for my view
public ObservableCollection<object> TabList { get; set; }
public ImportData()
{
InitializeComponent();
TabList = new ObservableCollection<object>();
TabList.Add(new SampleViewModel1());
TabList.Add(new SampleViewModel2());
}
You should first set DataContext to TabControl or Window,
then you can bind the TabItem to the viewmodel(TabList) list item
XAML
<TabControl>
<TabItem DataContext="{Binding TabList[0]}" x:Name="Tab1" Header="Tab1" Margin="-2,-2,-2,2" >
<Grid>
<TextBox x:Name ="EnterNum1" Margin="300,100,300,240" Text="{Binding Test1, Mode =TwoWay}"/>
<Button Name="RunBtn1" Command="{Binding Path=RunBtn1, Mode=TwoWay}" Content="RUN" HorizontalAlignment="Right" Width="180" Height="40" FontSize="18"/>
</Grid>
</TabItem>
<TabItem DataContext="{Binding TabList[1]}" x:Name="Tab2" Header="Tab2" >
<Grid>
<TextBox x:Name ="EnterNum2" Margin="300,100,300,240" Text="{Binding Test2, Mode =TwoWay}" Grid.Column="1"/>
<Button Name="RunBtn2" Command="{Binding Path=RunBtn2, Mode=TwoWay}" Content="RUN" HorizontalAlignment="Right" Width="180" Height="40" FontSize="18"/>
</Grid>
</TabItem>
</TabControl>
XAML.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AllViewModel allViewModel = new AllViewModel();
allViewModel.TabList = new ObservableCollection<object>();
allViewModel.TabList.Add(new SampleViewModel1());
allViewModel.TabList.Add(new SampleViewModel2());
this.DataContext = allViewModel;
}
}
public class AllViewModel : INotifyPropertyChanged
{
private ObservableCollection<object> tabList;
public ObservableCollection<object> TabList
{
get => tabList;
set { tabList = value;RaiseChange("TabList"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaiseChange(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class SampleViewModel1 : INotifyPropertyChanged
{
private string test1 = "test1";
private ICommand runBtn1;
public string Test1
{
get => test1;
set { test1 = value;RaiseChange("Test1"); }
}
public ICommand RunBtn1
{
get => runBtn1;
set { runBtn1 = value;RaiseChange("RunBtn1"); }
}
public SampleViewModel1()
{
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaiseChange(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class SampleViewModel2 : INotifyPropertyChanged
{
private string test2 = "test2";
private ICommand runBtn2;
public string Test2
{
get => test2;
set { test2 = value; RaiseChange("Test2"); }
}
public ICommand RunBtn2
{
get => runBtn2;
set { runBtn2 = value; RaiseChange("RunBtn2"); }
}
public SampleViewModel2()
{
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaiseChange(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
run result
I followed few online tutorials to switch between dynamic views from a ListView.
In my MainWindow, I have a ListView in the left pane with ItemsSource of list of sub ViewModels. (Each sub ViewModel implements an Interface)
Each ViewModel have its own View as a DataTemplate.
I'm calling the GenerateReport() method of the selected view from the MainWindow. But the values of selected views becomes null.
Download my source from Github.
Steps to reproduce the issue:
Run the application and type text in the Students Report's Id and Name. (The breakpoints in StudentReportViewModels's properties are properly hitting and values updated.)
Then click Generate Report button. The StudentReportViewModels's properties values becomes null.
How to fix the issue? Please help.
Source:
MainWindow.xaml
<Window.Resources>
<DataTemplate DataType="{x:Type vm:StudentsReportViewModel}">
<view:StudentsReport/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:MarksReportViewModel}">
<view:MarksReport />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView ItemsSource="{Binding Reports}" SelectedItem="{Binding SelectedReport, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<GridSplitter Grid.Row="1" Grid.Column="1" Width="5" ResizeBehavior="PreviousAndNext" HorizontalAlignment="Stretch"/>
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<ContentControl Content="{Binding SelectedReport.ViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</ScrollViewer>
<Button Content="Generate Report" Grid.Row="2" Margin="5" HorizontalAlignment="Right" Command="{Binding GenerateReportCommand}"/>
</Grid>
</Grid>
MainWindowViewModel:
public class MainWindowViewModel : INotifyPropertyChanged
{
private ICommand _generateReportCommand;
private Report selectedReport;
public Report SelectedReport
{
get
{
return this.selectedReport;
}
set
{
if (value != this.selectedReport)
{
this.selectedReport = value;
NotifyPropertyChanged();
}
}
}
public List<Report> Reports { get; set; }
public ICommand GenerateReportCommand
{
get
{
if (_generateReportCommand == null)
{
_generateReportCommand = new RelayCommand(
p => GenerateReport()
);
}
return _generateReportCommand;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public MainWindowViewModel()
{
Reports = new List<Report>
{
new Report{ Name = "Students Report", ViewModel = new StudentsReportViewModel()},
new Report{ Name = "Marks Report", ViewModel = new MarksReportViewModel()}
};
SelectedReport = Reports[0];
}
public void GenerateReport()
{
SelectedReport.ViewModel.GenerateReport();
}
}
StudentsReport.xaml
<TextBox Height="25" Width="100" Margin="5" Text="{Binding Id, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Height="25" Width="100" Margin="5" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
StudentsReportViewModel:
public class StudentsReportViewModel : INotifyPropertyChanged, IReportViewModel
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string id;
public string Id
{
get
{
return this.id;
}
set
{
if (value != this.id)
{
this.id = value;
NotifyPropertyChanged();
}
}
}
private string name;
public string Name
{
get
{
return this.name;
}
set
{
if (value != this.name)
{
this.name = value;
NotifyPropertyChanged();
}
}
}
public StudentsReportViewModel()
{
}
public void GenerateReport()
{
System.Windows.MessageBox.Show($"Id = {Id}, Name = {Name}");
}
}
Interface:
public interface IReportViewModel
{
void GenerateReport();
}
Model:
public class Report
{
public string Name { get; set; }
public IReportViewModel ViewModel { get; set; }
}
Your StudentsReport.xaml UserControl is binding to an instance of the StudentsReportViewModel created in the XAML:
<UserControl.DataContext>
<vm:StudentsReportViewModel/>
</UserControl.DataContext>
The Generate Report button however is calling into another instance of the StudentsReportViewModel that is create in the MainWindowVieModel constructor and is stored in the Report class.
Reports = new List<Report>
{
new Report{ Name = "Students Report", ViewModel = new StudentsReportViewModel()},
new Report{ Name = "Marks Report", ViewModel = new MarksReportViewModel()}
};
You need to remove one of these instances so that the UserControl's DataContext is bound to the same view model instance that you are generating the report message from.
I suggest deleting this code from StudentsReport.xaml:
<UserControl.DataContext>
<vm:StudentsReportViewModel/>
</UserControl.DataContext>
I have an Employee Class That Contains two Property Id and Name.I was trying To show them in ComboBox and made a click event To show the Selected Item Through message box. The Click Event Is Working fine it shows me the correct value but in combobox it shows me the diffrent value. I am a newb in WPF MVVM. This is my EmployeeModelView Class
class EmployeeViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public ObservableCollection<Employee> Employee
{
get
{
return employee;
}
set
{
employee = value;
}
}
private ObservableCollection<Employee> employee = new ObservableCollection<Employee>
{
new Employee {Id=1,Name="asdasd"},
new Employee { Id=2,Name="wwerewr"}
};
private Employee selectedEmployee;
public Employee SelectedEmployee
{
get
{
return selectedEmployee;
}
set
{
selectedEmployee = value;
OnPropertyChanged("SelectedEmployee");
}
}
This is my View
<Grid>
<ComboBox x:Name="cmbEmployee"
ItemsSource="{Binding Employee}"
SelectedItem="{Binding SelectedEmployee}"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Center" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
I get this Output
This is my main Window Code
EmployeeViewModel emps;
public MainWindow()
{
InitializeComponent();
emps = new EmployeeViewModel();
this.DataContext = emps;
}
private void button_Click(object sender, RoutedEventArgs e)
{
Employee emp = emps.SelectedEmployee;
MessageBox.Show(emp.Id + " " + emp.Name);
}
You have to set ComboBox ItemTemplate:
<ComboBox x:Name="cmbEmployee"
ItemsSource="{Binding Employee}"
SelectedItem="{Binding SelectedEmployee}"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="120">
<ComboBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Id}"/>
<TextBlock Text="{Binding Name}" Margin="5,0,0,0"/>
</WrapPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Solution 1:
Have you tried to use DisplayMemberPath attribute to your ComboBox?
<ComboBox x:Name="cmbEmployee"
ItemsSource="{Binding Employee}"
SelectedItem="{Binding SelectedEmployee}"
DisplayMemberPath="Name"
HorizontalAlignment="Left" VerticalAlignment="Top" Width="120">
It shows the Name of the Employee in the Combo.
Solution 2:
It's better to override ToString of Employee
public class Employee{
public override string ToString(){
return $"{Id} {Name}";
}
}
Like this, your combo will show the text as you want. And it make more clair in button_Click
private void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(emps.SelectedEmployee?.ToString());
}
My problem is EMPTY Combobox list. I've walked through a lot of web site (during 3 days), but couldn't figure out it.
I've written a program that show a List of Students, clicking I can change properties them. So, I can change succsefuly all properties except Faculty (ComboBox).
Click to see View my programm
I used MVVM....
I have Class Student (Model). Here, General is enum StudentFaculty....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _15._01._2018.Model
{
public enum StudentFaculty { Programmer, SysAdministration, Designer};
class Student : INotifyPropertyChanged
{
private string _name;
private string _lastname;
private StudentFaculty _faculty;
private double _averageMark;
public string Name
{
get { return _name; }
set
{
if(_name == value) return;
_name = value;
OnPropertyChanged("Name");
}
}
public string Lastname
{
get { return _lastname; }
set
{
if (_lastname == value) return;
_lastname = value;
OnPropertyChanged("Lastname");
}
}
public StudentFaculty Faculty
{
get { return _faculty; }
set
{
if (_faculty == value) return;
_faculty = value;
OnPropertyChanged("Faculty");
}
}
public double AverageMark
{
get { return _averageMark; }
set
{
if (_averageMark == value) return;
_averageMark = value;
OnPropertyChanged("AverageMark");
}
}
public Student() { }
public Student(string name, string lastname, StudentFaculty faculty, double averageMark)
{
Name = name;
Lastname = lastname;
Faculty = faculty;
AverageMark = averageMark;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
Class ApplicationViewModel. Here, I make List (Faculties) from enum in Constructor, also SelectedFaculty...
using _15._01._2018.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _15._01._2018.ViewModel
{
class ApplicationViewModel : INotifyPropertyChanged
{
private Student _selectedStudent;
private string _selectedFaculty;
public ObservableCollection<Student> Students { get; set; }
public List<string> Faculties { get; set; }
public Student SelectedStudent
{
get { return _selectedStudent; }
set
{
if (_selectedStudent == value) return;
_selectedStudent = value;
OnChangedProperty("SelectedStudent");
}
}
public string SelectedFaculty
{
get { return _selectedFaculty; }
set
{
if (_selectedFaculty == value) return;
_selectedFaculty = value;
OnChangedProperty("SelectedFaculty");
}
}
public ApplicationViewModel()
{
Students = new ObservableCollection<Student>
{
new Student("Vova", "Zyabkin", StudentFaculty.Programmer, 9.4),
new Student("Vadym", "Lazariev", StudentFaculty.Programmer, 9),
new Student("SvEta", "Belyaeva", StudentFaculty.Designer, 9.8),
new Student("Vova", "Skachkov", StudentFaculty.SysAdministration, 8.7)
};
Faculties = new List<string>(Enum.GetNames(typeof(StudentFaculty)));
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnChangedProperty(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
XAML
<Window x:Class="_15._01._2018.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:_15._01._2018"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="14" />
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="14" />
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding Students}" SelectedItem="{Binding SelectedStudent}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
<TextBlock Text="{Binding Path=Lastname}"></TextBlock>
<!--<TextBlock Text="{Binding Path=Faculty}"></TextBlock>-->
<TextBlock Text="{Binding Path=AverageMark}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Column="1">
<StackPanel DataContext="{Binding SelectedStudent}">
<TextBlock Text="D A T A"></TextBlock>
<TextBlock Text="Name:"></TextBlock>
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="Lastname:"></TextBlock>
<TextBox Text="{Binding Lastname, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="Faculty:"></TextBlock>
<ComboBox ItemsSource="{Binding Faculties}" SelectedItem="{Binding SelectedFaculty}">
</ComboBox>
<TextBlock Text="Average Mark:"></TextBlock>
<TextBox Text="{Binding AverageMark, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>
</Grid>
</Grid>
</Window>
I have idea to create a Class with String property Faculty, but it's like with ListBox and Student.
To start off with, the DataContext for your ComboBox is set to the SelectedStudent, not the ApplicationViewModel (see the parent StackPanel). Second, you have the Faculties property returning a list of String, but the Student class has a property of StudentFaculty (binding for SelectedValue won't work).
Try the following:
Models/ViewModels:
class ApplicationViewModel : INotifyPropertyChanged
{
private Student _selectedStudent;
private StudentFaculty _selectedFaculty;
public ObservableCollection<Student> Students { get; set; }
public ObservableCollection<StudentFaculty> Faculties { get; set; }
public Student SelectedStudent
{
get => _selectedStudent;
set
{
if (_selectedStudent == value) return;
_selectedStudent = value;
OnChangedProperty("SelectedStudent");
}
}
public StudentFaculty SelectedFaculty
{
get => _selectedFaculty;
set
{
if (_selectedFaculty == value) return;
_selectedFaculty = value;
OnChangedProperty("SelectedFaculty");
}
}
public ApplicationViewModel()
{
Students = new ObservableCollection<Student>
{
new Student("Vova", "Zyabkin", StudentFaculty.Programmer, 9.4),
new Student("Vadym", "Lazariev", StudentFaculty.Programmer, 9),
new Student("SvEta", "Belyaeva", StudentFaculty.Designer, 9.8),
new Student("Vova", "Skachkov", StudentFaculty.SysAdministration, 8.7)
};
Faculties = new ObservableCollection<StudentFaculty>(Enum.GetValues(typeof(StudentFaculty)).OfType<StudentFaculty>());
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnChangedProperty(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
public enum StudentFaculty { Programmer, SysAdministration, Designer };
class Student : INotifyPropertyChanged
{
private string _name;
private string _lastname;
private StudentFaculty _faculty;
private double _averageMark;
public string Name
{
get => _name;
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged("Name");
}
}
public string Lastname
{
get => _lastname;
set
{
if (_lastname == value) return;
_lastname = value;
OnPropertyChanged("Lastname");
}
}
public StudentFaculty Faculty
{
get => _faculty;
set
{
if (_faculty == value) return;
_faculty = value;
OnPropertyChanged("Faculty");
}
}
public double AverageMark
{
get => _averageMark;
set
{
if (_averageMark == value) return;
_averageMark = value;
OnPropertyChanged("AverageMark");
}
}
public Student() { }
public Student(string name, string lastname, StudentFaculty faculty, double averageMark)
{
Name = name;
Lastname = lastname;
Faculty = faculty;
AverageMark = averageMark;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
XAML:
<Window x:Class="WpfApp4.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:converters="clr-namespace:WpfApp4.Views.Converters"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:viewModels="clr-namespace:WpfApp4.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<viewModels:ApplicationViewModel />
</Window.DataContext>
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="14" />
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="14" />
</Style>
<CollectionViewSource x:Key="Faculties" Source="{Binding Faculties}"></CollectionViewSource>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding Students}" SelectedItem="{Binding SelectedStudent}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
<TextBlock Text="{Binding Path=Lastname}"></TextBlock>
<!--<TextBlock Text="{Binding Path=Faculty}"></TextBlock>-->
<TextBlock Text="{Binding Path=AverageMark}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Column="1">
<StackPanel DataContext="{Binding SelectedStudent}">
<TextBlock Text="D A T A"></TextBlock>
<TextBlock Text="Name:"></TextBlock>
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="Lastname:"></TextBlock>
<TextBox Text="{Binding Lastname, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="Faculty:"></TextBlock>
<ComboBox ItemsSource="{Binding Source={StaticResource Faculties}}" SelectedValue="{Binding Faculty, Mode=TwoWay}">
</ComboBox>
<TextBlock Text="Average Mark:"></TextBlock>
<TextBox Text="{Binding AverageMark, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>
</Grid>
</Grid>
I want bind the subject name in a textbox when the selected index is changed in comboxbox which i am using within a DataGrid
Here is my code
////
<Window x:Class="WpfGridDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Students="clr-namespace:WpfGridDemo"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Height="22" Width="113" Text="{Binding ElementName=ComboSubjects,Path= SelectedItem.Subject}"/>
<DataGrid x:Name="ComboSubjects" IsSynchronizedWithCurrentItem="True" Grid.Row="1" AutoGenerateColumns="False"
ItemsSource="{Binding StudentDetailsList, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name }" Header="Student Name" Width="200" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Subjects" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Subject}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="ComboSubjects" SelectedValuePath="SubjectNames"
SelectedValue="{Binding Subject}"
DisplayMemberPath="SubjectNames"
ItemsSource="{Binding Path=DataContext.SubjectList,Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
/////
using System;
using System.Windows;
using System.Windows.Data;
namespace WpfGridDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
StudentDetails studentDetailsList = new StudentDetails();
this.DataContext = studentDetailsList;
}
}
}
///
using System.Collections.ObjectModel;
namespace WpfGridDemo
{
public class StudentDetails
{
public ObservableCollection<Subjects> SubjectList
{
get;
set;
}
public ObservableCollection<StudentModel> StudentDetailsList
{
get;
set;
}
public StudentDetails()
{
StudentDetailsList = new ObservableCollection<StudentModel>();
StudentDetailsList.Add(new StudentModel() { Name = "Rohit", Subject = "Java" });
StudentDetailsList.Add(new StudentModel() { Name = "Tarun", Subject = "C#" });
SubjectList = new ObservableCollection<Subjects>();
SubjectList.Add(new Subjects() { SubjectNames = "Java" });
SubjectList.Add(new Subjects() { SubjectNames = "C#" });
SubjectList.Add(new Subjects() { SubjectNames = "Python" });
SubjectList.Add(new Subjects() { SubjectNames = "Rails" });
}
}
}
//
using System.ComponentModel;
namespace WpfGridDemo
{
public class StudentModel : INotifyPropertyChanged
{
private string subject;
public string Subject
{
get
{
return this.subject;
}
set
{
this.subject = value;
this.OnPropertyChanged("Subject");
}
}
public string Name
{ get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Subjects : INotifyPropertyChanged
{
private string _subjectNames;
public string SubjectNames
{
get
{
return this._subjectNames;
}
set
{
this._subjectNames = value;
this.OnPropertyChanged("SubjectNames");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Please Provide any solution to bind the items of the combobox to the texbox when the selection is changed.
you can achive this by setting IsSynchronizedWithCurrentItem="True" on DataGrid
<DataGrid x:Name="GridWithComboBox" IsSynchronizedWithCurrentItem="True" .....
and needs to change your ComboBox Binding to ...
SelectedValue="{Binding Subject,UpdateSourceTrigger=PropertyChanged}"