I have a combobox that has an items source of type ObservableCollection<Clinic>
<ComboBox ItemsSource="{Binding Source={StaticResource ClinicList}}" DisplayMemberPath="Name" SelectedValue="{Binding Path=Name}" SelectedValuePath="Name"></ComboBox>
This combobox is within a ListView that is bound from EmployeeClinics.
public class Employee{
public ObservableCollection<Clinic> EmployeeClinics { get; set; }
}
When I launch the app I see the appropriate clinics. And the drop down seems to show the correct options, but when I update them, only the Name updates and not the ClinicId (it keeps previous ClinicId).
Edit: Similarly when I add a new clinic to the list and select it from the options, it's Id is 0 when I look at the collection.
Here is my clinic model.
public class Clinic {
public int ClinicId { get; set; }
public string _name { get; set; }
public string Name {
get {
return _name;}
set {
if (_name != value) {
_name = value;
}
}
}
}
UPDATE: Thanks #AyyappanSubramanian. I am making headway. I have updated my Objects
public class Employee{
public ObservableCollection<ClinicView> EmployeeClinics { get; set; }
}
public class ClinicView {
private Clinic selectedClinic;
public Clinic SelectedClinic {
get { return selectedClinic; }
set {
selectedClinic = value;
selectedClinicId = selectedClinic.ClinicId;
}
}
private int selectedClinicId;
public int SelectedClinicId {
get { return selectedClinicId; }
}
}
XAML:
<ComboBox ItemsSource="{Binding Source={StaticResource ClinicList}}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedClinic}"></ComboBox>
Changing the drop downs now properly changes the underlying object and updates the list as desired. Now my only issue is that the comboboxes don't display the current object, just show as blank on start. I've messed around with SelectedValue and Path with no luck. Any suggestions?
Refer the below code. You can use SelectedItem to get both the ID and Name in one SelectedObject. Get only ID using SelectedValue.
<ComboBox ItemsSource="{Binding Clinics}" DisplayMemberPath="ClinicName"
SelectedValuePath="ClinicId" SelectedValue="{Binding SelectedClinicId}"
SelectedItem="{Binding SelectedClinic}"/>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
class Clinic
{
public int ClinicId { get; set; }
public string ClinicName { get; set; }
}
class ViewModel
{
public ObservableCollection<Clinic> Clinics { get; set; }
public ViewModel()
{
Clinics = new ObservableCollection<Clinic>();
for (int i = 0; i < 10; i++)
{
Clinics.Add(new Clinic() { ClinicId=i+1,ClinicName="MyClinic"+(i+1) });
}
}
private int selectedClinicId;
public int SelectedClinicId
{
get { return selectedClinicId; }
set
{
selectedClinicId = value;
}
}
private Clinic selectedClinic;
public Clinic SelectedClinic
{
get { return selectedClinic; }
set
{
selectedClinic = value;
MessageBox.Show("ID:"+selectedClinic.ClinicId.ToString()+" "+"Name:"+selectedClinic.ClinicName);
}
}
}
I have a ListView which is populated with a collection called Files(ObservableCollection FileViewModel ), also I have another SelectedFiles(List Guid ) which hold the selected files id in it, how can I bind this to the UI to show the selected files with checkbox control.
Xaml:
<ListView Grid.Column="0" Grid.Row="2" Name="lstSourceFiles" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<CheckBox></CheckBox>
<TextBlock Text="{Binding Name}"></TextBlock>
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code:
public List<Guid> SelectedSourceFiles { get; set; }
public ObservableCollection<FileViewModel> Files { get; set; }
public class FileViewModel
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public partial class MainWindow : Window
{
public List<Guid> SelectedSourceFiles { get; set; }
public MainWindow()
{
AddHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(TreeItemSelected), true);
}
private void TreeItemSelected(object sender, RoutedEventArgs e)
{
var item = e.OriginalSource as TreeViewItem;
if (item == null)
{ return; }
var folder = item.DataContext as FolderViewModel;
if (folder == null)
{ return; }
if (!folder.IsFilesLoaded)
{
FileManager.LoadFiles(folder);
}
lstSourceFiles.ItemsSource = folder.Files;
}
}
The easiest way would be to add an "IsSelected" property to the view model:
public class FileViewModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
Loop through and set the property:
foreach (var file in folder.Files)
file.IsSelected = (SelectedSourceFiles.Contains(file.Guid);
And then of course bind to it:
<CheckBox IsChecked="{Binding IsSelected}" />
An alternate to the above would be to bind to the GUID and use IValueConverter that checks against the the selected list.
I'm attempting to access a listview subitem. I have to insert columns into this ListView/GridView dynamically as this program accesses multiple databases with different value types. If need be I can portion out the database searching into multiple tabs, but for ease of use, I'd rather not take this route.
I've been searching for roughly 3-4 days for a solution. This class organizes the subitems data from a OleDB query. Here is a snippet of the code I'm using:
public class Repair
{
public string RP { get; set; }
public string SN { get; set; }
public DateTime REC { get; set; }
public DateTime START { get; set; }
public string CUST { get; set; }
public string SP { get; set; }
public string TECH { get; set; }
public string STATUS { get; set; }
public string MODEL { get; set; }
public string NOTES { get; set; }
public DateTime ACCUSED { get; set; }
public string ACCNOTES { get; set; }
public int ID { get; set; }
}
public IList<Repair> OpenRepair { get; set; }
It then inserts the data into the ListView with the following snippet:
var gridView = new GridView();
this.searchListView.View = gridView;
gridView.Columns.Add(new GridViewColumn { Header = "RMA #", DisplayMemberBinding = new System.Windows.Data.Binding("RP") });
gridView.Columns.Add(new GridViewColumn { Header = "Serial", DisplayMemberBinding = new System.Windows.Data.Binding("SN") });
gridView.Columns.Add(new GridViewColumn { Header = "Recieved", DisplayMemberBinding = new System.Windows.Data.Binding("REC") });
gridView.Columns.Add(new GridViewColumn { Header = "Start", DisplayMemberBinding = new System.Windows.Data.Binding("START") });
gridView.Columns.Add(new GridViewColumn { Header = "Customer", DisplayMemberBinding = new System.Windows.Data.Binding("CUST") });
gridView.Columns.Add(new GridViewColumn { Header = "Sales Person", DisplayMemberBinding = new System.Windows.Data.Binding("SP") });
gridView.Columns.Add(new GridViewColumn { Header = "Technician", DisplayMemberBinding = new System.Windows.Data.Binding("TECH") });
gridView.Columns.Add(new GridViewColumn { Header = "Status", DisplayMemberBinding = new System.Windows.Data.Binding("STATUS") });
gridView.Columns.Add(new GridViewColumn { Header = "Repair Notes", DisplayMemberBinding = new System.Windows.Data.Binding("NOTES") });
gridView.Columns.Add(new GridViewColumn { Header = "Accidental Used Date", DisplayMemberBinding = new System.Windows.Data.Binding("ACCUSED") });
gridView.Columns.Add(new GridViewColumn { Header = "Accidental Notes", DisplayMemberBinding = new System.Windows.Data.Binding("ACCNOTES") });
gridView.Columns.Add(new GridViewColumn { Header = "ID", DisplayMemberBinding = new System.Windows.Data.Binding("ID"), Width = 0 });
using (OleDbConnection cn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=X:\***"))
{
cmd.Connection = cn;
cmd.CommandText = sqlstatement;
try
{
cn.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
OpenRepair = new List<Repair>();
if (dr[10].ToString() == "True")
{
OpenRepair.Add(
new Repair()
{
RP = dr[0].ToString(),
SN = dr[1].ToString(),
REC = Convert.ToDateTime(dr[2].ToString()).Date,
START = Convert.ToDateTime(dr[3].ToString()).Date,
CUST = dr[4].ToString(),
SP = dr[5].ToString(),
TECH = dr[6].ToString(),
STATUS = dr[7].ToString(),
MODEL = dr[8].ToString(),
NOTES = dr[9].ToString(),
ACCUSED = Convert.ToDateTime(dr[11].ToString()),
ACCNOTES = dr[12].ToString(),
ID = Convert.ToInt32(dr[13].ToString())
});
}
else
{
OpenRepair.Add(
new Repair()
{
RP = dr[0].ToString(),
SN = dr[1].ToString(),
REC = Convert.ToDateTime(dr[2].ToString()).Date,
START = Convert.ToDateTime(dr[3].ToString()).Date,
CUST = dr[4].ToString(),
SP = dr[5].ToString(),
TECH = dr[6].ToString(),
STATUS = dr[7].ToString(),
MODEL = dr[8].ToString(),
NOTES = dr[9].ToString(),
ID = Convert.ToInt32(dr[13].ToString())
});
}
searchListView.Items.Add(OpenRepair);
}
The XAML behind the ListView:
<ListView ItemsSource="{Binding Repair}" SelectionMode="Single" x:Name="searchListView" Margin="0,63,0,0" Background="DarkGray" MouseDoubleClick="searchListView_MouseDoubleClick">
<ListView.View>
<GridView />
</ListView.View>
</ListView>
I've attempted to grab the Subitem values within the ListView/GridView with a few approaches. It has not been successful on either of these codes:
Repair lvi = (Repair)searchListView.SelectedItems[0];
System.Windows.MessageBox.Show(lvi.RP + " " + lvi.SN + " " + lvi.SP);
&
Repair lvi = (Repair)this.customersListView.SelectedItem;
MessageBox.Show(string.Format("Repair: {1}{0}Serial Number:{2}", Environment.NewLine, lvi.RP, lvi.SN));
Even attempted a Windows Form approach, which obviously did not work.
Could someone please at least point me in the correct direction? The error message that I've been obtaining makes sense being it cannot be cast from the object Generic List to class Repair, however, since I am fairly new to WPF, I'm stumped on how to get passed this message!
I tested your code and it works for me, so you probably have got some mistake in the part that you didn't show. But let me try to win you over for using the MVVM pattern, it will make your life so much easier in the long run...
The ViewModel and your Repair class (swapped DateTime for string):
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1.Models
{
public class ListViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public ObservableCollection<Repair> Repairs { get; private set; }
public ActionCommand DoubleClickCommand { get; private set; }
private Repair _selectedRepair;
public Repair SelectedRepair { get { return _selectedRepair; } set { _selectedRepair = value; OnPropertyChanged("SelectedRepair"); } }
public ListViewModel()
{
this.Repairs = new ObservableCollection<Repair>();
this.Repairs.Add(new Repair { RP = "1000", SN = "A", START = DateTime.Today.ToString("d"), CUST = "C", ID = 0 });
this.Repairs.Add(new Repair { RP = "2000", SN = "D", REC = DateTime.Today.AddDays(-2).ToString("d"), CUST = "E", ID = 1 });
this.DoubleClickCommand = new ActionCommand(DoubleClick);
}
private void DoubleClick()
{
// do whatever (probably not show a MessageBox..)
MessageBox.Show(string.Format("Repair: {1}{0}Serial Number:{2}", Environment.NewLine, this.SelectedRepair.RP, this.SelectedRepair.SN));
}
}
public class ActionCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action _action;
public ActionCommand(Action action) { _action = action; }
public bool CanExecute(object parameter) { return true; }
public void Execute(object parameter) { if (_action != null) _action(); }
}
public class Repair
{
public string RP { get; set; }
public string SN { get; set; }
public string REC { get; set; }
public string START { get; set; }
public string CUST { get; set; }
public string SP { get; set; }
public string TECH { get; set; }
public string STATUS { get; set; }
public string MODEL { get; set; }
public string NOTES { get; set; }
public string ACCUSED { get; set; }
public string ACCNOTES { get; set; }
public int ID { get; set; }
}
}
The View:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:models="clr-namespace:WpfApplication1.Models"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="MainWindow" Height="350" Width="500">
<Window.DataContext>
<models:ListViewModel />
</Window.DataContext>
<ListView ItemsSource="{Binding Repairs}" SelectedItem="{Binding SelectedRepair}" SelectionMode="Single" Margin="0,63,0,0" Background="DarkGray">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding DoubleClickCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListView.View>
<GridView>
<GridViewColumn Header="RMA #" DisplayMemberBinding="{Binding RP}" />
<GridViewColumn Header="Serial" DisplayMemberBinding="{Binding SN}"/>
<GridViewColumn Header="Recieved" DisplayMemberBinding="{Binding REC}"/>
<GridViewColumn Header="Start" DisplayMemberBinding="{Binding START}"/>
<GridViewColumn Header="Customer" DisplayMemberBinding="{Binding CUST}"/>
<GridViewColumn Header="Sales Person" DisplayMemberBinding="{Binding SP}"/>
<GridViewColumn Header="Technician" DisplayMemberBinding="{Binding TECH}"/>
<GridViewColumn Header="Status" DisplayMemberBinding="{Binding STATUS}"/>
<GridViewColumn Header="Repair Notes" DisplayMemberBinding="{Binding NOTES}"/>
<GridViewColumn Header="Accidental Used Date" DisplayMemberBinding="{Binding ACCUSED}"/>
<GridViewColumn Header="Accidental Notes" DisplayMemberBinding="{Binding ACCNOTES}"/>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" Width="0" />
</GridView>
</ListView.View>
</ListView>
</Window>
Following my comment, here is a more complete answer, for future reference.
The selected object (the one displayed by the SelectedItem) is available through SelectedValue.
And as you use List then you should do the following:
((List<Repair>)searchListView.SelectedValue)[0].SN
I'm trying to fill ListBox by ObservableCollection. But when I add new item nothing displayed, only empty item adding.
There are fragments of my code:
XAML
<ListView ItemsSource="{Binding Points}" SelectedItem="{Binding Point}">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header ="X" Width="100" DisplayMemberBinding = "{Binding Path=ValueX, Mode=TwoWay}" />
<GridViewColumn Header ="Y" Width="100" DisplayMemberBinding = "{Binding Path=ValueY, Mode=TwoWay}"/>
</GridView>
</ListView.View>
</ListView>
Window class
var value = new Value();
var viewModel = new ViewModel(value);
DataContext = viewModel;
InitializeComponent();
Value class
private const Point POINT = null;
private readonly ObservableCollection<Point> _points = new ObservableCollection<Point>();
public Value() {
Point = POINT;
Points = _points;
}
public Point Point { get; set; }
public ObservableCollection<Point> Points { get; private set; }
public double ValueX { get; set; }
public int ValueY { get; set; }
ViewModel class
private readonly Value _value;
public ViewModel(Value value) {
_value = value;
}
public Point Point {
get { return _value.Point; }
set {
_value.Point = value;
OnPropertyChanged("Point");
}
}
public ObservableCollection<Point> Points {
get { return _value.Points; }
}
private RelayCommand _addCommand;
public ICommand AddCommand {
get {
if (_addCommand == null) {
_addCommand = new RelayCommand(Add);
}
return _addCommand;
}
}
private void Add(object obj) {
Points.Add(new Point(ValueX, ValueY));
ValueX = 0;
ValueY = 0;
}
public double ValueX {
get {
return _value.ValueX;
}
set {
if(Math.Abs(_value.ValueX - value) < Mathematics.EPSILON) return;
_value.ValueX = value;
OnPropertyChanged("ValueX");
}
}
public int ValueY {
get { return _value.ValueY; }
set {
if(_value.ValueX == value) return;
_value.ValueY = value;
OnPropertyChanged("ValueY");
}
}
and Point class
public class Point {
public readonly double ValueX;
public readonly double ValueY;
public Point(double valueX, double valueY) {
ValueX = valueX;
ValueY = valueY;
}
public override string ToString() {
return (ValueX + " " + ValueY);
}
}
When i try to add new item, new item is added but nothing is displayed. What reason can be here?
Since you bind ItemsSource to ObservableCollection<Point> it means that each item is of a Point type which has ValueX and ValueY declared as fields which are not valid binding source. Change them to properties:
public double ValueX { get; private set; }
public double ValueY { get; private set; }
Besides you use Mode=TwoWay for something that is read only. This should be changed to OneWay. If you want to leave TwoWay binding then remove private from the setter but then also you'll need to change GridViewColumn.CellTemplate to be some TextBox instead of using DisplayMemberBinding which is for display only.
I have the Xaml which should basically bind a set of ContextualButtons for a selected tab's viewmodel to the ItemsSource property of the ToolBar. For some reason, this binding is not actually occuring unless I use Snoop to inspect the element manually...It seems that the act of snooping the element is somehow requerying the binding somehow.
Does anyone know what I might be doing wrong here? This behavior is the same if I use a Listbox as well, so I know it is something that I am doing incorrectly...but I am not sure what.
SelectedView is a bound property to the selected view from a Xam Tab control.
XAML
<ToolBar DataContext="{Binding SelectedView.ViewModel}"
ItemsSource="{Binding ContextualButtons}" >
<ToolBar.ItemTemplate>
<DataTemplate>
<!-- <Button ToolTip="{Binding Name}"-->
<!-- Command="{Binding Command}">-->
<!-- <Button.Content>-->
<!-- <Image Width="32" Height="32" Source="{Binding ImageSource}"/>-->
<!-- </Button.Content>-->
<!-- </Button>-->
<Button Content="{Binding Name}"/>
</DataTemplate>
</ToolBar.ItemTemplate>
</ToolBar>
Code
public class TestViewModel : BaseViewModel, IBulkToolViewModel
{
public TestViewModel()
{
ContextualButtons = new ObservableCollection<IContextualButton>()
{
new ContextualButton("Test Button",
new DelegateCommand<object>(
o_ => Trace.WriteLine("Called Test Button")), String.Empty)
};
}
public string Key { get; set; }
private ObservableCollection<IContextualButton> _contextualButtons;
public ObservableCollection<IContextualButton> ContextualButtons
{
get { return _contextualButtons; }
set
{
if (_contextualButtons == value) return;
_contextualButtons = value;
//OnPropertyChanged("ContextualButtons");
}
}
}
public partial class TestView : UserControl, IBulkToolView
{
public TestView()
{
InitializeComponent();
}
public IBulkToolViewModel ViewModel { get; set; }
}
public class ContextualButton : IContextualButton
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public ICommand Command { get; set; }
public string ImageSource { get; set; }
public ContextualButton(string name_, ICommand command_, string imageSource_)
{
Name = name_;
Command = command_;
ImageSource = imageSource_;
}
}
public class BulkToolShellViewModel : BaseViewModel, IBaseToolShellViewModel, IViewModel
{
private IBulkToolView _selectedView;
public IBulkToolView SelectedView
{
get
{
return _selectedView;
}
set
{
if (_selectedView == value) return;
_selectedView = value;
OnPropertyChanged("SelectedView");
}
}
public ObservableCollection<IBulkToolView> Views { get; set; }
public DelegateCommand<object> AddViewCommand { get; private set; }
public DelegateCommand<object> OpenPortfolioCommand { get; private set; }
public DelegateCommand<object> SavePortfolioCommand { get; private set; }
public DelegateCommand<object> GetHelpCommand { get; private set; }
public BulkToolShellViewModel(ObservableCollection<IBulkToolView> views_)
: this()
{
Views = views_;
}
public BulkToolShellViewModel()
{
Views = new ObservableCollection<IBulkToolView>();
AddViewCommand = new DelegateCommand<object>(o_ => Views.Add(new TestView
{
ViewModel = new TestViewModel()
}));
OpenPortfolioCommand = new DelegateCommand<object>(OpenPortfolio);
SavePortfolioCommand = new DelegateCommand<object>(SavePortfolio);
GetHelpCommand = new DelegateCommand<object>(GetHelp);
}
private void GetHelp(object obj_)
{
}
private void SavePortfolio(object obj_)
{
}
private void OpenPortfolio(object obj_)
{
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged()
{
throw new NotImplementedException();
}
public void RaisePropertyChanged(string propertyName)
{
throw new NotImplementedException();
}
public string this[string columnName]
{
get { throw new NotImplementedException(); }
}
public string Error { get; private set; }
public AsyncContext Async { get; private set; }
public XmlLanguage Language { get; private set; }
public string Key { get; set; }
}
Thanks!
Why does BulkToolShellViewModel have its own PropertyChanged event along with RaisePropertyChanged methods that do nothing? Shouldn't it inherit this functionality from BaseViewModel? Perhaps the UI is attaching to BulkToolShellViewModel.PropertyChanged rather than BaseViewModel.PropertyChanged and is never being notified of changes.