Prechádzať zdrojové kódy

修复了布局管理

luo 11 mesiacov pred
rodič
commit
5746c50b3e

+ 12 - 2
ShakerManger/MainWindow.xaml

@@ -293,14 +293,24 @@
                 Content="{Binding CurrentMainPage.View, Converter={StaticResource Type2ViewConverter}}"
                 IsEnabled="{Binding IsEnabled}" />
 
-
+            <Popup
+                Grid.Row="0"
+                Grid.RowSpan="2"
+                Grid.ColumnSpan="2"
+                AllowsTransparency="True"
+                IsOpen="{Binding Source={StaticResource MainViewModel}, Path=Data.Shakers.SelectedIsOpen}"
+                Placement="Mouse"
+                PopupAnimation="Slide"
+                StaysOpen="False">
+                <view:ShakerSelectView DataContext="{Binding Source={StaticResource MainViewModel}, Path=Data.Shakers}" />
+            </Popup>
             <Popup
                 Grid.Row="0"
                 Grid.RowSpan="2"
                 Grid.ColumnSpan="2"
                 AllowsTransparency="True"
                 IsOpen="{Binding Source={StaticResource ColorPicker}, Path=IsOpen}"
-                Placement="Center"
+                Placement="Mouse"
                 PopupAnimation="Slide"
                 StaysOpen="True">
                 <view:ColorPickerView DataContext="{Binding Source={StaticResource ColorPicker}}" />

+ 3 - 0
ShakerManger/ShakerManger.csproj.user

@@ -51,6 +51,9 @@
     <Page Update="View\ShakerPageView.xaml">
       <SubType>Designer</SubType>
     </Page>
+    <Page Update="View\ShakerSelectView.xaml">
+      <SubType>Designer</SubType>
+    </Page>
     <Page Update="View\SqlPageView.xaml">
       <SubType>Designer</SubType>
     </Page>

+ 125 - 0
ShakerManger/View/ShakerSelectView.xaml

