在代码隐藏中处理事件,以及对某些事件调用视图模型的命令



以下是概述:

我使用自定义控件(CusCtrl)来显示任务栏图标,它还有一个弹出属性。因此,当您单击图标时,CusCtrl将显示弹出菜单。

我正在用UserControl设置弹出窗口的子项(比方说UC1)。

我正在用ViewModel设置CusCtrl的DataContext,因此即使UC1也会与相应的ViewModel绑定(比方说VM1

现在UC1有了一些元素-标签,点击标签时我需要两件事:

  1. 在视图模型VM1上调用命令-

    通过该命令,我需要将视图模型的一些属性作为参数传递,并打开一些窗口UI。

  2. 关闭弹出窗口-

    为此,我考虑在UserControl&然后激发路由事件(FirePopUpClose-该事件在UserControl UC1中定义),该事件将由应用程序&然后从内部处理程序,将调用Custom Conntrol的ClosePopUp方法。

我知道如何使用Interactivity dll调用标签上MouseUp事件的命令,但我如何引发FirePopUpClose路由事件?

或者如何在标签上应用MouseUp事件处理程序并将命令绑定到该标签?

我是否认为这是正确的方式,或者有更好、更干净的方式来执行一些UI操作,并通过坚持MVVM来关闭弹出窗口?

下一个解决方案是什么;尝试使用Popup.IsOpen属性(这里是有关的信息,这里是用法#867–Controlling a Popup is Open Using Data Binding的示例)。直接或通过CusCtrl用户控件的DependencyProperty将其绑定到UC1 DataContext(您必须在封装Popup的控件中创建该属性)。仅此而已,通过这种方式,您将能够在没有事件的情况下管理弹出窗口的打开或关闭。

更新尝试下一个:1.主Xaml:

<Window x:Class="PopupIsOpenDataBindingHelpAttempt.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:popupIsOpenDataBindingHelpAttempt="clr-namespace:PopupIsOpenDataBindingHelpAttempt"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <popupIsOpenDataBindingHelpAttempt:DemoMainViewModel/>
</Window.DataContext>
<Window.Resources>
    <DataTemplate x:Key="PopupInnerControlDataTemplateKey" DataType="{x:Type popupIsOpenDataBindingHelpAttempt:TaskBarDemoViewModel}">
        <Grid Width="150" Height="85">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="{Binding TextString}"
                       Margin="5" Foreground="Red"
                       Width="150" TextWrapping="WrapWithOverflow"
                       VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
            <Button    Grid.Row="1" Content="Press to close" Command="{Binding PopupInnerButtonCommand}"
                    VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
        </Grid>
    </DataTemplate>
    <Image x:Key="ImageControl" Source="Pic/2015_10_16_Bing_en-US.jpg" IsHitTestVisible="False"/>
</Window.Resources>
<Grid Width="75" Height="75" HorizontalAlignment="Center" VerticalAlignment="Center">
    <popupIsOpenDataBindingHelpAttempt:TaskBarIconProjectDemo 
        ButtonContentProperty="{StaticResource ImageControl}"
        ButtonCommandProperty="{Binding ShowPopupCommand}"
        PopupIsOpenProperty="{Binding IsPopupOpen, UpdateSourceTrigger=PropertyChanged}"
        PopupInnerContentControlDataContext="{Binding TaskBarDemoViewModel, UpdateSourceTrigger=PropertyChanged}"
        PopupInnerContentControlContentTemplate="{StaticResource PopupInnerControlDataTemplateKey}"/>
</Grid>

2.封装XAMl:的弹出窗口

<UserControl x:Class="PopupIsOpenDataBindingHelpAttempt.TaskBarIconProjectDemo"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" x:Name="This">
<Grid>
    <Button Style="{StaticResource SpecialButtonStyle}" Command="{Binding ElementName=This, Path=ButtonCommandProperty}" 
            Content="{Binding ElementName=This, Path=ButtonContentProperty}"></Button>
    <Popup IsOpen="{Binding ElementName=This, Path=PopupIsOpenProperty}">
        <ContentControl Content="{Binding ElementName=This, Path=PopupInnerContentControlDataContext}" 
                        ContentTemplate="{Binding ElementName=This, Path=PopupInnerContentControlContentTemplate}"/>
    </Popup>
</Grid>

3.弹出封装控制代码(依赖属性):

public partial class TaskBarIconProjectDemo : UserControl
{
    public static readonly DependencyProperty ButtonCommandPropertyProperty = DependencyProperty.Register("ButtonCommandProperty", typeof (ICommand), typeof (TaskBarIconProjectDemo), new PropertyMetadata(default(ICommand)));
    public static readonly DependencyProperty PopupIsOpenPropertyProperty = DependencyProperty.Register("PopupIsOpenProperty", typeof (bool), typeof (TaskBarIconProjectDemo), new PropertyMetadata(default(bool)));
    public static readonly DependencyProperty PopupInnerContentControlDataContextProperty = DependencyProperty.Register("PopupInnerContentControlDataContext", typeof (object), typeof (TaskBarIconProjectDemo), new PropertyMetadata(default(object)));
    public static readonly DependencyProperty PopupInnerContentControlContentTemplateProperty = DependencyProperty.Register("PopupInnerContentControlContentTemplate", typeof (DataTemplate), typeof (TaskBarIconProjectDemo), new PropertyMetadata(default(DataTemplate)));
    public static readonly DependencyProperty ButtonContentPropertyProperty = DependencyProperty.Register("ButtonContentProperty", typeof (object), typeof (TaskBarIconProjectDemo), new PropertyMetadata(default(object)));
    public TaskBarIconProjectDemo()
    {
        InitializeComponent();
    }
    public ICommand ButtonCommandProperty
    {
        get { return (ICommand) GetValue(ButtonCommandPropertyProperty); }
        set { SetValue(ButtonCommandPropertyProperty, value); }
    }
    public bool PopupIsOpenProperty
    {
        get { return (bool) GetValue(PopupIsOpenPropertyProperty); }
        set { SetValue(PopupIsOpenPropertyProperty, value); }
    }
    public object PopupInnerContentControlDataContext
    {
        get { return (object) GetValue(PopupInnerContentControlDataContextProperty); }
        set { SetValue(PopupInnerContentControlDataContextProperty, value); }
    }
    public DataTemplate PopupInnerContentControlContentTemplate
    {
        get { return (DataTemplate) GetValue(PopupInnerContentControlContentTemplateProperty); }
        set { SetValue(PopupInnerContentControlContentTemplateProperty, value); }
    }
    public object ButtonContentProperty
    {
        get { return (object) GetValue(ButtonContentPropertyProperty); }
        set { SetValue(ButtonContentPropertyProperty, value); }
    }
}

4.视图模型:

    public class DemoMainViewModel:BaseObservableObject
{
    private bool _isOpen;
    private TaskBarDemoViewModel _taskBarDemoViewModel;
    private ICommand _showPopupCommnad;
    public DemoMainViewModel()
    {
        TaskBarDemoViewModel = new TaskBarDemoViewModel(ClosePopup, "Here you can put your content. Go for it...");
    }
    private void ClosePopup()
    {
        IsPopupOpen = false;
    }
    public bool IsPopupOpen
    {
        get { return _isOpen; }
        set
        {
            _isOpen = value;
            OnPropertyChanged();
        }
    }
    public TaskBarDemoViewModel TaskBarDemoViewModel    
    {
        get { return _taskBarDemoViewModel; }
        set
        {
            _taskBarDemoViewModel = value;
            OnPropertyChanged();
        }
    }
    public ICommand ShowPopupCommand
    {
        get { return _showPopupCommnad ?? (_showPopupCommnad = new RelayCommand(ShowPopup)); }
    }
    private void ShowPopup()
    {
        IsPopupOpen = true;
    }
}
public class TaskBarDemoViewModel:BaseObservableObject
{
    private readonly Action _closePopupCommand;
    private ICommand _command;
    private string _textString;
    public TaskBarDemoViewModel(Action closePopupCommand, string content)
    {
        _closePopupCommand = closePopupCommand;
        TextString = content;
    }
    public ICommand PopupInnerButtonCommand
    {
        get { return _command ?? (_command = new RelayCommand(TargetMethod)); }
    }
    private void TargetMethod()
    {
        //add your logic here
        if(_closePopupCommand == null) return;
        _closePopupCommand();
    }
    public string TextString
    {
        get { return _textString; }
        set
        {
            _textString = value;
            OnPropertyChanged();
        }
    }
}

5.按钮样式(根据需要更改):

    <Color x:Key="ButtonLowerPartKey">#FFD5E0EE</Color>
    <Color x:Key="ButtonUpperPartKey">#FFEAF1F8</Color>
    <Color x:Key="PressedColorButtonLowerPartKey">#FFF4C661</Color>
    <Color x:Key="PressedButtonUpperPartKey">#FFF4CC87</Color>
    <Color x:Key="HooveredButtonLowerPartKey">#FFFFD06D</Color>
    <Color x:Key="HooveredButtonUpperPartKey">#FFFFF0DF</Color>
    <Style x:Key="SpecialButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Padding" Value="5">
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid x:Name="Grid">
                        <Ellipse x:Name="ButtonControlBorder" Stroke="{TemplateBinding BorderBrush}" 
                                 StrokeThickness="{TemplateBinding BorderThickness}" 
                                 Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                            <Ellipse.Fill>
                                <LinearGradientBrush x:Name="BrushKey" MappingMode="RelativeToBoundingBox" SpreadMethod="Repeat" StartPoint="0.5,0" EndPoint="0.5,1">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStop Offset="0.5" Color="{StaticResource ButtonUpperPartKey}" />
                                        <GradientStop Offset="0.5" Color="{StaticResource ButtonUpperPartKey}" />
                                        <GradientStop Offset="0.5" Color="{StaticResource ButtonLowerPartKey}" />
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <Ellipse x:Name="Pressed" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Opacity="0">
                            <Ellipse.Fill>
                                <LinearGradientBrush x:Name="PressedBrushKey" MappingMode="RelativeToBoundingBox" SpreadMethod="Repeat" StartPoint="0.5,0" EndPoint="0.5,1">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStop Offset="0.5" Color="{StaticResource PressedButtonUpperPartKey}" />
                                        <GradientStop Offset="0.5" Color="{StaticResource PressedButtonUpperPartKey}" />
                                        <GradientStop Offset="0.5" Color="{StaticResource PressedColorButtonLowerPartKey}" />
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <Ellipse x:Name="InnerPressed" 
                                Width="{Binding ElementName=Pressed, Path=Width}" Height="{Binding ElementName=Pressed, Path=Height}" 
                                Stroke="DarkOrange" Opacity="0" StrokeThickness="1" SnapsToDevicePixels="True" Fill="Transparent"/>
                        <ContentPresenter Content="{TemplateBinding Button.Content}" HorizontalAlignment="Center" VerticalAlignment="Center">
                            <ContentPresenter.OpacityMask>
                                <VisualBrush Visual="{Binding ElementName=ButtonControlBorder}" />
                            </ContentPresenter.OpacityMask>
                        </ContentPresenter>
                        <Grid.Triggers>
                            <EventTrigger RoutedEvent="Mouse.MouseEnter">
                                <BeginStoryboard x:Name="MouseEnterStoryboard">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="BrushKey" Storyboard.TargetProperty="GradientStops[0].Color" From="{StaticResource ButtonUpperPartKey}" To="{StaticResource HooveredButtonUpperPartKey}" Duration="0:0:0.3" AutoReverse="False" />
                                        <ColorAnimation Storyboard.TargetName="BrushKey" Storyboard.TargetProperty="GradientStops[2].Color" From="{StaticResource ButtonLowerPartKey}" To="{StaticResource HooveredButtonLowerPartKey}" Duration="0:0:0.3" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                            <EventTrigger RoutedEvent="Mouse.MouseLeave">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="BrushKey" Storyboard.TargetProperty="GradientStops[0].Color" From="{StaticResource HooveredButtonUpperPartKey}" To="{StaticResource ButtonUpperPartKey}" Duration="0:0:1" AutoReverse="False" />
                                        <ColorAnimation Storyboard.TargetName="BrushKey" Storyboard.TargetProperty="GradientStops[2].Color" From="{StaticResource HooveredButtonLowerPartKey}" To="{StaticResource ButtonLowerPartKey}" Duration="0:0:1" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </Grid.Triggers>
                    </Grid>
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="MouseUpTimeLine">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Pressed" Storyboard.TargetProperty="Opacity">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="MouseDownTimeLine">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Pressed" Storyboard.TargetProperty="Opacity">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.05" Value="0.8" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="InnerPressedMouseUpTimeLine">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="InnerPressed" Storyboard.TargetProperty="Opacity">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="InnerPressedMouseDownTimeLine">
                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="InnerPressed" Storyboard.TargetProperty="Opacity">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.05" Value="1" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" SourceName="Grid" Value="True">
                            <Setter Property="Stroke" TargetName="ButtonControlBorder">
                                <Setter.Value>
                                    <SolidColorBrush Color="{StaticResource HooveredButtonLowerPartKey}">
                                    </SolidColorBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="ButtonBase.IsPressed" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource MouseDownTimeLine}" />
                                <BeginStoryboard Storyboard="{StaticResource InnerPressedMouseDownTimeLine}">
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource MouseUpTimeLine}" />
                                <BeginStoryboard Storyboard="{StaticResource InnerPressedMouseUpTimeLine}">
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
  1. BaseObservableObject是INCP的简单实现。

  2. RelayCommand是ICommand接口的简单实现。

如果你对代码有问题,我很乐意帮忙。问候,

最新更新