wpf样式绑定 行为绑定 事件关联 路由事件实例

时间:2023-03-08 19:30:28

代码说明:我要实现一个这样的功能  有三个window窗口  每个窗体有一个label标签  当我修改三个label标签中任意一个字体颜色的时候  其他的label标签字体颜色也变化

首先三个窗体不用贴代码了  直接添加三个就行了

样式绑定:

先添加数据源  代码如下: (注:为了防止propertyName硬编码写死   可以使用CallerMemberName附加属性来获取默认的属性名称 或者使用表达式目录树Expression<Func<T>>的方式来获取)

 public class ButtonBase : ContentControl, INotifyPropertyChanged
{
public static readonly RoutedEvent ClickEvent;
private SolidColorBrush brush = new SolidColorBrush(Colors.Red); public event PropertyChangedEventHandler PropertyChanged; private static ButtonBase btnBase; public static ButtonBase getButtonBase()
{
if (btnBase == null)
btnBase = new ButtonBase() { Foreground = new SolidColorBrush(Colors.Red) };
return btnBase;
}
public SolidColorBrush Brush
{
get { return brush; }
set
{
if (value != brush)
{
brush = value;
NotifyPropertyChanged<SolidColorBrush>(() => this.Brush);//NotifyPropertyChanged();
}
}
}
private void NotifyPropertyChanged([CallerMemberName] String PropertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
private void NotifyPropertyChanged<T>(Expression<Func<T>> PropertyName)
{
if (PropertyChanged != null)
{
var expressions = PropertyName.Body as MemberExpression; PropertyChanged(this, new PropertyChangedEventArgs(expressions.Member.Name));
}
}

给Label标签绑定数据源  窗体初始化的时候绑定三遍就可以了  绑定完以后  直接设置就行了

            Binding bind = new Binding();
bind.Source = ButtonBase.getButtonBase();
bind.Mode = BindingMode.TwoWay;
bind.Path = new PropertyPath("Brush");
label1.SetBinding(Label.ForegroundProperty, bind);

行为绑定

先添加引用 C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\System.Windows.Interactivity.dll
using System.Windows.Interactivity;

定义一个UIElement拖动的行为

 public class DragInCanvasBehavior : Behavior<UIElement>
{
private Canvas canvas;
private bool isDragOn = false;
private Point mouseOffset;
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
this.AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;
this.AssociatedObject.MouseMove += AssociatedObject_MouseMove;
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
this.AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseLeftButtonUp;
this.AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
}
void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
{
if (isDragOn)
{
Point point = e.GetPosition(canvas);
AssociatedObject.SetValue(Canvas.LeftProperty,point.X);
AssociatedObject.SetValue(Canvas.TopProperty,point.Y);
}
} void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isDragOn)
{
AssociatedObject.ReleaseMouseCapture();
isDragOn = false;
}
} void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (canvas == null)
canvas = (Canvas)VisualTreeHelper.GetParent(this.AssociatedObject);
isDragOn = true;
mouseOffset = e.GetPosition(AssociatedObject);
AssociatedObject.CaptureMouse();
} }

前台使用该行为

  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

 <Canvas HorizontalAlignment="Left" Height="" VerticalAlignment="Top" Width="" Margin="10,138,0,0" RenderTransformOrigin="0.5,0.5">
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleY="0.583"/>
<RotateTransform/>
<TranslateTransform Y="0.565"/>
</TransformGroup>
</Canvas.RenderTransform>
<Rectangle x:Name="dragRec" Canvas.Left="" Canvas.Right="" Fill="Red" Width="" Height="">
<i:Interaction.Behaviors>
<loc:DragInCanvasBehavior></loc:DragInCanvasBehavior>
</i:Interaction.Behaviors>
</Rectangle>
<Label Content="abcdefg">
<i:Interaction.Behaviors>
<loc:DragInCanvasBehavior></loc:DragInCanvasBehavior>
</i:Interaction.Behaviors>
</Label>
</Canvas>

事件关联   当鼠标放到button上面的时候字体变大  button背景颜色变化

 <Style x:Key="buttonStyle">
<EventSetter Event="Button.MouseEnter" Handler="button_MouseEnter"></EventSetter>
<EventSetter Event="Button.MouseLeave" Handler="button_MouseLeave"></EventSetter>
<Style.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize" To="">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="FontSize">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>

App.xmal.cs 后台代码

  private void button_MouseEnter(object sender, MouseEventArgs e)
{
(sender as Button).Background = new SolidColorBrush(Colors.Brown);
}
private void button_MouseLeave(object sender, MouseEventArgs e)
{
(sender as Button).Background = null;
}