@@ -0,0 +1,125 @@
+<UserControl
+    x:Class="ShakerManger.View.ShakerSelectView"
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:local="clr-namespace:ShakerManger.View"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    xmlns:vm="clr-namespace:ShakerManger.ViewModel"
+    Width="200"
+    Height="320"
+    d:DataContext="{d:DesignInstance {x:Type vm:ShakersViewModel}}"
+    d:DesignHeight="450"
+    d:DesignWidth="800"
+    mc:Ignorable="d">
+    <Border
+        Background="{StaticResource SystemItemsSelectBackColor}"
+        BorderBrush="{StaticResource BorderBrush}"
+        BorderThickness="1"
+        CornerRadius="6">
+        <Grid>
+            <Grid.RowDefinitions>
+                <RowDefinition Height="46" />
+                <RowDefinition Height="*" />
+            </Grid.RowDefinitions>
+            <Border Background="{StaticResource TitleBackColor}" CornerRadius="6,6,0,0">
+                <TextBlock
+                    HorizontalAlignment="Center"
+                    VerticalAlignment="Center"
+                    FontSize="18"
+                    Foreground="White"
+                    Text="振动台选择" />
+            </Border>
+            <ListView
+                Grid.Row="1"
+                Margin="0,10,0,0"
+                VerticalAlignment="Top"
+                ItemsSource="{Binding ShakerSelectInfos}"
+                SelectedItem="{Binding SelectedInfo}">
+                <ListView.Style>
+                    <Style TargetType="ListView" BasedOn="{x:Null}">
+                        <Setter Property="Background" Value="Transparent" />
+                        <Setter Property="BorderBrush" Value="Transparent" />
+                        <Setter Property="VerticalContentAlignment" Value="Stretch" />
+                        <Setter Property="Padding" Value="0" />
+                        <Setter Property="Template">
+                            <Setter.Value>
+                                <ControlTemplate TargetType="ListView">
+                                    <ScrollViewer
+                                        HorizontalScrollBarVisibility="Hidden"
+                                        SnapsToDevicePixels="True"
+                                        VerticalScrollBarVisibility="Auto">
+                                        <ItemsPresenter Margin="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
+                                    </ScrollViewer>
+                                </ControlTemplate>
+                            </Setter.Value>
+                        </Setter>
+                        <Setter Property="ItemTemplate">
+                            <Setter.Value>
+                                <DataTemplate>
+                                    <Border
+                                        x:Name="border"
+                                        BorderBrush="BlueViolet"
+                                        BorderThickness="0">
+                                        <TextBlock
+                                            Margin="20,0,0,0"
+                                            VerticalAlignment="Center"
+                                            FontSize="14"
+                                            Foreground="White"
+                                            IsEnabled="{Binding IsEnble}"
+                                            Text="{Binding Name}" />
+                                    </Border>
+                                    <DataTemplate.Triggers>
+                                        <Trigger Property="IsMouseOver" Value="True">
+                                            <Setter TargetName="border" Property="BorderThickness" Value="1" />
+                                        </Trigger>
+                                        <DataTrigger Binding="{Binding Path=IsEnble}" Value="False">
+                                            <Setter TargetName="border" Property="OpacityMask" Value="#8FAAAAAA" />
+                                        </DataTrigger>
+                                    </DataTemplate.Triggers>
+                                </DataTemplate>
+                            </Setter.Value>
+                        </Setter>
+                        <Setter Property="ItemContainerStyle">
+                            <Setter.Value>
+                                <Style TargetType="ListViewItem">
+                                    <Setter Property="Height" Value="46" />
+                                    <Setter Property="Background" Value="Transparent" />
+                                    <Setter Property="BorderBrush" Value="Transparent" />
+                                    <Setter Property="BorderThickness" Value="0" />
+                                    <Setter Property="SnapsToDevicePixels" Value="True" />
+                                    <Setter Property="OverridesDefaultStyle" Value="True" />
+                                    <Setter Property="Cursor" Value="Hand" />
+                                    <Setter Property="IsEnabled" Value="{Binding IsEnble}" />
+                                    <Setter Property="Padding" Value="0" />
+                                    <Setter Property="Template">
+                                        <Setter.Value>
+                                            <ControlTemplate TargetType="ListViewItem">
+                                                <ContentPresenter
+                                                    Margin="0"
+                                                    Content="{TemplateBinding Content}"
+                                                    ContentStringFormat="{TemplateBinding ContentStringFormat}"
+                                                    ContentTemplate="{TemplateBinding ContentTemplate}"
+                                                    ContentTemplateSelector="{x:Null}" />
+                                            </ControlTemplate>
+                                        </Setter.Value>
+                                    </Setter>
+                                </Style>
+                            </Setter.Value>
+                        </Setter>
+                        <Setter Property="ItemsPanel">
+                            <Setter.Value>
+                                <ItemsPanelTemplate>
+                                    <UniformGrid
+                                        Margin="0"
+                                        Columns="1"
+                                        Style="{x:Null}" />
+                                </ItemsPanelTemplate>
+                            </Setter.Value>
+                        </Setter>
+                    </Style>
+                </ListView.Style>
+            </ListView>
+        </Grid>
+    </Border>
+</UserControl>

+ 28 - 0
ShakerManger/View/ShakerSelectView.xaml.cs

@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+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 ShakerManger.View
+{
+    /// <summary>
+    /// ShakerSelectView.xaml 的交互逻辑
+    /// </summary>
+    public partial class ShakerSelectView : UserControl
+    {
+        public ShakerSelectView()
+        {
+            InitializeComponent();
+        }
+    }
+}

+ 9 - 10
ShakerManger/View/ShakerView.xaml

@@ -157,17 +157,16 @@
                         Margin="20,0,0,0"
                         hc:IconElement.Geometry="m840.336,401.168c-12.736,-12.736 -33.408,-12.736 -46.144,0l-346.176,346.176l-346.176,-346.176c-12.736,-12.736 -33.408,-12.736 -46.144,0l-46.144,46.144c-12.736,12.736 -12.736,33.408 0,46.144l415.36,415.36c12.736,12.736 33.408,12.736 46.144,0l415.36,-415.36c12.736,-12.736 12.736,-33.408 0,-46.144l-46.08,-46.144zm-415.424,116.096c12.736,12.736 33.408,12.736 46.144,0l415.36,-415.36c12.736,-12.736 12.736,-33.408 0,-46.144l-46.08,-46.208c-12.736,-12.736 -33.408,-12.736 -46.144,0l-346.176,346.176l-346.176,-346.176c-12.736,-12.736 -33.408,-12.736 -46.144,0l-46.144,46.144c-12.736,12.736 -12.736,33.408 0,46.144l415.36,415.424z"
                         hc:IconElement.Width="{StaticResource buttonwidth}"
