WPF中, 如何将控件的触发事件绑定到ViewModel

news2024/10/6 20:34:16

在DataGrid 等控件中, 有很多这种带闪电符号的触发事件. 如果用传统的事件驱动, 则直接在后台中建立 一个private PropertyChanged(Sender s, EventAgars Args) 即可. 但是如果需要绑定到ViewModel的话? 应该怎么做?
在这里插入图片描述
带闪电符号的触发事件

实现viewModel绑定前端触发事件的写法:

        <DataGrid
            x:Name="myDataGrid"
            AlternationCount="2"
            AutoGenerateColumns="False"
            FontSize="24"
            ItemsSource="{Binding Students}"
            SelectedItem="{Binding SelectStudent}"
            SelectionMode="Extended">

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding DataGridSelectedCommand}" CommandParameter="{Binding ElementName=myDataGrid, Path=SelectedItems}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="BorderThickness" Value="0,0,0,3" />
                    <Setter Property="Cursor" Value="Hand" />
                    <Setter Property="FontWeight" Value="SemiBold" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                    <Setter Property="Margin" Value="0" />
                    <Setter Property="MinHeight" Value="25" />
                    <Setter Property="MinWidth" Value="0" />
                    <Setter Property="Background" Value="#2B2C31" />
                    <Setter Property="SnapsToDevicePixels" Value="True" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="DataGridCell">
                                <Grid Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Columns>
                <DataGridTextColumn
                    Width="*"
                    Binding="{Binding Id}"
                    Header="Id" />
                <DataGridTextColumn
                    Width="*"
                    Binding="{Binding Age}"
                    Header="年龄" />
                <DataGridTextColumn
                    Width="*"
                    Binding="{Binding Name}"
                    Header="姓名" />
                <DataGridTemplateColumn Header="操作">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock
                                    Margin="5"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Center"
                                    Background="White"
                                    FontFamily="{StaticResource fontAwesome}"
                                    FontSize="24"
                                    Tag="修改"
                                    Text="&#xf14b;">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseUp">
                                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.DataGridUpDateCommand}" CommandParameter="{Binding}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Cursor" Value="Hand" />
                                                    <Setter Property="Foreground" Value="Orange" />
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="False">
                                                    <Setter Property="Foreground" Value="Black" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                                <TextBlock
                                    Margin="5"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Center"
                                    Background="White"
                                    FontFamily="{StaticResource fontAwesome}"
                                    Tag="删除"
                                    Text="&#xf056;">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseUp">
                                            <i:InvokeCommandAction Command="{Binding DataContext.DataGridDeleteCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{Binding}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Cursor" Value="Hand" />
                                                    <Setter Property="Foreground" Value="Red" />
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="False">
                                                    <Setter Property="Foreground" Value="Black" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

分析核心代码:

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding DataGridSelectedCommand}" CommandParameter="{Binding ElementName=myDataGrid, Path=SelectedItems}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>

i的命名空间 : xmlns:i=“http://schemas.microsoft.com/xaml/behaviors”

使用触发器, 事件触发器, 将前端的触发事件写在EventName中 SelectionChanged. 然后把事件绑定到后台, 将多选的Student 以CommandParameter的形式传入后端

后端代码:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MathNet.Numerics.Distributions;
using NavTest.Eneities;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NavTest.ViewModels
{
    public partial class Page3ViewModel:ObservableObject
    {
        [ObservableProperty]
        private ObservableCollection<Student> students = new();

        public Page3ViewModel()
        {
            for (int i = 0; i < 15; i++)
            {
                Students.Add(
                    new()
                    {
                        Id = i + 1,
                        Name = $"StudentName{i}",
                        Age = $"{i}+10",
                        Description = $"str+{i}"
                    }
                );
            }
        }

        [RelayCommand]
        public void DataGridDelete(Student student)
        {

        }

        //[RelayCommand]

        //public void DataGridUpDate(Student student)
        //{

        //}

        public RelayCommand<Student> DataGridUpDateCommand => new RelayCommand<Student>((arg) =>
        {

        });

        [RelayCommand]
        public void DataGridSelected(IList<object> objs)
        {
            MyStudents = new();
            foreach (var item in objs)
            {
                if (item is Student stu)
                {
                    MyStudents.Add(stu);
                }
            }

            Student stu1 = SelectStudent;
        }

        [RelayCommand]
        public void ItemControlCmd(Student student)
        {

        }

        [ObservableProperty]
        private ObservableCollection<Student> myStudents = new ();
        [ObservableProperty]
        private Student selectStudent = new();
    }
}

