using Microsoft.Office.Interop.Word;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PDF_Master.CustomControl.CompositeControl
{
///
/// SlidComboControl.xaml 的交互逻辑
///
public partial class SlidComboControl : UserControl
{
public List Items { get;private set; }
public event RoutedEventHandler ValueChanged;
public string Title
{
get { return (string)GetValue(TitleProperty); }
set {
SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(SlidComboControl), new PropertyMetadata("Line Width"));
//public int IndexSelected
//{
// get { return (int)GetValue(IndexSelectedProperty); }
// set
// {
// SetValue(IndexSelectedProperty, value);
// }
//}
//public static readonly DependencyProperty IndexSelectedProperty =
// DependencyProperty.Register("IndexSelected", typeof(int), typeof(SlidComboControl), new PropertyMetadata(0));
public string Unit
{
get { return (string)GetValue(UnitProperty); }
set { SetValue(UnitProperty, value); }
}
public static readonly DependencyProperty UnitProperty =
DependencyProperty.Register("Unit", typeof(string), typeof(SlidComboControl), new PropertyMetadata("pt"));
public SlidComboControl()
{
InitializeComponent();
Items = new List();
DefaultItems();
SetLanguage();
}
private void SetLanguage()
{
//焊死了
if(thicknessText.Text== "Line Width")
{
thicknessText.Text = App.MainPageLoader.GetString("Annotation_Thickness");
}
}
public void SetItemSource(List items)
{
Items = items;
combox.ItemSource = Items;
UpdateMaxMinValue(items);
}
private void UpdateMaxMinValue(List items)
{
double MaxValue = 0;
double MinValue = 0;
if (items != null)
{
foreach (var item in items)
{
if (item.Value >= MaxValue)
MaxValue = item.Value;
}
MinValue = MaxValue;
foreach (var item in items)
{
if (item.Value <= MinValue)
MinValue = item.Value;
}
ThicknessSlider.Maximum = MaxValue;
ThicknessSlider.Minimum = MinValue;
}
}
private void DefaultItems()
{
var item = new ComboDataItem(3);
Items.Add(item);
item = new ComboDataItem(6);
Items.Add(item);
item = new ComboDataItem(9);
Items.Add(item);
item = new ComboDataItem(12);
Items.Add(item);
item = new ComboDataItem(15);
Items.Add(item);
item = new ComboDataItem(18);
Items.Add(item);
item = new ComboDataItem(21);
Items.Add(item);
item = new ComboDataItem(24);
Items.Add(item);
combox.ItemSource = Items;
UpdateMaxMinValue(Items);
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
//ThicknessSlider.ValueChanged += ThicknessSlider_ValueChanged;
combox.ValueChanged -= combox_ValueChanged;
combox.ValueChanged += combox_ValueChanged;
}
private void combox_ValueChanged(object sender, RoutedEventArgs e)
{
if (sender is double == true)
{
//点击下拉框时触发
if (combox.IsExistInComBox(sender))
{
Trace.WriteLine("combox_ValueChanged" + (double)sender);
ThicknessSlider.Value = (double)sender;
ValueChanged?.Invoke((double)sender, null);
}
}
}
//事件重复SelectedValuePropertyChanged 2023/4/4
//private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
//{
// combox.SelectedItems = new ComboDataItem(Value);
// // ValueChanged?.Invoke(Value, null);
//}
//暂无作用,后续准备
public int SelectedIndex
{
get { return (int)GetValue(SelectedIndexProperty); }
set { SetValue(SelectedIndexProperty, value);
}
}
public static readonly DependencyProperty SelectedIndexProperty =
DependencyProperty.Register("SelectedIndex", typeof(int), typeof(SlidComboControl), new PropertyMetadata(-1, SelectedIndexPropertyChanged));
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(SlidComboControl), new PropertyMetadata(1.0, SelectedValuePropertyChanged));
private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as SlidComboControl;
var value = (double)e.NewValue;
if (control != null)
{
Trace.WriteLine("SelectedValuePropertyChanged" + value);
control.combox.SelectedItems = new ComboDataItem(value, control.Unit);
//判断滑动条的值是否与combox中的项匹配
if (control != null && control.combox.ItemSource != null)
{
for (int i = 0; i < control.combox.ItemSource.Count; i++)
{
if (value == control.combox.ItemSource[i].Value)
{
return;
}
if(i==control.combox.ItemSource.Count-1)
{
control.combox.SelectedIndex = -1;
control.combox.SelectedItems = new ComboDataItem(value, control.Unit);
}
}
}
}
}
public List ItemsSource
{
get { return (List)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(List), typeof(SlidComboControl), new PropertyMetadata(null, SelectedItemsSourcePropertyChanged));
//选中项触发属性
private static void SelectedIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as SlidComboControl;
var selectedIndex = (int)e.NewValue;
if (control != null /*&& control.IsSelectedEmpty == false*/)
{
//删掉了selectedIndex != -1,设置不选中任何选项的情况
//删掉了control.SelectedItems = null;避免崩溃
if (control.combox.Items != null && control.combox.Items.Count > 0)
{
control.combox.SelectedIndex = selectedIndex;
if (control.combox.SelectedItems != null)
control.combox.SelectedItems = (ComboDataItem)control.combox.SelectedItems;
}
control.combox.UpdateSelectedIndex();
}
}
private static void SelectedItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as SlidComboControl;
var value = (List)e.NewValue;
if (control != null && value != null)
{
control.SetItemSource(value);
}
}
private void ThicknessSlider_PreviewMouseMove(object sender, MouseEventArgs e)
{
if(e.LeftButton == MouseButtonState.Pressed)
{
ValueChanged?.Invoke(ThicknessSlider.Value, null);
}
}
}
}