Binding a property to change the listbox items foreground individually for each item?

Sounds like bad design to me. If you have more data than a hotkey string just create a class and store the color together with the display string as separate properties.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm trying to change the foreground color of the items in the ListBox individually for each item, I've already posted a similar question but this one is more concrete. I hoped that the state of the color is reserved for each item added to the ListBox, so I tried to create a property (in this case "HotkeysForeground"), bind it and change the color when the items are added in the "HotkeyManager_NewHotkey" event, the problem it's changing the foreground color for all the items in the ListBox. How can I do that per item?

Here is the ViewModel I use. Namespace Monkey.Core. ViewModel { using System; using System.Collections.

ObjectModel; using System.Windows. Media; using Monkey.Core.SystemMonitor. Accessibility; using Monkey.Core.SystemMonitor.

Input; public class MainWindowViewModel : WorkspaceViewModel { private readonly FocusManager _focusManager; private readonly HotkeyManager _hotkeyManager; private readonly ObservableCollection _hotkeys; private Color _foregroundColor; private string _title; public MainWindowViewModel() { _hotkeys = new ObservableCollection(); _hotkeyManager = new HotkeyManager(); _hotkeyManager. NewHotkey += HotkeyManager_NewHotkey; _focusManager = new FocusManager(); _focusManager. Focus += FocusManager_Focus; } public Color HotkeysForeground { get { return _foregroundColor; } set { _foregroundColor = value; OnPropertyChanged(() => HotkeysForeground); } } public ReadOnlyObservableCollection Hotkeys { get { return new ReadOnlyObservableCollection(_hotkeys); } } public string Title { get { return _title; } set { _title = value; OnPropertyChanged(() => Title); } } protected override void OnDispose() { base.OnDispose(); _hotkeyManager.Dispose(); _focusManager.Dispose(); } private void FocusManager_Focus(object sender, FocusManagerEventArgs e) { Title = e.

Title; } private void HotkeyManager_NewHotkey(object sender, EventArgs eventArgs) { HotkeysForeground = _hotkeys. Count Thank you in advance. C# wpf data-binding mvvm listbox link|improve this question edited Dec 27 '11 at 8:08Cameron Skinner12.2k1120 asked Nov 18 '11 at 21:30LavaSeven497 83% accept rate.

Try making your Foreground binding a "OneTime" binding. – mydogisbox Nov 18 '11 at 21:39 Nop, doing that results a constant blue items. – LavaSeven Nov 18 '11 at 21:57.

Sounds like bad design to me. If you have more data than a hotkey string just create a class and store the color together with the display string as separate properties. Edit: Example: public class HotkeyViewModel { private readonly string _DisplayString; public string DisplayString { get { return _DisplayString; } } private readonly Color _Color; public Color Color { get { return _Color; } } public HotkeyViewModel(string displayString, Color color) { _DisplayString = displayString; _Color = color; } } You can also make the properties editable, but if any bindings should update you need to implement INPC.

The new collection should then be of type ObservableCollection and the template has two bindings: (You could also make the property a Brush and bind directly to Background).

P – LavaSeven Nov 19 '11 at 0:57 @Eyal-Shilony: Actually that is a bad idea. I meant that you create a new class which has two properties, don't move i'll edit my answer. – H.B. Nov 19 '11 at 1:36 @Eyal-Shilony: Added example.

– H.B. Nov 19 '11 at 1:46 @Eyal-Shilony: Yes, you should not have any UI-elements or UI-related classes in the model or even viewmodel. If you have more than one property a new ViewModel is justified. – H.B. Nov 19 '11 at 1:47 @Eyal-Shilony: Well, it's not quite a proper ViewModel, the model is somewhat missing, if you have one you can plug that in of course.

– H.B. Nov 19 '11 at 2:03.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions