WPF EventTrigger根据父控件的视图模型更改中的属性值修改控件的属性值

时间:2017-06-07 23:10:01

标签: c# wpf triggers styles prism

我在Button内托管了UserControl我想在视图中保留属性的数值时更改BackgroundForeground属性UserControl更改的模型。

我的视图模型显示如下(为简洁起见,删除了属性和方法)。我正在使用Prism的DelegateCommand<T>作为我的命令。

        public class PianoKeyViewModel : BindingTarget, IPianoKey
        {
            private byte afterTouch;
            private DelegateCommand<AfterTouchEventArgs> afterTouchChangedCommand;

            public byte AfterTouch
            {
                get
                {
                    return afterTouch;
                }
                set
                {
    //Calls a method on BindingTarget to assign to afterTouch and call RaisePropertyChanged.
                    SetProperty(ref afterTouch, value);
                }
            }

            public DelegateCommand<AfterTouchEventArgs> AfterTouchChangedCommand
            {
                get
                {
                    return afterTouchChangedCommand;
                }
                set
                {
    //Calls a method on BindingTarget to assign to afterTouchChangedCommand and call RaisePropertyChanged.
                    SetProperty(ref afterTouchChangedCommand, value);
                }
            }

            protected override void RaisePropertyChanged([CallerMemberName]string propertyName = null)
            {
                base.RaisePropertyChanged(propertyName);

                switch (propertyName)
                {
                    case nameof(AfterTouch):
                        AfterTouchEventArgs afterTouchEventArgs = new AfterTouchEventArgs(AfterTouch);

                        if (AfterTouchChangedCommand != null && AfterTouchChangedCommand.CanExecute(afterTouchEventArgs))
                            //Executes the AfterTouchChangedCommand since the AfterTouchProperty has just changed and the command exists and can execute.
        AfterTouchChangedCommand.Execute(afterTouchEventArgs);

                        break;
                }
            }
        }

UserControl的XAML大幅缩减版本如下。请注意我的TODO注释下面的行 - AfterTouchChanged不存在,需要替换为绑定到AfterTouchChangedCommand,当调用Execute时触发触发器。此外,此DataContext的{​​{1}}将是上面显示的视图模型,这是通过使用Prism的模块实现的。

UserControl

我需要更换<UserControl x:Class="MyNamespace.PianoKey.View" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:converters="clr-namespace:MyNamespace.PianoKey.View.Converters" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <UserControl.Resources> <converters:AfterTouchToBackgroundColorConverter x:Key="afterTouchToBackgroundColorConverter"/> <converters:AfterTouchToForegroundColorConverter x:Key="afterTouchToForegroundColorConverter"/> </UserControl.Resources> <UserControl.Template> <Button> <Button.Triggers> <!--TODO: Find out what trigger is needed here to respond to AfterTouchChangedCommand.Execute.--> <EventTrigger RoutedEvent="AfterTouchChanged"> <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=AfterTouch, Converter={StaticResource afterTouchToBackgroundColorConverter}}"/> <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=AfterTouch, Converter={StaticResource afterTouchToForegroundColorConverter}}"/> </EventTrigger> </Button.Triggers> </Button> </UserControl.Template> </UserControl> 以获取更新EventTrigger的{​​{1}}和Background颜色? Foreground看起来很有希望,但据我所知,这只能响应绑定源上属性的单个值。

0 个答案:

没有答案