|
It looks like your view and view-model are in the same assembly. Try removing the assembly from the XML namespace declaration:
<Window x:Class="TestWPFApplication.MainWindow"
xmlns:testns="clr-namespace:TestWPFApplication"
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Rick,
I have removed it as below, it is compiling and running executing, but showing me that Invalid Markup on the UI screen, when I try to open the Properties settings, the Properties box is not showing me any Properties, why could that be?
Thank you very much my friend.
<Window x:Class="TestWPFApplication.MainWindow"<br />
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:testns="clr-namespace:TestWPFApplication"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources><br />
<testns:TestViewModel x:Key="_testViewModel" Name="Abdul">
</testns:TestViewModel>
</Window.Resources>
<Grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap"
Name="txtName" VerticalAlignment="Top" Width="120"/>
<pre>
<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
<Label Content="Enter Id" HorizontalAlignment="Left" Margin="58,17,0,0" VerticalAlignment="Top" Name="lblId"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,18,0,0" TextWrapping="Wrap"
Name="txtId" VerticalAlignment="Top" Width="120"/>
<Button Content="Save 2" HorizontalAlignment="Left" Margin="412,142,0,0" VerticalAlignment="Top" Width="74" 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}"
AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name of Employee" Binding="{Binding Path=Name}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
modified 16-May-16 12:34pm.
|
|
|
|
|
indian143 wrote: Hi Pete,
I am not Pete.
indian143 wrote: Invalid Markup on the UI screen
Does it give you an error message? Maybe something in the "Error List" or "Output" windows?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sorry and thank you for bringing it up, good catch
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Hi Rick,
It says that the
The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
But according to the code you posted earlier, it's not in the TestWPFApplication.ViewModel namespace; it's in the TestWPFApplication namespace.
It sounds like you've put:
xmlns:testns="clr-namespace:TestWPFApplication.ViewModel"
instead of:
xmlns:testns="clr-namespace:TestWPFApplication"
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Friends,
I have two Textboxes, One for Id and another for the Name, I need to pass both of these two into MyViewModel class Id and Name Properties in a Button Click, so far I am able to pass one of them to the ViewModel, how can I pass both the parameters to the ViewModel, any help is greatly helpful. Thanks in advance please.
So far I am able to do as below
in View
<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
In the Command class
public void Execute(object parameter)
{
var values = (string)parameter;
this.ViewModel.AddAnItem(values);
}
In View Model
public void AddAnItem(string _name)
{
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));
}
I want to change this AddAnItem to take two parameters int and string and if possible return value also, but return is not important, what is important is to take two parameters. Please need help, any sort of help a link or code snippet or even a suggestion also helps.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
modified 15-May-16 7:38am.
|
|
|
|
|
Why? Just put two string properties in your ViewModel and binding your Text boxes to them. You can then read them from your command.
This space for rent
|
|
|
|
|
The condition in the company is to bind them from xaml but when I am trying to bind the ViewModel from xaml, I am getting value only if I bind one but when I am trying to bind the second one and try to raise NotifyPropertyChanged event then both the values are not coming inth xaml.
Then I tried add static resource of ViewModel in xaml, the ViewModel has the same namespace and assembly as xaml view file, its not letting me add the static resource of the ViewModel.
Without ViewModel is not being accessed as StaticResource I am not able to reference the ViewModels Properties in the View, its giving me compile error saying that ViewModel is not available in the namespace. I tried it many ways not not helping me out, I wrote the code as above question.
Anyhelp please.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Without you showing your ViewModel and View here, it's impossible to say why it doesn't work, however I would take a guess that you aren't actually referencing the namespace in your XAML. Typically, I would expect to see something like this in your Window/UserControl header:
xmlns:vm="clr-namespace:MyApplication.ViewModelNamespace" Then you would reference this as such
<Window.DataContext><vm:NameOfViewModel></Window.Resources>
This space for rent
|
|
|
|
|
Hi,
My here is my View
<Window x:Class="TestWPFApplication.MainWindow"<br />
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:testns="clr-namespace:TestWPFApplication"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources><br />
<testns:TestViewModel x:Key="_testViewModel" Name="Abdul">
</testns:TestViewModel>
</Window.Resources>
<Grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap"
Name="txtName" VerticalAlignment="Top" Width="120"/>
<pre>
<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
<Label Content="Enter Id" HorizontalAlignment="Left" Margin="58,17,0,0" VerticalAlignment="Top" Name="lblId"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,18,0,0" TextWrapping="Wrap"
Name="txtId" VerticalAlignment="Top" Width="120"/>
<Button Content="Save 2" HorizontalAlignment="Left" Margin="412,142,0,0" VerticalAlignment="Top" Width="74" 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}"
AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name of Employee" Binding="{Binding Path=Name}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
And ViewModel
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace TestWPFApplication
{
public class TestViewModel : INotifyPropertyChanged
{
TestData _testData;
public TestSpecificCommand TestSpecificCommand { get; set; }
public TestViewModel()
{
_testData = new TestData();
_personNames = new ObservableCollection<TestData>();
this.TestSpecificCommand = new TestSpecificCommand(this);
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value; NotifyPropertyChanged("Name");
}
}
ObservableCollection<TestData> _personNames;
public ObservableCollection<TestData> PersonNames
{
get { return _personNames; }
set
{
_personNames = value; NotifyPropertyChanged("PersonNames");
}
}
ObservableCollection<Tuple<int, TestData>> _dictionaryNames;
ObservableCollection<Tuple<int, TestData>> DictionaryNames
{
get { return _dictionaryNames; }
set
{
_dictionaryNames = value; NotifyPropertyChanged("DictionaryNames");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{<br />
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private ICommand _clickCommand;
public ICommand ClickCommand
{
get
{
return _clickCommand ?? (_clickCommand = new TestSpecificCommand(() => AddAnItem(), _canExecute));
}
}
public bool _canExecute { get; set; }
private int _id;
public int Id
{
get { return _id; }
set
{
_id = value; NotifyPropertyChanged("Id");
}
}
public void AddAnItem()
{<br />
_personNames.Add(new TestData(Id, _name));
}
public void AddAnItem(string _name)
{
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));
}
}
}
When I am referencing it, it is saying that the TestViewModel is not present in the clr-namespace:TestWPFApplication namespace and saying the UI above that Invalid Markup, can you please let me know buddy what's wrong in it?
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
TestViewModel is not a resource, yet you are attempting to use it as though it is. Change your Window.Resources section to a Window.DataContext that looks like this
<Window.DataContext>
<testns:TestViewModel />
</Window.DataContext>
This space for rent
|
|
|
|
|
Hi,
I changed it as below, but still it is giving me the same error, any help please, still it is saying me that and in the UI it is saying invalid xaml.
Error 1 The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication
My code is below, any help please?
<Window x:Class="TestWPFApplication.MainWindow"<br />
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 Name="Abdul">
</testns:TestViewModel>
</Window.DataContext>
<Grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap"
Name="txtName" VerticalAlignment="Top" Width="120"/>
<pre>
<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
<DataGrid HorizontalAlignment="Left" Margin="103,188,0,0" VerticalAlignment="Top" Height="98" Width="313"
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=PersonNames, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name of Employee" Binding="{Binding Path=Name}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
Now I changed a little bit the ViewModel I put in a folder called ViewModel and Model in Model folder and changed their namespaces and changed the reference of clr-namespace as well but still the error persists.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
modified 16-May-16 20:53pm.
|
|
|
|
|
And that would be correct. Look at the code you posted. The ViewModel isn't in that namespace, it's in TestWPFApplication. The error was very explicit about what the problem was.
This space for rent
|
|
|
|
|
Thanks for all your help but I found the Problem that I was struggling with, it was because of the network drive I loading the Application path from. When we are using loading the static resources data context the UI can take them only if they from trusted source, my application was on network drive.
Thanks for all your support Pete and Rick, without you guys I couldn't have got this far. About my wrong code here I was changing the code all the time, may be pasted it when it was wrong. Thank you very much for that.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Glad you got there in the end.
This space for rent
|
|
|
|
|
Your binding mode should be OneWayToSource not OneWay ,
<TextBox Text="{Binding Name, Mode=OneWayToSource}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> Or TwoWay if you want your View and the property in your ViewModel to modify each other.
"As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
|
|
|
|
|
Hi All,
How can I add Dictionary into an ObservableCollection and when an Item is added to Dictionary then it will update the UI, I tried in all the ways but UI is not getting updated.
I even used ObservableCollection<tupple<int, object="">> to overcome of it, but still it is not updating the UI.
Can anybody please help me in that, any sort of help is appreciated. Thanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
You can't directly do this. The change notification has to come directly from the object that is changing so the dictionary has to raise the notification.
Have a search for an ObservableDictionary implementation instead. Here's one I found with a quick Google. ObservableDictionary<TKey, TValue> (C#) | Shimmy on .NET[^]
This space for rent
|
|
|
|
|
Try using this class from the Windows Samples for Parallel Programming with the .NET Framework[^]:
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;
namespace System.Collections.Concurrent
{
[DebuggerDisplay("Count={Count}")]
public class ObservableConcurrentDictionary<TKey, TValue> :
ICollection<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>,
INotifyCollectionChanged, INotifyPropertyChanged
{
private readonly SynchronizationContext _context;
private readonly ConcurrentDictionary<TKey, TValue> _dictionary;
public ObservableConcurrentDictionary()
{
_context = AsyncOperationManager.SynchronizationContext;
_dictionary = new ConcurrentDictionary<TKey, TValue>();
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyObserversOfChange()
{
var collectionHandler = CollectionChanged;
var propertyHandler = PropertyChanged;
if (collectionHandler != null || propertyHandler != null)
{
_context.Post(s =>
{
if (collectionHandler != null)
{
collectionHandler(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
if (propertyHandler != null)
{
propertyHandler(this, new PropertyChangedEventArgs("Count"));
propertyHandler(this, new PropertyChangedEventArgs("Keys"));
propertyHandler(this, new PropertyChangedEventArgs("Values"));
}
}, null);
}
}
private bool TryAddWithNotification(KeyValuePair<TKey, TValue> item)
{
return TryAddWithNotification(item.Key, item.Value);
}
private bool TryAddWithNotification(TKey key, TValue value)
{
bool result = _dictionary.TryAdd(key, value);
if (result) NotifyObserversOfChange();
return result;
}
private bool TryRemoveWithNotification(TKey key, out TValue value)
{
bool result = _dictionary.TryRemove(key, out value);
if (result) NotifyObserversOfChange();
return result;
}
private void UpdateWithNotification(TKey key, TValue value)
{
_dictionary[key] = value;
NotifyObserversOfChange();
}
#region ICollection<KeyValuePair<TKey,TValue>> Members
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
TryAddWithNotification(item);
}
void ICollection<KeyValuePair<TKey, TValue>>.Clear()
{
((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Clear();
NotifyObserversOfChange();
}
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Contains(item);
}
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).CopyTo(array, arrayIndex);
}
int ICollection<KeyValuePair<TKey, TValue>>.Count
{
get { return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Count; }
}
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
{
get { return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).IsReadOnly; }
}
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{
TValue temp;
return TryRemoveWithNotification(item.Key, out temp);
}
#endregion
#region IEnumerable<KeyValuePair<TKey,TValue>> Members
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator();
}
#endregion
#region IDictionary<TKey,TValue> Members
public void Add(TKey key, TValue value)
{
TryAddWithNotification(key, value);
}
public bool ContainsKey(TKey key)
{
return _dictionary.ContainsKey(key);
}
public ICollection<TKey> Keys
{
get { return _dictionary.Keys; }
}
public bool Remove(TKey key)
{
TValue temp;
return TryRemoveWithNotification(key, out temp);
}
public bool TryGetValue(TKey key, out TValue value)
{
return _dictionary.TryGetValue(key, out value);
}
public ICollection<TValue> Values
{
get { return _dictionary.Values; }
}
public TValue this[TKey key]
{
get { return _dictionary[key]; }
set { UpdateWithNotification(key, value); }
}
#endregion
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you very much.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Hi All,
I have a Datagrid in a Window container as below
<DataGrid HorizontalAlignment="Left" Margin="103,188,0,0" VerticalAlignment="Top" Height="98" Width="313"
ItemsSource="{Binding Path=DictionaryName, Mode=TwoWay}" ></DataGrid>
And Collection in the ViewModel as below
public Dictionary<short, String> DictionaryName
{
get { return _testData.DictionaryName; }
set { _testData.DictionaryName = value; NotifyPropertyChanged("DictionaryName"); }
}
public void AddAnItem()
{
int t = DictionaryName.Max(x => x.Key) + 1;
DictionaryName.Add(short.Parse(t.ToString()), t.ToString() + "Number");
}
When Button click calls the Command then it is adding an item into the Dictionary but that is not being added into the Datagrid eventhough I kept the Mode as TwoWay?
Can anybody please help me any sort of help may be a code snippet, a link or even a suggestion also helps me?
Please its very urgent and I am new to the WPF, thanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
The Dictionary<TKey, TValue> class doesn't raise an event when the collection is modified. WPF has no way to know that you've added an item.
You'll need to raise a PropertyChanged event for the collection to tell WPF that it has changed:
public void AddAnItem()
{
int t = DictionaryName.Keys.Max() + 1;
DictionaryName.Add((short)t, t + "Number");
NotifyPropertyChanged("DictionaryName");
}
NB: You can use an explicit cast to convert an int to a short ; there's no need to convert it to a string and then parse it back to a short .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I did that (I have added NotifyPropertyChanged method in the AddAnItem() method) still my Datagrid is not getting updated according to the collection change.
public void AddAnItem()
{
int t = DictionaryName.Keys.Max() + 1;
DictionaryName.Add((short)t, t + "Number");
NotifyPropertyChanged("DictionaryName");
}
My NotitfyProertyChanged method is written as below, do I have any error in there?
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
The value in the TextBox is getting added to the DictionaryName collection but is not getting displayed into the DataGrid, I tried all sorts of options, but didn't work.
<Window x:Class="TestWPFApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="using:TestWPFApplication">
<Grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<TextBox Text="{Binding Name, Mode=OneTime}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap" Name="txtName" VerticalAlignment="Top" Width="120"/>
<Button Content="Save" HorizontalAlignment="Left" Margin="180,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSave"
Command="{Binding Path=TestCommand}"/>
<Button Content="Save Specific" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
<DataGrid HorizontalAlignment="Left" Margin="103,188,0,0" VerticalAlignment="Top" Height="98" Width="313"
ItemsSource="{Binding Path=DictionaryName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="True" >
<pre>
</DataGrid>
</Grid>
Anybody please help me man, even suggestion just to execute this is also fine.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
modified 13-May-16 19:48pm.
|
|
|
|
|
I tried with ObservableCollection also, with ObservableCollection datagrid is filling up a row every time (means the collection is filling up) but the value is not showing up there, can anybody please help me any sort of help.
I have my code in View as below
<Window x:Class="TestWPFApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="using:TestWPFApplication">
<Grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap" Name="txtName" VerticalAlignment="Top" Width="120"/>
<Button Content="Save" HorizontalAlignment="Left" Margin="180,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSave"
Command="{Binding Path=TestCommand}"/>
<Button Content="Save Specific" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
<DataGrid HorizontalAlignment="Left" Margin="103,188,0,0" VerticalAlignment="Top" Height="98" Width="313"
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=DictionaryNames, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name of Employee" Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
<pre>
</Grid>
My ViewModel
public class TestViewModel : INotifyPropertyChanged
{
TestData _testData;
public TestCommand TestCommand { get; set; }
public TestSpecificCommand TestSpecificCommand { get; set; }
public TestViewModel()
{
_testData = new TestData();
this.TestCommand = new TestCommand(this);
this.TestSpecificCommand = new TestSpecificCommand(this);
_dictionaryNames = new ObservableCollection<String>();
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
ObservableCollection<String> _dictionaryNames;
public ObservableCollection<String> DictionaryNames
{
get { return _dictionaryNames; }
set { _dictionaryNames = value; NotifyPropertyChanged("DictionaryNames"); }
}
public Dictionary<short, String> DictionaryName
{
get { return _testData.DictionaryName; }
set { _testData.DictionaryName = value; NotifyPropertyChanged("DictionaryName"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public void AddAnItem()
{
int t ;
if ((DictionaryName == null) || (DictionaryName.Count()<=0))
t = 1;
else
t = DictionaryName.Max(x => x.Key) + 1;
DictionaryName.Add((short)t, t.ToString() + "Number");
NotifyPropertyChanged("DictionaryNames");
}
public void AddAnItem(string _name)
{
int t;
if ((DictionaryName == null) || (DictionaryName.Count() <= 0))
t = 1;
else
t = (DictionaryName.Count <= 0) ? 1 : DictionaryName.Max(x => x.Key) + 1;
DictionaryName.Add(short.Parse(t.ToString()), _name);
_dictionaryNames.Add(_name);
NotifyPropertyChanged("DictionaryNames");
}
}
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|