-                        Command="{Binding SelectShakerCommand}"
                         Cursor="Hand"
                         RenderTransformOrigin="0.5,0.5"
-                        Style="{StaticResource ImageButton}" />
-                    <Popup
-                        AllowsTransparency="True"
-                        IsOpen="{Binding IsOpen}"
-                        Placement="Bottom"
-                        PlacementTarget="{Binding ElementName=shakerselct}"
-                        PopupAnimation="Slide"
-                        StaysOpen="False" />
+                        Style="{StaticResource ImageButton}">
+                        <hc:Interaction.Triggers>
+                            <hc:EventTrigger EventName="Click">
+                                <hc:ExInvokeCommandAction Command="{Binding Source={StaticResource MainViewModel}, Path=Data.Shakers.SelectShakerCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}, Path=DataContext}" />
+                            </hc:EventTrigger>
+                        </hc:Interaction.Triggers>
+                    </Button>
+
                     <Button
                         x:Name="control"
                         Grid.Column="6"
@@ -248,7 +247,7 @@
                         </Button.Style>
                         <hc:Interaction.Triggers>
                             <hc:EventTrigger EventName="Click">
-                                <hc:ExInvokeCommandAction Command="{Binding MaxOrMinCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}, Path=DataContext}" />
+                                <hc:ExInvokeCommandAction Command="{Binding Source={StaticResource MainViewModel}, Path=Data.Shakers.MaxOrMinCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}, Path=DataContext}" />
                             </hc:EventTrigger>
                         </hc:Interaction.Triggers>
                     </Button>

+ 1 - 1
ShakerManger/View/ShakersView.xaml

@@ -37,7 +37,7 @@
                 <Setter Property="ItemTemplate">
                     <Setter.Value>
                         <DataTemplate>
-                            <ContentControl Content="{Binding View, Converter={StaticResource Type2ViewConverter}, ConverterParameter='1'}" />
+                            <ContentControl Content="{Binding Content}" />
                         </DataTemplate>
                     </Setter.Value>
                 </Setter>

+ 4 - 0
ShakerManger/ViewModel/MainWindowViewModel.cs

@@ -39,6 +39,10 @@ namespace ShakerManger.ViewModel
             {
                 CurrentMainPage = MainPages[0];
             }
+            LayoutManger.Close = () =>
+            {
+                Shakers.Layout = LayoutManger.Selected;
+            };
         }
         public DataMangerViewModel DataManger { get; init; }
         public LogMangerViewModel LogManger { get; init; }

+ 6 - 15
ShakerManger/ViewModel/ShakerControlViewModel.cs

@@ -92,10 +92,11 @@ namespace ShakerManger.ViewModel
                 }
             }
         }
+
+
         public bool IsConnected { get => isConnected; set =>UpdateProperty(ref isConnected, value); }
         public string[] AnalogNames { get; } = new string[12];
         public string[] Units { get; } = new string[12];
-        public bool ShakerControlIsOpen { get => shakerControlIsOpen; set =>UpdateProperty(ref shakerControlIsOpen, value); }
         private Dictionary<string, string> Properties { get; init; } = new Dictionary<string, string>();
 
         public ObservableCollection<PlotConfigViewModel> PlotConfig { get; } = new ObservableCollection<PlotConfigViewModel>();
@@ -323,6 +324,7 @@ namespace ShakerManger.ViewModel
                 }
             }, $"{Shaker.Model.GlobalVariable.ShakerIDKey}='{Model.Id}'");
         }
+        public View.ShakerView Content { get; } = new View.ShakerView();
         public bool GainSwich { get => gainSwich; set =>UpdateProperty(ref gainSwich, value); }
         public override Type View { get; } = typeof(View.ShakerView);
         public ICommand OpenCommand => new DelegateCommand(Open);
