Files

44 lines
1.5 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace MainShell.Converter
{
public class WidthToMarginConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double actualWidth = 0;
if (value is double d) actualWidth = d;
double expanded = 200;
double extra = 0;
if (parameter is string s)
{
var parts = s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 1 && double.TryParse(parts[0], out var p0)) expanded = p0;
if (parts.Length >= 2 && double.TryParse(parts[1], out var p1)) extra = p1;
}
// 计算右侧 Margin
// 当 actualWidth == expanded 时返回 0不偏移
// 当 actualWidth 减小时会返回正值,从而把 ToggleButton 向左推(或根据需要调整公式)
double right = Math.Max(0, expanded - actualWidth) + extra;
// 根据布局需要你可以修改这里返回的 Thickness 的哪一项Left/Right
return new Thickness(-30, 0, 0, 0);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}