using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace FLY.ControlLibrary
{
///
/// 后台添加功能,按钮 click事件 属性=true
///
public class Set1Action : IDisposable
{
///
/// 对象
///
public object obj;
///
/// bool 属性
///
public string propertyname;
///
/// 按下时为 true
///
public bool pressIsTrue;
UIElement uIElement;
///
///
///
///
///
///
public Set1Action(UIElement uIElement, object obj, string propertyname)
{
this.uIElement = uIElement;
this.obj = obj;
this.propertyname = propertyname;
uIElement.PreviewMouseDown += UIElement_PreviewMouseDown;
uIElement.PreviewTouchDown += UIElement_PreviewTouchDown;
}
///
///
///
///
///
public Set1Action(UIElement uIElement, string propertyname) : this(uIElement, null, propertyname)
{
}
public void Dispose()
{
uIElement.PreviewMouseDown -= UIElement_PreviewMouseDown;
uIElement.PreviewTouchDown -= UIElement_PreviewTouchDown;
}
private void UIElement_PreviewTouchDown(object sender, TouchEventArgs e)
{
Down();
}
private void UIElement_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Down();
}
bool IsObjNull()
{
if (obj == null)
{
if (uIElement is FrameworkElement)
{
obj = (uIElement as FrameworkElement).DataContext;
if (obj == null)
return true;
}
else
{
return true;
}
}
return false;
}
void Down()
{
if (IsObjNull()) return;
Misc.PropertiesManager.SetValue(obj, propertyname, true);
}
}
///
/// Set1Action 的 XAML 版 ,附加行为。
///
public class Set1Behavior : Behavior
{
public System.Windows.Data.Binding Binding { get; set; }
Set1Action set1Action = null;
public Set1Behavior()
{
}
protected override void OnAttached()
{
base.OnAttached();
UIElement dobj = AssociatedObject;
if (Binding != null && Binding.Path != null)
{
if (Binding.Source != null)
{
set1Action = new Set1Action(dobj, Binding.Source, Binding.Path.Path);
}
else
{
var obj = COMMON.GetDataContext(dobj);
if (obj == null)
{
set1Action = new Set1Action(dobj, Binding.Path.Path);
}
else
{
set1Action = new Set1Action(dobj, obj, Binding.Path.Path);
}
}
}
}
protected override void OnDetaching()
{
base.OnDetaching();
if (set1Action != null)
{
set1Action.Dispose();
set1Action = null;
}
}
}
}