@@ -330,23 +332,12 @@ namespace ShakerManger.ViewModel
         public ICommand StartCommand => new DelegateCommand(Start);
         public ICommand StopCommand => new DelegateCommand(Stop);
         public ICommand ResetCommand => new DelegateCommand(Reset);
-        private ShakerSelectInfoViewModel selectedInfo = new ShakerSelectInfoViewModel(0);
         private bool isOpen = false;
-        public ShakerSelectInfoViewModel SelectedInfo
-        {
-            get => selectedInfo;
-            set
-            {
-                if (IsOpen)
-                {
-                    UpdateProperty(ref selectedInfo, value);
-                    IsOpen = false;
-                }
-            }
-        }
+
+        public bool ShakerControlIsOpen { get => shakerControlIsOpen; set => UpdateProperty(ref shakerControlIsOpen, value); }
         public OxyPlot.PlotModel PlotModel { get; } = new OxyPlot.PlotModel();
-        public ObservableCollection<ShakerSelectInfoViewModel> ShakerSelectInfos { get; } = new ObservableCollection<ShakerSelectInfoViewModel>();
         public bool IsOpen { get => isOpen; set => UpdateProperty(ref isOpen, value); }
+
         private async void Close()
         {
             GetEvent<Shaker.Model.LogModel>().Publish(this, new Shaker.Model.LogModel($"振动台{Name}关闭功放"));

+ 98 - 37
ShakerManger/ViewModel/ShakersViewModel.cs

@@ -23,7 +23,6 @@ namespace ShakerManger.ViewModel
 
         public ShakersViewModel()
         {
-            
         }
 
         public LayoutViewModel Layout 
@@ -36,11 +35,17 @@ namespace ShakerManger.ViewModel
                 {
                     if (value.ColumnCount == 1 && value.RowCount == 1)
                     {
-                        LayoutShakers[0].IsMax = true;
+                        for(int i=0;i<LayoutShakers.Count;i++)
+                        {
+                            LayoutShakers[i].IsMax = i==0;
+                        }
                     }
                     else
                     {
-                        LayoutShakers[0].IsMax = false;
+                        for (int i = 0; i < LayoutShakers.Count; i++)
+                        {
+                            LayoutShakers[i].IsMax = false;
+                        }
                     }
                 }
             }
@@ -79,38 +84,7 @@ namespace ShakerManger.ViewModel
                 MainWindowViewModel.Default.LogManger.Shakers.Add(new KeyValuePair<string, string>(x.Description, x.Id));
                 MainWindowViewModel.Default.WarnManger.Shakers.Add(new KeyValuePair<string, string>(x.Description, x.Id));
                 Shakers.Add(new ShakerControlViewModel(x));
-                Shakers[^1].PropertyChanged += (sender, args) =>
-                {
-                    if (sender is ShakerControlViewModel vm  && args.PropertyName == nameof(ShakerControlViewModel.SelectedInfo))
-                    {
-                        int index = -1;
-                        for (int i = 0; i < LayoutShakers.Count; i++)
-                        {
-                            if (LayoutShakers[i].Name == vm.Name)
-                            {
-                                index = i;
-                                break;
-                            }
-                        }
-                        if (index == -1) return;
-                        var shaker = Shakers[vm.SelectedInfo.ShakerIndex];
-                        var tempshaker = LayoutShakers[index];
-                        int srcindex = -1;
-                        for (int i = 0; i < LayoutShakers.Count; i++)
-                        {
-                            if (LayoutShakers[i].Name == shaker.Name)
-                            {
-                                srcindex = i;
-                                break;
-                            }
-                        }
-                        LayoutShakers[index] = shaker;
-                        if (srcindex >= 0)
-                        {
-                            LayoutShakers[srcindex] = tempshaker;
-                        }
-                    }
-                };
+
                 LayoutShakers.Add(Shakers[^1]);
             });
             MainWindowViewModel.Default.SystemConfig.ShakerPage.CurrentShaker = MainWindowViewModel.Default.Shakers.Shakers.First();
