2011-11-18 116 views
0

我有以下ViewModel,我想綁定HotkeysForeground以更改列表框中的顏色。將屬性綁定到更改列表框項目前景

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<string> _hotkeys; 

     private Brush _foregroundColor; 

     private string _title; 

     public MainWindowViewModel() 
     { 
      _hotkeys = new ObservableCollection<string>(); 

      _hotkeyManager = new HotkeyManager(); 
      _hotkeyManager.NewHotkey += HotkeyManager_NewHotkey; 

      _focusManager = new FocusManager(); 
      _focusManager.Focus += FocusManager_Focus; 
     } 

     public Brush HotkeysForeground 
     { 
      get 
      { 
       return _foregroundColor; 
      } 
      set 
      { 
       _foregroundColor = value; 

       OnPropertyChanged(() => Title); 
      } 
     } 

     public ReadOnlyObservableCollection<string> Hotkeys 
     { 
      get 
      { 
       return new ReadOnlyObservableCollection<string>(_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 = Brushes.Blue; 

      _hotkeys.Clear(); 

      foreach (var hotkey in _hotkeyManager.GetHotkeys()) 
      { 
       _hotkeys.Add(hotkey); 
      } 
     } 
    } 
} 

我想每一次「HotkeyManager_NewHotkey」被觸發時更改項目的前景顏色列表框,以某種原因,我不能似乎將其綁定到視圖,我試着多的東西,使它的工作無濟於事。

這是我的觀點。

<Window x:Class="Monkey.View.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="{Binding Mode=OneWay, Path=Title, UpdateSourceTrigger=PropertyChanged}" Height="200" Width="200" ShowInTaskbar="False" WindowStyle="ToolWindow" Topmost="True" ResizeMode="CanResizeWithGrip" AllowsTransparency="False"> 
    <ListBox 
     Canvas.Left="110" 
     Canvas.Top="74" 
     Name="HotkeyList" 
     Height="Auto" Width="Auto" HorizontalContentAlignment="Left" 
     BorderThickness="0" 
     ScrollViewer.CanContentScroll="False" 
     ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
     ScrollViewer.VerticalScrollBarVisibility="Disabled" 
     ItemsSource="{Binding Path=Hotkeys}" VerticalAlignment="Stretch" VerticalContentAlignment="Center" FontSize="20"> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="IsEnabled" Value="False" /> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 
</Window> 

我是相當新的WPF,並沒有真正探索綁定到深度,所以任何幫助表示讚賞。

回答

0

對於初學者,您將通知TitleHotkeysForeground屬性更改。但是,情況可能並非如此。

如果固定沒有幫助,這相當lenghty方式應爲你工作:

  • 變化HotkeysForeground物業類型string(只存儲顏色名稱)
  • 在XAML中創建靜態資源刷,綁定它
  • 覆蓋列表框項目模板顏色名稱的東西很簡單(如Label)並結合其前景前面提到bursh

所以,應用這些變化:

public string HotkeysForeground 
{ 
    get { return _foregroundColor; } 
    set 
    { 
     _foregroundColor = value; 
     // I assume this is some smart workaround to INPC... 
     OnPropertyChanged(() => HotkeysForeground); 
    } 
} 

現在,在XAML你必須這樣做:

<!-- need to import System namespace --> 
<Window x:Class="Monkey.View.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
<Window.Resources> 
    <SolidColorBrush Color="{Binding HotkeysForeground}" x:Key="HotkeysBrush"/> 
</Window.Resources> 
    <ListBox ItemsSource="{Binding Path=Hotkeys}"> 
     <ListBox.ItemTemplate> 
      <! 
      <DataTemplate DataType="{x:Type sys:String}"> 
       <Label Content="{Binding}" 
         Foreground="{StaticResource HotkeysBrush}"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate>    
    </ListBox> 
</Window> 
+0

謝謝!我忘了更改「HotkeysForeground」屬性的INPC。 ;) – LavaSeven

相關問題