|
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
|
|
|
|
|
|
How can I handle the user clicking the icon in a Window's title bar?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
AFAIK the Title bar isn't part of the area of an application that exposes events like click. I believe you will have to create some sort of custom title bar.
I found this here on CP - WPF Custom Chrome Library[^]
|
|
|
|
|
|
|
One and a half years for your first question and this is it. Could you expand your question, just a little bit? Maybe you could identify what GAF is (other than a rather crude acronym). Then, maybe you could tell us what you are trying to achieve.
This space for rent
|
|
|
|
|
What is GAF?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I want to create some controls - a Host and a Content control. There will be one Host and many Content controls. Other developers will create Content controls and use them in the Host. For example I have a Tab control. On each tab will be a host, but each tab's Content control will be different:
MainWindow
<Window x:Class="MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:Views"
Title="Demo"
WindowStartupLocation="CenterScreen"
Height="800"
Width="1000">
<pre>
<Grid>
<TabControl Margin="5">
<TabItem Header="Tab 1">
<views:HostView HostContent="ContentControl_1.xaml"/>
</TabItem>
<TabItem Header="Tab 2">
<views:HostView HostContent="ContentControl_2.xaml"/>
</TabItem>
<TabItem Header="Tab 3">
<views:HostView HostContent="ContentControl_3.xaml"/>
</TabItem>
</TabControl>
</Grid>
I'm thinking that the Host control will have a ContentPresenter bound to the HostContent DP. The question is how to create the DP HostContent so that it resolves at design time?
Thanks
If it's not broken, fix it until it is
modified 3-May-16 11:38am.
|
|
|
|
|
I'm trying to use a Digital font I downloaded as a resource:
<Window x:Class="KClock.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:KClock"
WindowStyle="None"
MouseDown="Window_MouseDown"
AllowsTransparency="True"
Background="Black"
BorderBrush="Transparent"
Height="300"
Width="500">
<pre>
<Grid>
<TextBlock Text="4:40:25 pm"
Foreground="Lime"
FontSize="34">
<TextBlock.Style>
<Style>
<Setter Property="TextBlock.FontSize" Value="36" />
<Setter Property="TextElement.FontFamily" Value="Resources/DSEG14Modern-Bold" />
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
The font is in the resource, but it's unchanged in the TextBlock. What's wrong here??
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
You need to specify the font family name, separated from the folder name with a # :
The folder and font family name are delimited by a # character. The folder reference may be absolute, or relative. For example, "Custom Fonts\#My Custom Font".
<TextBlock FontFamily="./resources/#Pericles Light">
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Ok, so now I have
<TextBlock Grid.Row="2"
Text="1234"
Foreground="Lime"
FontSize="40"
FontFamily="./resources/#DSEG14Modern-Bold"/>
No change
If it's not broken, fix it until it is
|
|
|
|
|
Also tried
<TextBlock Grid.Row="2"
Text="1234"
Foreground="Lime"
FontSize="40"
FontFamily="pack://application:,,,/KClock;Component/Resources/#DSEG14Modern-Bold"/>
No change
If it's not broken, fix it until it is
|
|
|
|
|