完整的前端代码:

<UserControl
    x:Class="NavTest.Views.Page3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:hc="https://handyorg.github.io/handycontrol"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:local="clr-namespace:NavTest.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:mv="clr-namespace:NavTest.ViewModels"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:tt="clr-namespace:NavTest.Eneities"
    xmlns:vc="clr-namespace:NavTest.Components"
    d:DataContext="{d:DesignInstance mv:Page3ViewModel}"
    d:DesignHeight="450"
    d:DesignWidth="800"
    FontSize="24"
    mc:Ignorable="d">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <DataGrid
            x:Name="myDataGrid"
            AlternationCount="2"
            AutoGenerateColumns="False"
            FontSize="24"
            ItemsSource="{Binding Students}"
            SelectedItem="{Binding SelectStudent}"
            SelectionMode="Extended">

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding DataGridSelectedCommand}" CommandParameter="{Binding ElementName=myDataGrid, Path=SelectedItems}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="BorderThickness" Value="0,0,0,3" />
                    <Setter Property="Cursor" Value="Hand" />
                    <Setter Property="FontWeight" Value="SemiBold" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                    <Setter Property="Margin" Value="0" />
                    <Setter Property="MinHeight" Value="25" />
                    <Setter Property="MinWidth" Value="0" />
                    <Setter Property="Background" Value="#2B2C31" />
                    <Setter Property="SnapsToDevicePixels" Value="True" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="DataGridCell">
                                <Grid Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Columns>
                <DataGridTextColumn
                    Width="*"
                    Binding="{Binding Id}"
                    Header="Id" />
                <DataGridTextColumn
                    Width="*"
                    Binding="{Binding Age}"
                    Header="年龄" />
                <DataGridTextColumn
                    Width="*"
                    Binding="{Binding Name}"
                    Header="姓名" />
                <DataGridTemplateColumn Header="操作">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock
                                    Margin="5"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Center"
                                    Background="White"
                                    FontFamily="{StaticResource fontAwesome}"
                                    FontSize="24"
                                    Tag="修改"
                                    Text="&#xf14b;">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseUp">
                                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.DataGridUpDateCommand}" CommandParameter="{Binding}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Cursor" Value="Hand" />
                                                    <Setter Property="Foreground" Value="Orange" />
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="False">
                                                    <Setter Property="Foreground" Value="Black" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                                <TextBlock
                                    Margin="5"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Center"
                                    Background="White"
                                    FontFamily="{StaticResource fontAwesome}"
                                    Tag="删除"
                                    Text="&#xf056;">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseUp">
                                            <i:InvokeCommandAction Command="{Binding DataContext.DataGridDeleteCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{Binding}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Cursor" Value="Hand" />
                                                    <Setter Property="Foreground" Value="Red" />
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="False">
                                                    <Setter Property="Foreground" Value="Black" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <WrapPanel
            Grid.Row="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Orientation="Horizontal">
            <TextBlock
                Margin="5,5,5,5"
                Foreground="White"
                Text="测试1" />
            <TextBlock
                Margin="5,5,5,5"
                Foreground="White"
                Text="测试2" />
            <TextBlock
                Margin="5,5,5,5"
                Foreground="White"
                Text="测试3" />
            <TextBlock
                Margin="5,5,5,5"
                Foreground="White"
                Text="测试4" />
            <TextBlock
                Margin="5,5,5,5"
                Foreground="White"
                Text="测试5" />
            <TextBlock
                Margin="5,5,5,5"
                Foreground="White"
                Text="测试6" />
        </WrapPanel>

        <Grid Grid.Row="1" Grid.Column="1">
            <ItemsControl AlternationCount="2" ItemsSource="{Binding MyStudents}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border x:Name="border" Padding="2">
                            <StackPanel>
                                <TextBlock Foreground="White" Text="{Binding Name}" />
                                <TextBlock Foreground="White" Text="{Binding Age}" />
                                <Button
                                    Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.ItemControlCmdCommand}"
                                    CommandParameter="{Binding}"
                                    Content="{Binding Description}"
                                    Foreground="White" />
                            </StackPanel>
                        </Border>
                        <DataTemplate.Triggers>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                <Setter TargetName="border" Property="Background" Value="red" />
                            </Trigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </Grid>
