Click here to Skip to main content
15,916,188 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have View as below

<window x:class="TestWPFApplication.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:testns="clr-namespace:TestWPFApplication.ViewModel"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<window.datacontext>
<testns:testviewmodel x:name="_testViewModel" id="1" name="Abdul" xmlns:testns="#unknown">
</testns:testviewmodel>
</window.datacontext>
<grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<textbox horizontalalignment="Left" height="22" margin="160,66,0,0" textwrapping="Wrap">
Name="txtName" VerticalAlignment="Top" Width="120" Text="{Binding Name, Mode=TwoWay, ValidatesOnDataErrors=True}"
/>
<!--<Button Content="Save with One Parameter" HorizontalAlignment="Left" Margin="69,142,0,0" VerticalAlignment="Top" Width="159" Name="btnSaveSpecific"
Command="{Binding Path=SaveWithOneParamenter}"
CommandParameter="{Binding ElementName=txtName, Path=Text}"
RenderTransformOrigin="-1.855,0.516" />-->
<Button Content="Save with One Parameter" HorizontalAlignment="Left" Margin="69,142,0,0" VerticalAlignment="Top" Width="159" Name="btnSaveSpecific"
Command="{Binding Path=ClickCommandSaveWithOneParamenter}"
RenderTransformOrigin="-1.855,0.516" />
<Label Content="Enter Id" HorizontalAlignment="Left" Margin="58,17,0,0" VerticalAlignment="Top" Name="lblId"/>
<textbox text="{Binding Id, Mode=TwoWay, ValidatesOnDataErrors=True}" horizontalalignment="Left" height="22" margin="160,20,0,0" textwrapping="Wrap">
x:Name="txtId" VerticalAlignment="Top" Width="120" IsReadOnlyCaretVisible="True" />
<Button Content="Save with Tow Parameter" HorizontalAlignment="Left" Margin="253,142,0,0" VerticalAlignment="Top" Width="163" Name="btnSaveSpecific2"
Command="{Binding Path=ClickCommand}"
/>
<datagrid horizontalalignment="Left" margin="103,188,0,0" verticalalignment="Top" height="98" width="313">
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=PersonNames, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedItem, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
AutoGenerateColumns="False" >
<datagrid.columns>
<datagridtextcolumn header="Employee Id" binding="{Binding Path=Id, UpdateSourceTrigger=PropertyChanged,Mode =TwoWay}" />
<datagridtextcolumn header="Name of Employee" binding="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
<datagridtemplatecolumn header="Status" width="100">
<datagridtemplatecolumn.celltemplate>
<datatemplate>
<textblock text="{Binding SelectedStatus.StatusName}"></textblock>
</datatemplate>
</datagridtemplatecolumn.celltemplate>
<datagridtemplatecolumn.celleditingtemplate>
<datatemplate>
<combobox height="22">ItemsSource="{StaticResource ListOfStatuses}"
SelectedItem="{Binding SelectedStatus}"></combobox>
</datatemplate>
</datagridtemplatecolumn.celleditingtemplate>
</datagridtemplatecolumn>

</datagrid.columns>
</datagrid>
<Label Content="{Binding SelectedItem.Id}" Name="lblSelectedId" HorizontalAlignment="Left" Margin="130,294,0,0" VerticalAlignment="Top"/>
<Label Content="{Binding SelectedItem.Name}" Name="lblSelectedName" HorizontalAlignment="Left" Margin="271,294,0,0" VerticalAlignment="Top"/>
</textbox></textbox></grid>
</window>

