双向绑定数据模型
public class BaseModel : INotifyPropertyChanged//基础数据模型
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
双向绑定集合属性
public ObservableCollection<ShowLrcItme> ShowMainTime { get; set; }
绑定枚举类型Xaml
namespace Test.Enums
{
public enum E_Progect_Type
{
WinForm,
WPF,
WebApi,
}
}
//WPF前端页面绑定
<ComboBox ItemsSource="{Binding Source={StaticResource Enu_XiangMu_Enum}}" />
//样式xaml编写绑定数据源
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:tp="clr-namespace:Test.Enums"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ObjectDataProvider x:Key="Enu_XiangMu_Enum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="tp:E_Progect_Type" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</ResourceDictionary>
//注意app.xaml文件中添加自定义样式xaml
//<ResourceDictionary Source="Style/DataEnums.xaml"/>
WPF Listbox 拖拽排序
public ObservableCollection<MainType> Level_Two_Datas { get; set; }
public void BindData(){
Level_Two_Datas =new ObservableCollection<MainType>();
}
private void Level_Two_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var pos = e.GetPosition(Level_Two);
HitTestResult result = VisualTreeHelper.HitTest(Level_Two, pos);
if (result == null)
{
return;
}
var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
if (listBoxItem == null || listBoxItem.Content != Level_Two.SelectedItem)
{
return;
}
MouseDownPoint= listBoxItem.TranslatePoint(new Point(), Level_Two);
DataObject dataObj = new DataObject(listBoxItem.Content as MainType);
DragDrop.DoDragDrop(Level_Two, dataObj, DragDropEffects.Move);
}
}
private void Level_Two_Drop(object sender, DragEventArgs e)
{
var pos = e.GetPosition(Level_Two);
var result = VisualTreeHelper.HitTest(Level_Two, pos);
if (result == null)
{
return;
}
//查找元数据
var sourcePerson = e.Data.GetData(typeof(MainType)) as MainType;
if (sourcePerson == null)
{
return;
}
//查找目标数据
var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
if (listBoxItem == null)
{
return;
}
var targetPerson = listBoxItem.Content as MainType;
if (ReferenceEquals(targetPerson, sourcePerson))
{
return;
}
var PT = listBoxItem.TranslatePoint(new Point(), Level_Two);
if (MouseDownPoint.Y>PT.Y)
{
Level_Two_Datas.Remove(sourcePerson);
Level_Two_Datas.Insert(Level_Two_Datas.IndexOf(targetPerson), sourcePerson);
}
else
{
var newIndex = Level_Two_Datas.IndexOf(targetPerson);
Level_Two_Datas.Remove(sourcePerson);
Level_Two_Datas.Insert(newIndex, sourcePerson);
}
}
private void Level_One_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var maintype = Level_One.SelectedItem as MainType;
if (maintype!=null)
{
Level_Two_Datas.Clear();
foreach (var item in maintype.children)
{
Level_Two_Datas.Add(item);
}
}
}
}
internal static class Utils
{
//根据子元素查找父元素
public static T FindVisualParent<T>(DependencyObject obj) where T : class
{
while (obj != null)
{
if (obj is T)
return obj as T;
obj = VisualTreeHelper.GetParent(obj);
}
return null;
}
}
发表回复