</UserControl>


如果在一个有ItemSource的控件内找不到 后台的Property 和command, 可以尝试 binding ElementName 和binding Relationsource

例如command 可以 binding Relationsource = {RelationSource AncestorType = window} path = datacontext. xxxcommand
commandparameter={binding} 返回单个class为数据源

CommandParameter=“{Binding ElementName=myDataGrid, Path=SelectedItems}” 绑定别的控件上的属性 或者 用child的方式

一些补充的内容:

除了BooleanToVisibilityConverter之外,WPF框架还提供了许多其他的内置转换器。以下是一些常用的系统现有转换器的示例:

BooleanToVisibilityConverter: 将bool值转换为Visibility枚举值。
InverseBooleanConverter: 反转bool值,将true转换为false,将false转换为true。
StringFormatConverter: 格式化字符串,可以将值与指定的格式字符串进行组合。
DateTimeConverter: 将DateTime对象转换为不同格式的字符串,或者将字符串转换为DateTime对象。
BrushConverter: 将字符串表示的颜色转换为Brush对象。
ValueConverterGroup: 将多个转换器组合成一个组,按照顺序依次进行转换。
EnumToStringConverter: 将枚举值转换为对应的字符串表示。
NumericUpDownConverter: 用于增加和减少数值类型的转换器。
CollectionViewSource: 用于在集合和视图之间进行转换和筛选。
这只是一些常见的示例,WPF框架提供了更多的内置转换器,可以满足各种转换需求。你可以根据具体的场景和需求选择合适的转换器来使用。

eventTrriger
DataTrigger

常用触发器:

CallMethodAction
ChangePropertyAction
InvokeCommandAction


CallMethodAction 和 InvokeCommandAction 是在 WPF 中用于触发操作的两种不同方式,它们有一些区别,并且在特定的情况下可能不可互相替代。

CallMethodAction:CallMethodAction 允许你直接调用指定的方法。你可以通过设置 TargetObject 属性指定要调用方法的对象,并使用 MethodName 属性指定要调用的方法名称。这种方式适用于简单的、特定于 UI 的操作,例如在按钮点击或其他事件发生时执行某个方法。它可以方便地将事件触发与方法调用关联起来,但缺点是它与 UI 逻辑紧耦合,并且无法利用 WPF 中的命令系统。

InvokeCommandAction:InvokeCommandAction 允许你通过绑定一个命令来执行操作。你可以使用 Command 属性绑定到一个实现了 ICommand 接口的命令对象。当触发与行为关联的事件时,命令的 Execute 方法将被调用,而命令的 CanExecute 方法决定是否可以执行。这种方式更符合 MVVM 架构和解耦原则,它将 UI 逻辑与业务逻辑分离,并提供了更好的可测试性和可重用性。

虽然 CallMethodAction 和 InvokeCommandAction 实现了类似的功能,但在大多数情况下,推荐使用 InvokeCommandAction 和命令模式来处理用户交互。它更符合 MVVM 设计模式的理念,并且提供了更好的灵活性和可扩展性。但在一些简单的场景下,CallMethodAction 也可以作为一种快速临时解决方案。

因此,根据具体的需求和架构设计,你可以选择使用 CallMethodAction 或 InvokeCommandAction 来触发操作。


在 WPF 中,CallMethodAction 是一种交互行为(Interaction Behavior),它允许你通过 XAML 触发调用特定方法。它是 System.Windows.Interactivity 命名空间下的一个类,需要通过添加对 System.Windows.Interactivity 程序集的引用才能使用。

CallMethodAction 可以用于任何具有无参数的方法。它与事件触发器(EventTrigger)一起使用,当特定事件发生时,将触发并调用绑定的方法。

以下是使用 CallMethodAction 的示例:

CallMethodAction :===============