Then I have ViewModel as below
public class TestViewModel : INotifyPropertyChanged, IDataErrorInfo
{
TestData _testData;
ObservableCollection<status> _listOfStatuses;


//public TestCommand TestCommand { get; set; }
public SaveWithOneParamenter SaveWithOneParamenter { get; set; }
private SavingItemDetailsViewModel _savingItemDetailsViewModel;

public TestViewModel()
{
_testData = new TestData();
_personNames = new ObservableCollection<testdata>();
//_dictionaryNames = new ObservableCollection<tuple><int,>>();

//this.TestCommand = new TestCommand(this);
this.SaveWithOneParamenter = new SaveWithOneParamenter(this);

_savingItemDetailsViewModel = new SavingItemDetailsViewModel();

_listOfStatuses = new ObservableCollection<status>();
_listOfStatuses.Add(new Status(1, "Active"));
_listOfStatuses.Add(new Status(2, "InActive"));
_listOfStatuses.Add(new Status(3, "Deleted"));

}

private int _id;
public int Id
{
get { return _id; }
set
{
_id = value; NotifyPropertyChanged("Id");
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;

NotifyPropertyChanged("Name");
}
}

private TestData TestData;
public TestData SelectedItem
{ get { return TestData; } set { TestData = value; NotifyPropertyChanged("SelectedItem"); } }

private Status _status;
public Status SelectedStatus
{ get { return _status; } set { _status = value; NotifyPropertyChanged("SelectedStatus"); } }

public ObservableCollection<status> ListOfStatuses
{
get { return _listOfStatuses; }
private set { _listOfStatuses = value; NotifyPropertyChanged("ListOfStatuses"); }
}



ObservableCollection<testdata> _personNames;
public ObservableCollection<testdata> PersonNames
{
get { return _personNames; }
set
{
_personNames = value; NotifyPropertyChanged("PersonNames");
}
}

ObservableCollection<tuple><int,>> _dictionaryNames;
ObservableCollection<tuple><int,>> DictionaryNames
{
get { return _dictionaryNames; }
set
{
_dictionaryNames = value; NotifyPropertyChanged("DictionaryNames");
}
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

private ICommand _clickCommand;
public ICommand ClickCommandSaveWithTwoParamenters
{
get
{
return _clickCommand ?? (_clickCommand = new SaveWithTwoParamenters(() => AddAnItem(), CanExecute));
}
}

public ICommand ClickCommandSaveWithOneParamenter
{
get
{
return _clickCommand ?? (_clickCommand = new SaveWithTwoParamenters(() => AddAnItem(Name), CanExecute));
}
}

public bool CanExecute { get { return string.IsNullOrEmpty(Error); } private set { } }
public void AddAnItem()
{
SavingItemDetails _savingItemDetails = new SavingItemDetails();
_savingItemDetails.DataContext = _savingItemDetailsViewModel;

_savingItemDetailsViewModel.SavingId = Id;
_savingItemDetailsViewModel.SavingName = _name;
_savingItemDetailsViewModel.SaveMassage = "Saving Id=" + Id.ToString() + ", Name=" + _name;

_savingItemDetails.ShowDialog();

_personNames.Add(new TestData(Id, _name));
}

public void AddAnItem(string _name)
{
SavingItemDetails _savingItemDetails = new SavingItemDetails();
_savingItemDetails.DataContext = _savingItemDetailsViewModel;

_savingItemDetailsViewModel.SavingId = Id;
_savingItemDetailsViewModel.SavingName = _name;
_savingItemDetailsViewModel.SaveMassage = "Saving Id=" + Id.ToString() + ", Name=" + _name;

_savingItemDetails.ShowDialog();

int t;

if ((_personNames == null) || (_personNames.Count() <= 0))
t = 1;
else
t = (_personNames.Count <= 0) ? 1 : _personNames.Max(x => x.Id) + 1;

_personNames.Add(new TestData(t, _name));

//int t;

//if ((_personNames == null) || (_personNames.Count() <= 0))
// t = 1;
//else
// t = (_personNames.Count <= 0) ? 1 : _personNames.Max(x => x.Id) + 1;

//SavingItemDetails _savingItemDetails = new SavingItemDetails();
//_savingItemDetails.DataContext = _savingItemDetailsViewModel;

//_savingItemDetailsViewModel.SavingId = t;
//_savingItemDetailsViewModel.SavingName = _name;
//_savingItemDetailsViewModel.SaveMassage = "Saving Id=" + t.ToString() + ", Name=" + _name;

//_savingItemDetails.ShowDialog();

//_personNames.Add(new TestData(t, _name));
}

public string Error
{
get;
private set;
}

public string this[string columnName]
{
get
{
if (columnName == "Id")
{
if (Id == 0)
Error = "Id Can not be 0";
}

if (columnName == "Name")
{
if (string.IsNullOrEmpty(Name))
Error = "Name can not be empty";
}

return Error;
}
}

}

Then I have Model as below
public class Status : INotifyPropertyChanged, IDataErrorInfo
{
private int _statusId;
private string _statusName;

public int StatusId { get { return _statusId; } set { _statusId = value; NotifyPropertyChanged("StatusId"); } }
public string StatusName { get { return _statusName; } set { _statusName = value; NotifyPropertyChanged("StatusName"); } }

public Status(int _statusId, string _statusName)
{
this.StatusId = _statusId;
this.StatusName = _statusName;
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}


public string Error
{
get;
private set;
}

public string this[string columnName]
{
get
{
if (columnName == "StatusId")
{
if (StatusId == 0)
Error = "StatusId Can not be 0";
}

if (columnName == "StatusName")
{
if (string.IsNullOrEmpty(StatusName))
Error = "StatusName can not be empty";
}

return Error;
}
}
}

The error is at the bold text that is: ItemsSource="{StaticResource ListOfStatuses}"

and it says "Item ListOfStatuses could not be resolved", even ListOfStatuses is there in the ViewModel. Can somebody please help me in this regards. Thanks in advance.
I tried by putting it within the Model that's being bound with the DataGrid that's TestData right, in that I tried putting, still not solved any help is much appreciated.

What I have tried:

I am trying my best by putting different combinations and searching as well
Posted
Updated 27-May-16 5:12am
v4

1 solution

Try to give a Name (WindowName for example) to your window and change
C#
ItemsSource="{StaticResource ListOfStatuses}"

to
C#
ItemsSource="{Binding ElementName=WindowName, Path=ListOfStatuses, Mode=OneWay}"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900