using ivf_tl_Entity.ControlEntity;
using ivf_tl_Entity.Entity;
using ivf_tl_Service.HttpProvider;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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;
using static System.Net.Mime.MediaTypeNames;
using System.IO;
using System.Text.Json;
using ivf_tl_Manage.Win;
namespace ivf_tl_Manage.UserControls
{
///
/// AddressSelectUserControl.xaml 的交互逻辑
///
public partial class AddressSelectUserControl : UserControl
{
public delegate void SelectValueDelegate(string value);
public event SelectValueDelegate SelectValue;
public AddressSelectUserControl()
{
InitializeComponent();
string path = AppDomain.CurrentDomain.BaseDirectory + "address.json";
if (File.Exists(path))
{
string JSONstring = File.ReadAllText(path);
if (string.IsNullOrEmpty(JSONstring))
{
new ToastMessageWindow(AppData.Instance.MainWindow, 1920, 65, "地址json文件格式错误!").Show();
return;
}
Source = JsonSerializer.Deserialize>(JSONstring);
}
}
public ObservableCollection Source { get; set; }
public AddressEntity Province { get; set; }
public AddressEntity City { get; set; }
public AddressEntity Region { get; set; }
public string AddressProperty
{
get { return (string)GetValue(AddressPropertyProperty); }
set { SetValue(AddressPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for AddressProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AddressPropertyProperty =
DependencyProperty.Register("AddressProperty", typeof(string), typeof(AddressSelectUserControl), new PropertyMetadata(null, new PropertyChangedCallback(AddressPropertyChangedCallback)));
private static void AddressPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue == null) return;
string newValue = e.NewValue.ToString();
if (string.IsNullOrEmpty(newValue)) return;
var source = (AddressSelectUserControl)d;
source.tb_Defualt.Visibility = Visibility.Collapsed;
string[] address = newValue.Split("|");
for (int i = 0; i < address.Length; i++)
{
if (i == 0)
source.tb_Province.Text = address[i];
if (i == 1)
source.tb_City.Text = address[i];
if (i == 2)
source.tb_Region.Text = address[i];
if (i == 3)
source.tb_Street.Text = address[i];
}
}
public int Level
{
get { return (int)GetValue(LevelProperty); }
set { SetValue(LevelProperty, value); }
}
// Using a DependencyProperty as the backing store for Level. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LevelProperty =
DependencyProperty.Register("Level", typeof(int), typeof(AddressSelectUserControl), new PropertyMetadata(-1, new PropertyChangedCallback(LevelPropertyChangedCallback)));
private static void LevelPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
int newValue = (int)e.NewValue;
if (newValue == -1)
return;
var source = (AddressSelectUserControl)d;
switch (newValue)
{
case 0:
#region 省
source.Retset();
source.SetTextForeground(source.tb_Province, true);
var items = source.Source.GroupBy(a => a.pinyinPrefix).ToList();
source.LoadData(items, 1);
#endregion
break;
case 1:
#region 城市
source.Retset();
source.SetTextForeground(source.tb_Province, false);
source.SetTextForeground(source.tb_City, true);
var cityItems = source.Province.children.GroupBy(a => a.pinyinPrefix).ToList();
source.LoadData(cityItems, 2);
#endregion
break;
case 2:
#region 区域
source.Retset();
source.SetTextForeground(source.tb_City, false);
source.SetTextForeground(source.tb_Region, true);
var regionItems = source.City.children.GroupBy(a => a.pinyinPrefix).ToList();
source.LoadData(regionItems, 3);
#endregion
break;
case 3:
#region 街道
source.Retset();
source.SetTextForeground(source.tb_Region, false);
source.SetTextForeground(source.tb_Street, true);
var streetItems = source.Region.children.GroupBy(a => a.pinyinPrefix).ToList();
source.LoadData(streetItems, 0);
#endregion
break;
default:
break;
}
source.addressPopup.IsOpen = true;
}
private void LoadData(List> items, int type)
{
// var items = source.Source.GroupBy(a => a.pinyinPrefix).ToList();
for (int i = 0; i < items.Count; i++)
{
container.RowDefinitions.Add(new RowDefinition());
var item = items[i];
TextBlock textBlock = new TextBlock();
textBlock.Text = item.Key.ToUpper();
textBlock.SetValue(Grid.RowProperty, i);
textBlock.SetValue(Grid.ColumnProperty, 0);
textBlock.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#9B9B9B"));
textBlock.Margin = new Thickness(20, 6, 0, 0);
textBlock.FontSize = 12;
textBlock.FontFamily = tb_Defualt.FontFamily;
container.Children.Add(textBlock);
if (item.Count() == 0)
continue;
StackPanel stackPanel = new StackPanel();
//stackPanel.Background = new SolidColorBrush((Colors.Red));
stackPanel.Margin = new Thickness(3, 1, 0, 0);
stackPanel.SetValue(Grid.RowProperty, i);
stackPanel.SetValue(Grid.ColumnProperty, 1);
stackPanel.Orientation = Orientation.Vertical;
container.Children.Add(stackPanel);
foreach (var itemChild in item)
{
TextBlock itemTextBlock = new TextBlock();
itemTextBlock.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D4D4D"));
itemTextBlock.Margin = new Thickness(0, 5, 0, 0);
itemTextBlock.FontSize = 12;
itemTextBlock.FontFamily = tb_Defualt.FontFamily;
itemTextBlock.FontWeight = FontWeights.Medium;
itemTextBlock.Text = itemChild.extName;
itemTextBlock.MouseEnter += ItemTextBlock_MouseEnter;
itemTextBlock.MouseLeave += ItemTextBlock_MouseLeave;
itemTextBlock.MouseLeftButtonDown += (s, e) =>
{
switch (type)
{
case 1:
Province = itemChild;
break;
case 2:
City = itemChild;
break;
case 3:
Region = itemChild;
break;
case 0:
addressPopup.IsOpen = false;
break;
default:
break;
}
SetText(itemChild.extName, itemChild.deep);
};
stackPanel.Children.Add(itemTextBlock);
}
}
}
private void ItemTextBlock_MouseLeave(object sender, MouseEventArgs e)
{
TextBlock? textBlock = sender as TextBlock;
if (textBlock != null)
textBlock.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D4D4D"));
}
private void ItemTextBlock_MouseEnter(object sender, MouseEventArgs e)
{
TextBlock? textBlock = sender as TextBlock;
if (textBlock != null)
textBlock.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D75AC"));
}
public void Retset()
{
container.RowDefinitions.Clear();
container.ColumnDefinitions.Clear();
ColumnDefinition one = new ColumnDefinition();
one.Width = new GridLength(35d);
container.ColumnDefinitions.Add(one);
container.ColumnDefinitions.Add(new ColumnDefinition());
container.Children.Clear();
}
public void SetText(string text, int level)
{
tb_Defualt.Visibility = Visibility.Collapsed;
switch (level)
{
case 0:
currentImageState = true;
ChangeImageState();
tb_Province.Text = text;
tb_City.Text = "请选择城市";
tb_Region.Text = "";
tb_Street.Text = "";
break;
case 1:
currentImageState = true;
ChangeImageState();
tb_City.Text = text;
tb_Region.Text = "请选择县级";
tb_Street.Text = "";
break;
case 2:
currentImageState = true;
ChangeImageState();
tb_Region.Text = text;
tb_Street.Text = "请选择街道";
break;
case 3:
currentImageState = true;
ChangeImageState();
tb_Street.Text = text;
AddressProperty = tb_Province.Text + "|" + tb_City.Text + "|" + tb_Region.Text + "|" + tb_Street.Text;
break;
default:
break;
}
if (level == 3)
{
Level = -1;
SetTextForeground(tb_Street, false);
}
else
Level = level + 1;
}
public void SetTextForeground(TextBlock tb, bool isBlue)
{
if (isBlue)
tb.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D75AC"));
else
tb.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#4D4D4D"));
}
private void tb_Province_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
tb_Defualt.Visibility = Visibility.Collapsed;
tb_Province.Text = "请选择省份";
tb_City.Text = "";
tb_Region.Text = "";
tb_Street.Text = "";
Level = 0;
e.Handled = true;
}
private void tb_City_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
tb_City.Text = "请选择城市";
tb_Region.Text = "";
tb_Street.Text = "";
Level = 1;
e.Handled = true;
}
private void tb_Region_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
tb_Region.Text = "请选择县级";
tb_Street.Text = "";
Level = 2;
e.Handled = true;
}
private void tb_Street_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
tb_Street.Text = "请选择街道";
Level = 3;
e.Handled = true;
}
private void tb_Defualt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//tb_Defualt.Visibility = Visibility.Collapsed;
//tb_Province.Text = "请选择省份";
//Level = 0;
}
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
tb_Defualt.Visibility = Visibility.Collapsed;
tb_Province.Text = "请选择省份";
tb_City.Text = "";
tb_Region.Text = "";
tb_Street.Text = "";
Level = 0;
}
private void ChangeImageState()
{
if (currentImageState)
img.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/closeX.png", UriKind.Absolute));
else
img.Source = new BitmapImage(new Uri("pack://application:,,,/ivf_tl_Manage;component/Resources/Images/PullDownBlueIcon.png", UriKind.Absolute));
}
private bool currentImageState = false;
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
private void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (Level == -1 && addressPopup.IsOpen)
{
currentImageState = true;
ChangeImageState();
}
else
{
currentImageState = false;
ChangeImageState();
}
}
}
}