<StackPanel>
    <Button Content="Click Me">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <i:CallMethodAction TargetObject="{Binding}" MethodName="HandleButtonClick" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
</StackPanel>

关闭窗口,用到window的close方法:
                <Button
                    Background="Red"
                    Content="&#xe653;"
                    Style="{StaticResource ControlButtonStyle}"
                    ToolTip="Close">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <i:CallMethodAction MethodName="Close" TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>



ChangePropertyAction=============
例如:TargetObject="{Binding} 返回了DataContext,就是ViewModel,它的PropertyName就是里面的属性IsMarker

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:ChangePropertyAction PropertyName="IsMarker" Value="True" TargetObject="{Binding}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="Closing">
            <i:ChangePropertyAction PropertyName="IsMarker" Value="False" TargetObject="{Binding}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

最小化,用到window的Minimized属性:

                <Button
                    Background="#22FFFFFF"
                    Content="&#xe7e6;"
                    Style="{StaticResource ControlButtonStyle}"
                    ToolTip="Minimize">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <i:ChangePropertyAction
                                PropertyName="WindowState"
                                TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
                                Value="Minimized" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>



<Button Content="Change Background">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <i:ChangePropertyAction TargetObject="{Binding ElementName=MyBorder}"
                                    PropertyName="Background"
                                    Value="Red" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>
<Border x:Name="MyBorder" Width="100" Height="100" Background="Green" />



<Button Content="Change Background">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <i:ChangePropertyAction TargetObject="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
                                    PropertyName="Background"
                                    Value="Red" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

=============

                        <RadioButton
                            Width="200"
                            Height="50"
                            Content="连接PLC"
                            FontSize="18"
                            Foreground="White"
                            Style="{DynamicResource RadioButtonMenuStyle}"
                            Tag="&#xf11c;">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Checked">
                                    <i:InvokeCommandAction Command="{Binding ConnCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=RadioButton}}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </RadioButton>


============


在 <i:EventTrigger> 元素中,可以使用不同的 EventName 属性来指定常见事件的名称,以触发相应的动作或触发器。以下是一些常用的 EventName 值示例:

Loaded:当元素加载完成时触发。
Unloaded:当元素卸载时触发。
MouseEnter:当鼠标指针进入元素时触发。
MouseLeave:当鼠标指针离开元素时触发。
MouseDown:当鼠标按下按钮时触发。
MouseUp:当鼠标释放按钮时触发。
Click:当元素被点击时触发。
Checked:当复选框或单选按钮的选中状态改变时触发。
TextChanged:当文本框的文本内容改变时触发。
PreviewMouseMove:鼠标在元素上移动。
PreviewMouseUp:鼠标释放按钮。
MouseEnter:鼠标进入元素。
MouseLeave:鼠标离开元素。
PreviewMouseWheel:滚动鼠标滚轮。
键盘事件:

PreviewKeyDown:按下键盘上的键。
PreviewKeyUp:释放键盘上的键。
KeyDown:按下键盘上的键(冒泡事件)。
KeyUp:释放键盘上的键(冒泡事件)。


SelectionChanged:当下拉列表、列表框或其他选择控件的选择项发生改变时触发。
这只是一些常见的 EventName 值示例,实际上,可以根据具体的控件和需求,选择适合的事件名称。根据控件的类型和事件的定义,可以在相关文档或控件的事件文档中找到更多可用的 EventName 值。


<Button Content="Toggle Size" Width="100" Height="30">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <i:Interaction.Behaviors>
                <ei:ChangePropertyAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
                                         PropertyName="WindowState">
                    <ei:ChangePropertyAction.Value>
                        <System:WindowState>
                            <System:WindowState x:FactoryMethod="FromValue">
                                <System:WindowState.Normal />
                                <System:WindowState.Maximized />
                            </System:WindowState>
                        </System:WindowState>
                    </ei:ChangePropertyAction.Value>
                </ei:ChangePropertyAction>
            </i:Interaction.Behaviors>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>



========i:DataTrigger========