@@ -123,7 +97,7 @@ namespace ShakerManger.ViewModel
         {
             if(parameter.Parameter is ShakerControlViewModel shaker)
             {
-                var tempshaker = Shakers.First(x => x.Name == shaker.Name);
+                var tempshaker = Shakers.First(x => x.Model.Id == shaker.Model.Id);
                 if(tempshaker.IsMax)
                 {
                     LayoutShakers.Clear();
@@ -178,10 +152,92 @@ namespace ShakerManger.ViewModel
                 LayoutShakers.Add(item);
             }
         }
+        public ICommand SelectShakerCommand => new DelegateCommand<ExCommandParameter>(SelectShaker);
+        private void SelectShaker(ExCommandParameter parameter)
+        {
+            if (parameter.Parameter is ShakerControlViewModel shaker)
+            {
+                ShakerSelectInfos.Clear();
+                for (int i = 0; i < MainWindowViewModel.Default.Shakers.Shakers.Count; i++)
+                {
+                    var model = MainWindowViewModel.Default.Shakers.Shakers[i].Model;
+
+                    ShakerSelectInfos.Add(new ShakerSelectInfoViewModel(i,model.Id)
+                    {
+                        Name = model.Description,
+                        IsEnble = model.Id != shaker.Model.Id,
+                    });
+                    if (!ShakerSelectInfos[^1].IsEnble)
+                    {
+                        SelectedInfo = ShakerSelectInfos[^1];
+                    }
+                }
+                SelectedIsOpen = true;
+            }
+        }
+
+        public bool SelectedIsOpen { get => selectedIsOpen; set => UpdateProperty(ref selectedIsOpen, value); }
+
+        private bool selectedIsOpen = false;
+
+        private ShakerSelectInfoViewModel? selectedInfo = null;
+        public ShakerSelectInfoViewModel? SelectedInfo
+        {
+            get => selectedInfo;
+            set
+            {
+                if(selectedInfo ==null)
+                {
+                    UpdateProperty(ref selectedInfo, value);
+                    SelectedIsOpen = false;
+                    return;
+                }
+                if(value ==null)
+                {
+                    UpdateProperty(ref selectedInfo, value);
+                    SelectedIsOpen = false;
+                    return;
+                }
+                if (SelectedIsOpen)
+                {
+
+                    int index = -1;
+                    for (int i = 0; i < LayoutShakers.Count; i++)
+                    {
+                        if (LayoutShakers[i].Model.Id == value.Id)
+                        {
+                            index = i;
+                            break;
+                        }
+                    }
+                    if (index == -1) return;
+                    var shaker = Shakers[selectedInfo.ShakerIndex];
+                    var tempshaker = LayoutShakers[index];
+                    int srcindex = -1;
+                    for (int i = 0; i < LayoutShakers.Count; i++)
+                    {
+                        if (LayoutShakers[i].Name == shaker.Name)
+                        {
+                            srcindex = i;
+                            break;
+                        }
+                    }
+                    LayoutShakers[index] = shaker;
+                    if (srcindex >= 0)
+                    {
+                        LayoutShakers[srcindex] = tempshaker;
+                    }
+                    UpdateProperty(ref selectedInfo, value);
+                    SelectedIsOpen = false;
+                }
+            }
+        }
         public bool IsSelected { get; set; }
         public Data.MainPageType MainPageType => Data.MainPageType.ShakerManger;
+
         public string IconPath { get; } = (string)App.Current.FindResource("ShakerIcon");
         public override Type View { get; } = (typeof(View.ShakersView));
+        public ObservableCollection<ShakerSelectInfoViewModel> ShakerSelectInfos { get; } = new ObservableCollection<ShakerSelectInfoViewModel>();
         /// <summary>
         /// 布局用,根据布局的变化会自动变化
         /// </summary>
@@ -192,10 +248,15 @@ namespace ShakerManger.ViewModel
     }
     internal class ShakerSelectInfoViewModel : Shaker.ViewModel.ViewModelBase<Shaker.Model.ModelBase>
     {
-        public ShakerSelectInfoViewModel(int index) => ShakerIndex = index;
+        public ShakerSelectInfoViewModel(int index, string id)
+        {
+            ShakerIndex = index;
+            Id = id;
+        }
         private bool isEnble = true;
         private string name = string.Empty;
 
+        public string Id { get; }
         public int ShakerIndex { get; }
         public bool IsEnble { get => isEnble; set => UpdateProperty(ref isEnble, value); }
         public string Name { get => name; set => UpdateProperty(ref name, value); }