|
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."
|
|
|
|
|
Abdul, when adding or removing items from a collection, you have to raise an INotifyCollectionChanged event to notify the items that the number of rows has changed. This is baked into ObservableCollection.
This space for rent
|
|
|
|
|
[UPDATE]
I opened Snoop and dragged over my window. It popped up a message about "Snoop has noticed windows running in multiple dispatchers..."
As I was reading it my content appeared. IF I DON'T SNOOOP, IT BEHAVE AS BELOW.
.
.
.
.
I've got this tab control that is defined in XAML. It's an outer TabControl and it's tab items contain their own sub tabs:
I want to convert it to C# so I can load it at runtime.
<TabControl x:Name="mainTabControl"
ItemsSource="{Binding TabItems}"
SelectedItem="{Binding SelectedTabItem}"
BorderBrush="{DynamicResource MainTab.BorderBrush}"
Style="{DynamicResource MainTab.TabControl.Style}">
<pre>
<userControl:ToolBarTabItem x:Name="ScopeTabItem"
DataContext="{Binding ScopeViewModel}"
Header="{DynamicResource Maintab.Oscilloscope.Label}"
IsTabEnabled="{Binding Path=IsTabAvailable}"
TabType="Oscilloscope">
<TabControl Style="{DynamicResource InnerTabControl.Style}"
SelectedIndex="{Binding InnerTabSelectedIndex}"
ItemContainerStyle="{DynamicResource InnerTab.SelectedTabItem.Style}"<br />
ItemTemplate="{DynamicResource InnerTab.ViewPort.Header}"
ItemsSource="{Binding OscilloscopeViewPortItems}"
ContentTemplate="{StaticResource TabControlTemplate}"/>
</userControl:ToolBarTabItem>
I added the TabItems collection in the VM and bound it to the outter TabControl(above), then tried the following. The outter tab shows up OK, but the inner
private void LoadTabs()
{
TabItems = new ObservableCollection<ToolBarTabItem>();
var scopeTab = AddTab();
TabItems.Add(scopeTab);
}
private ToolBarTabItem AddTab()
{
ToolBarTabItem tab = new ToolBarTabItem();
tab.DataContext = ScopeViewModel;
tab.Header = GetStringFromResource("Maintab.Oscilloscope.Label");
tab.TabType = Tabs.Oscilloscope;
Binding isTabAvailableBinding = new Binding();
isTabAvailableBinding.Path = new PropertyPath("IsTabAvailable");
isTabAvailableBinding.Source = ScopeViewModel;
BindingOperations.SetBinding(tab, ToolBarTabItem.IsTabEnabledProperty, isTabAvailableBinding);
Style tabControlStyle = Application.Current.FindResource("InnerTabControl.Style") as Style;
Style itemContainerStyle = Application.Current.FindResource("InnerTab.SelectedTabItem.Style") as Style;
DataTemplate contentTemplate = Application.Current.FindResource("TabControlTemplate") as DataTemplate;
DataTemplate itemTemplate = Application.Current.FindResource("InnerTab.ViewPort.Header") as DataTemplate;
TabControl tabControl = new TabControl();
tabControl.Style = tabControlStyle;
tabControl.ItemContainerStyle = itemContainerStyle;
tabControl.ItemTemplate = itemTemplate;
tabControl.ContentTemplate = contentTemplate;
Binding selectedIndexBinding = new Binding();
selectedIndexBinding.Path = new PropertyPath("InnerTabSelectedIndex");
selectedIndexBinding.Source = ScopeViewModel;
BindingOperations.SetBinding(tab, TabControl.SelectedIndexProperty, selectedIndexBinding);
Binding itemSourceBinding = new Binding();
itemSourceBinding.Path = new PropertyPath("OscilloscopeViewPortItems");
itemSourceBinding.Source = ScopeViewModel;
BindingOperations.SetBinding(tab, TabControl.ItemsSourceProperty, itemSourceBinding);
tab.Content = tabControl;
return tab;
}
The resources all seem to be found OK, but I don't see the inner tab. I'm wondering if my bindings are correct. Anyone see anything wrong?
Thanks
If it's not broken, fix it until it is
modified 12-May-16 19:59pm.
|
|
|
|
|
Hi,
I have a DataGrid in WPF, how can I add a check box and need code to delete multiple selected rows from the DataGrid?
Any help a link or code snippet helps me a lot.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Add a bool property to your collection - bind this to the checkbox column in the datagrid.
Add a delete button to the view
when the user clicks the button delete each item from the collection that has been checked.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Is there anyway I can bind this DataGrid to a method in the ViewModel directly?
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
I have icons defined as Paths:
<!--SCOPE ICON-->
<Geometry x:Key="data1">M98.219,48.111C97...</Geometry>
<Geometry x:Key="data2">M98.219,46.948C97...</Geometry>
<UserControl x:Key="scopeIcon">
<Path>
<Path.Data>
<GeometryGroup>
<StaticResource ResourceKey="data1"/>
<StaticResource ResourceKey="data2"/>
</GeometryGroup>
</Path.Data>
</Path>
</UserControl
What I want to do now is use them in a Button control. I have subclassed Button into ImageButton:
public class MenuButton : Button
{
public string Caption
{
get { return (string)GetValue(CaptionProperty); }
set { SetValue(CaptionProperty, value); }
}
public static readonly DependencyProperty CaptionProperty =
DependencyProperty.Register("Caption", typeof(string), typeof(MenuButton), new UIPropertyMetadata(null));
}
and the style
<Style TargetType="Button"
x:Key="TestButtonStyle">
<pre>
<Setter Property="Height" Value="140"/>
<Setter Property="Width" Value="195"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:MenuButton}">
<Border >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50*"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
PATH(ICON) GOES HERE <=============
<TextBlock Grid.Row="1"
Grid.Column="0"Text="{TemplateBinding Caption}"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="5"
Foreground="White"
FontSize="14"
TextAlignment="Center"
TextWrapping="WrapWithOverflow"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
and it's used this way:
<controls:MenuButton Caption="{Binding Caption}"
Margin="2"
Width="100"
Style="{StaticResource TestButtonStyle}"
VerticalAlignment="Top"
Command="{Binding Path=ButtonClick}"
CommandParameter="{x:Static enums:Tabs.Oscilloscope}"/>
The question is, how in the button above to I set the path icon. It will be different for each use case. I want to set it up as part of the ControlTemplate then change it each time I add a new Menu Button.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|