{Binding RelativeSource  = {RelativeSource Self} 
{Binding RelativeSource = {RelativeSource  AncestorType=Button}}  

{Binding ElementName=TestTBlock, Path=Name}


<Button Content="Click Me">
    <i:Interaction.Triggers>
        <i:DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path = IsMouseOver}" Value="False">
            <i:ChangePropertyAction TargetObject="{Binding RelativeSource={RelativeSource Self}}"
                                    PropertyName="BackGround"
                                    Value="Red" />
        </i:DataTrigger>
    </i:Interaction.Triggers>
</Button>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1062999.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【C++设计模式之原型模式:创建型】分析及示例

简介 原型模式&#xff08;Prototype Pattern&#xff09;是一种创建型设计模式&#xff0c;它允许通过复制已有对象来生成新的对象&#xff0c;而无需再次使用构造函数。 描述 原型模式通过复制现有对象来创建新的对象&#xff0c;而无需显式地调用构造函数或暴露对象的创建…

JAVA编程题-求矩阵螺旋值

螺旋类 package entity; /*** 打印数组螺旋值类*/ public class Spiral { // 数组行private int row; // 数组列private int col; // 行列数private int size; // 当前行索引private int rowIndex; // 当前列索引private int colIndex; // 行开始索引private int rowStart; //…

大模型部署手记(5)ChatGLM2+Jetson AGX Orin

1.简介&#xff1a; 组织机构&#xff1a;智谱/清华 代码仓&#xff1a;https://github.com/THUDM/ChatGLM2-6B 模型&#xff1a;THUDM/chatglm2-6b 下载&#xff1a;https://huggingface.co/THUDM/chatglm2-6b 镜像下载&#xff1a;https://aliendao.cn/models/THUDM/chat…

Java日期的学习篇

关于日期的学习 目录 关于日期的学习JDK8以前的APIDate Date常用APIDate的API应用 SimpleDateFormatSimpleDateFormat常用API测试 反向格式化(逆操作)测试 训练案例需求(秒杀活动)实现 Calendar需求痛点常见API应用测试 JDK8及以后的API(修改与新增)为啥学习(推荐使用)新增的AP…

ArcGIS Engine:鹰眼图的拓展功能-点击和矩形+坐标状态栏

目录 01 前言 02 鹰眼图的控制功能 03 显示当前鼠标的地理坐标 01 前言 说是拓展&#xff0c;不过是忘记了实验还有附加实验.这里补上. 前文不再赘述,上一节查看&#xff1a;ArcGIS Engine&#xff1a;视图菜单的创建和鹰眼图的实现_炒茄子的博客-CSDN博客 这里加上三个功能…

unity脚本_Vector3 c#

接下来学习 相对世界坐标 首先我们给场景物体一个空物体 修改新建空物体名字为GameObjectFather 修改GameObjectFather坐标 修改GameObject2坐标 然后将GameObjectFahter设置成GameObject2的父物体 我们观察到子物体的坐标改变了但是 运行显示的相对世界坐标this.transform.po…

R语言教程课后习题答案(持续更新中~~)

R语言教程网址如下 https://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/index.html 目录 source()函数可以运行保存在一个文本文件中的源程序 R向量下标和子集 数值型向量及其运算 日期功能 R因子类型 source()函数可以运行保存在一个文本文件中的源程序…

学信息系统项目管理师第4版系列18_采购管理

1. 协议 1.1. 合同 1.1.1. 国际合作的项目经理应牢记&#xff0c;无论合同规定如何详尽&#xff0c;文化和当地法律对合同及其可执行性均有影响 1.2. 服务水平协议&#xff08;SLA&#xff09; 1.3. 谅解备忘录 1.4. 协议备忘录&#xff08;MOA&#xff09; 1.5. 订购单 …

十天学完基础数据结构-第八天(哈希表(Hash Table))

哈希表的基本概念 哈希表是一种数据结构&#xff0c;用于存储键值对。它的核心思想是将键通过哈希函数转化为索引&#xff0c;然后将值存储在该索引位置的数据结构中。 哈希函数的作用 哈希函数是哈希表的关键部分。它将输入&#xff08;键&#xff09;映射到哈希表的索引位…

Python常用功能的标准代码

后台运行并保存log 1 2 3 4 5 6 7 8 9 nohup python -u test.py > test.log 2>&1 & #最后的&表示后台运行 #2 输出错误信息到提示符窗口 #1 表示输出信息到提示符窗口, 1前面的&注意添加, 否则还会创建一个名为1的文件 #最后会把日志文件输出到test.log文…

卷积神经网络-池化层和激活层

2.池化层 根据特征图上的局部统计信息进行下采样&#xff0c;在保留有用信息的同时减少特征图的大小。和卷积层不同的是&#xff0c;池化层不包含需要学习的参数。最大池化(max-pooling)在一个局部区域选最大值作为输出&#xff0c;而平均池化(average pooling)计算一个局部区…

卷积神经网络-卷积层

卷积神经网络 卷积神经网络&#xff08;convolutional neural network&#xff0c;CNN&#xff09;是一类包含卷积计算且具有深度结构的前馈神经网络&#xff0c;是深度学习的代表算法之一。卷积神经网络具有表征学习能力&#xff0c;能够按其阶层结构对输入信息进行平移不变分…

H5移动端购物商城系统源码 小型商城全新简洁风格全新UI 支持易支付接口

一款比较简单的 H5 移动端购物商城系统源码&#xff0c;比较适合单品商城、小型商城使用。带有易支付接口。 源码下载&#xff1a;https://download.csdn.net/download/m0_66047725/88391704 源码下载2&#xff1a;评论留言或私信留言

【Spring笔记03】Spring依赖注入各种数据类型

这篇文章&#xff0c;详细介绍一下Spring框架中如何注入各种数据类型&#xff0c;包含&#xff1a;注入基本数据类型、数组、集合、Map映射、Property属性、注入空字符串、注入null值、注入特殊字符等内容&#xff0c;以及如何使用命名空间进行依赖注入。 目录 一、注入各种数据…

云原生Kubernetes:简化K8S应用部署工具Helm

目录 一、理论 1.HELM 2.部署HELM2 3.部署HELM3 二、实验 1.部署 HELM2 2.部署HELM3 三、问题 1.api版本过期 2.helm初始化报错 3.pod状态为ImagePullBackOff 4.helm 命令显示 no repositories to show 的错误 5.Helm安装报错 6.git命令报错 7.CentOS 7 下git c…

互联网Java工程师面试题·Elasticsearch 篇·第一弹

目录 1、elasticsearch 了解多少&#xff0c;说说你们公司 es 的集群架构&#xff0c;索引数据大小&#xff0c;分片有多少&#xff0c;以及一些调优手段 。 1.1 设计阶段调优 1.2 写入调优 1.3 查询调优 1.4 其他调优 2、elasticsearch 的倒排索引是什么 3、elastic…

ToDoList使用自定义事件传值

MyTop与MyFooter与App之间传递数据涉及到的就是子给父传递数据&#xff0c;MyList和MyItem与App涉及到爷孙传递数据。 之前的MyTop是使用props接收App传值&#xff0c;然后再在methods里面调用&#xff0c;现在使用自定义事件来处理子组件和父组件之间传递数据。 图是之前的…

新款UI动态壁纸头像潮图小程序源码

新款UI动态壁纸头像潮图小程序源码&#xff0c;不需要域名服务器&#xff0c;直接添加合法域名&#xff0c;上传发布就能使用。 可以对接开通流量主&#xff0c;个人也能运营&#xff0c;不需要服务器源码完整。整合头像&#xff0c;动态壁纸&#xff0c;文案功能齐全。 源码…

代码随想录Day12 二叉树 LeetCode T102二叉树的层序遍历 T226 翻转二叉树 T101 对称二叉树

本文思路和详细讲解来自于:代码随想录 (programmercarl.com) LeetCode T102 二叉树的层序遍历 题目链接:102. 二叉树的层序遍历 - 力扣&#xff08;LeetCode&#xff09; 题目思路: 本题使用队列辅助完成,讲解主要函数CheckOrder:首先判断root是否为空,是就直接返回,然后创建…

芯驰D9评测(3)--建立开发环境

1. 建立交叉编译链接环境 官网下载的SDK包中就有交叉工具链&#xff0c;米尔提供的这个 SDK 中除了包含各种源代码外还提供了必要的交叉工具链&#xff0c;可以直接用于编译应用程序等。 用户可以直接使用次交叉编译工具链来建立一个独立的开发环境&#xff0c;可单独编译…