【问题标题】:Binding a property to the change the listbox items foreground将属性绑定到更改列表框项目的前景
【发布时间】:2011-11-18 18:33:48
【问题描述】:

我有以下 ViewModel,我想绑定 HotkeysForeground 以更改 ListBox 中的颜色。

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”时,我都想更改 ListBox 中项目的前景色,由于某种原因,我似乎无法将其绑定到视图,我尝试了多种方法使其无法正常工作有用。

这是我的观点。

<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 还很陌生,还没有真正深入研究过绑定,因此感谢您提供任何帮助。

【问题讨论】:

    标签: wpf binding listbox


    【解决方案1】:

    首先,您将通知Title 中的HotkeysForeground 属性更改。但是,情况可能并非如此。

    如果修复没有帮助,这种相当冗长的方法应该适合你:

    • 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>
    

    【讨论】:

    • 谢谢!我忘记更改“HotkeysForeground”属性的 INPC。 ;)
    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2010-12-31
    相关资源
    最近更新 更多