赞
踩
前台绑定数据,到后台传输,后台响应,再反馈给前台的机制,其实核心就在于一个响应的过程,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;
}
}
UpdateSourceTrigger进行数据改变触发事件。是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>
<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>
<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>
<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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。