当前位置:   article > 正文

WPF总结一(数据响应,双向绑定修改事件,四大触发器)_wpf datepicker双向绑定多次触发

wpf datepicker双向绑定多次触发

数据响应

前台绑定数据,到后台传输,后台响应,再反馈给前台的机制,其实核心就在于一个响应的过程,WPF数据响应是对应的类属性继承于NotifyPropertyBase,接口进行实现的。
有时候值修改不生效,就是属于没有继承该接口导致的无法响应问题

双向绑定,修改机制触发事件

SearchText="{Binding SearchValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
后台:
PropertyChanged += SearchMessageChanged;
 private void SearchMessageChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "SearchValue":
                    QueryData();
                    break;
                default:
                    break;
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

UpdateSourceTrigger进行数据改变触发事件。是WPF自带的框架机制

常见触发事件

WPF触发器

简单触发器

满足条件就进行触发

<Window x:Class="Styles.SimpleTriggers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SimpleTriggers" Height="300" Width="300"
    >
  <Window.Resources>
    <Style x:Key="BigFontButton">
      <Style.Setters>
        <Setter Property="Control.FontFamily" Value="Times New Roman" />
        <Setter Property="Control.FontSize" Value="10" />
        
      </Style.Setters>
      <Style.Triggers>
        <Trigger Property="Control.IsFocused" Value="True">
          <Setter Property="Control.Foreground" Value="DarkRed" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </Window.Resources>
 
  <StackPanel Margin="5">
    <Button Padding="5" Margin="5"
            Style="{StaticResource BigFontButton}" 
              >A Customized Button</Button>
    <TextBlock Margin="5">Normal Content.</TextBlock>
    <Button Padding="5" Margin="5"
            >A Normal Button</Button>
    <TextBlock Margin="5">More normal Content.</TextBlock>
    <Button Padding="5" Margin="5"
            Style="{StaticResource BigFontButton}" 
              >Another Customized Button</Button>
  </StackPanel>
</Window>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

多条件触发器

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsFocused" Value="True"></Condition>
        <Condition Property="Content" Value="{x:Null}"></Condition>
    </MultiTrigger.Conditions>
 
    <Setter Property="ToolTip" Value="content is null!"></Setter>
</MultiTrigger>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

事件触发器EventTrigger——在特定的路由事件发生时被触发,主要用于动画。

<Style TargetType="ListBoxItem">
      <Setter Property="Opacity" Value="0.5" />
      <Setter Property="MaxHeight" Value="75" />
      <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
          <Trigger.Setters>
            <Setter Property="Opacity" Value="1.0" />
          </Trigger.Setters>
        </Trigger>
        <EventTrigger RoutedEvent="Mouse.MouseEnter">
          <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation
                  Duration="0:0:0.2"
                  Storyboard.TargetProperty="MaxHeight"
                  To="90"  />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger.Actions>
        </EventTrigger>
        <EventTrigger RoutedEvent="Mouse.MouseLeave">
          <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation
                  Duration="0:0:1"
                  Storyboard.TargetProperty="MaxHeight"  />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger.Actions>
        </EventTrigger>
      </Style.Triggers>
    </Style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

数据触发器DataTrigger——根据绑定的数据不同显示不同的内容

<TreeView Name="_tree" Margin="0" BorderThickness="0" VerticalAlignment="Stretch" Background="Transparent" ItemsSource="{Binding Children}" >
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                        <Border CornerRadius="0" Margin="1"  x:Name="back" MinWidth="70"
                                Background="Transparent" DataContext="{Binding}" PreviewMouseMove="TreeItem_PreviewMouseMove">
                            <StackPanel Orientation="Horizontal" Margin="2">
                                <Image x:Name="BGimage" Source="/Vdc3D.Coms.DModelEditor;component/Images/item.png" Height="15" Width="15" />
                                <TextBlock Text="{Binding ShowText}" Margin="2 0"/>
                            </StackPanel>
                            <Border.ContextMenu>
                                <ContextMenu x:Name="menu" >
                                    <MenuItem Header="Add Directory" x:Name="menu_addDir" Click="AddDir_Click" DataContext="{Binding}"/>
                                    <MenuItem Header="Add Property" x:Name="menu_addChild"  Click="AddChild_Click" DataContext="{Binding}"/>
                                    <MenuItem Header="Edit" Click="Modify_Click" DataContext="{Binding}"/>
                                    <MenuItem Header="Delete" Click="Delete_Click" DataContext="{Binding}"/>
                                </ContextMenu>
                            </Border.ContextMenu>
                        </Border>
                        <HierarchicalDataTemplate.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type TreeViewItem}},Path=IsExpanded}"  Value="False"/>
                                    <Condition Binding="{Binding IsDir}" Value="True"/>
                                </MultiDataTrigger.Conditions>
                                <MultiDataTrigger.Setters>
                                    <Setter TargetName="BGimage" Property="Source" Value="/Vdc3D.Coms.DModelEditor;component/Images/dir.png" />
                                </MultiDataTrigger.Setters>
                            </MultiDataTrigger>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type TreeViewItem}},Path=IsExpanded}"  Value="True"/>
                                    <Condition Binding="{Binding IsDir}" Value="True"/>
                                </MultiDataTrigger.Conditions>
                                <MultiDataTrigger.Setters>
                                    <Setter TargetName="BGimage" Property="Source" Value="/Vdc3D.Coms.DModelEditor;component/Images/dir_open.png" />
                                </MultiDataTrigger.Setters>
                            </MultiDataTrigger>
                            <DataTrigger Binding="{Binding IsDir}" Value="True">
                                <Setter TargetName="menu_addDir"  Property="Visibility" Value="Visible"/>
                                <Setter TargetName="menu_addChild"  Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsDir}" Value="False">
                                <Setter TargetName="menu_addDir"  Property="Visibility" Value="Collapsed"/>
                                <Setter TargetName="menu_addChild"  Property="Visibility" Value="Collapsed"/>
                            </DataTrigger>
                        </HierarchicalDataTemplate.Triggers>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/928514
推荐阅读
相关标签
  

闽ICP备14008679号