m0_58844937 2024-05-20 21:42 采纳率: 94.3%
浏览 4
已结题

我该如何修改以下代码实现,我按用户控件的最大化按钮时,用户控件占满整个主窗口,点击最小化按钮时回归原来的大小

我该如何修改以下代码实现,我按用户控件的最大化按钮时,用户控件占满整个主窗口,点击最小化按钮时回归原来的大小
用户控件的xmal代码如下:

<UserControl x:Class="IZeroVisionVM.LineChart_UserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" Loaded="UserControl_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" HorizontalAlignment="Right">
            <DockPanel DockPanel.Dock="Right" Margin="-1,0,5,0" >
                <Button Name="btnMinimize" Width="21" Height="16" Margin="0,0,5,0" Padding="0" BorderThickness="0" Click="BtnMinimize_Click">
                    <materialDesign:PackIcon Kind="WindowMinimize"/>
                </Button>
                <Button Name="btnMaximize" Width="21" Height="16" Margin="0,0,0,0" Padding="0" BorderThickness="0" Click="BtnMaximize_Click">
                    <materialDesign:PackIcon Kind="WindowMaximize"/>
                </Button>
            </DockPanel>
        </Grid>
        <Grid Grid.Row="1">
            <StackPanel Orientation="Vertical" Name="LargeWindowContent" Margin="10" Visibility="Collapsed">
                <StackPanel Orientation="Horizontal">
                    <Label Content="上限:"></Label>
                    <TextBox Text="{Binding IP}" Width="160" BorderThickness="0,0,0,1" BorderBrush="White"></TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="警告线:"></Label>
                    <TextBox Text="{Binding IP}" Width="160" BorderThickness="0,0,0,1" BorderBrush="White"></TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="规格:"></Label>
                    <TextBox Text="{Binding IP}" Width="160" BorderThickness="0,0,0,1" BorderBrush="White"></TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="警告线:"></Label>
                    <TextBox Text="{Binding Port}" Width="160" BorderThickness="0,0,0,1" BorderBrush="White"></TextBox>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="下限:"></Label>
                    <TextBox Text="{Binding Port}" Width="160" BorderThickness="0,0,0,1" BorderBrush="White"></TextBox>
                </StackPanel>
            </StackPanel>
            <Button Content="修改" Margin="0,10,5,5" HorizontalAlignment="Right" VerticalAlignment="Top" Click="Button_Click_1"></Button>
            <Button Content="保存" Margin="0,10,80,5" HorizontalAlignment="Right" VerticalAlignment="Top" Click="Button_Click"></Button>
        </Grid>
        <Grid Grid.Row="2">
            <!-- 第一个内容区域,适用于较小的窗口宽度 -->
            <StackPanel Orientation="Vertical" Name="SmallWindowContent" Margin="10">
                <lvc:CartesianChart x:Name="MyChart" Series="{Binding LineSeriesCollection}" LegendLocation="Right" Height="347" Margin="0,0,10,0">
                    <lvc:CartesianChart.AxisX>
                        <lvc:Axis Labels="{Binding Labels}" FontSize="14">
                            <lvc:Axis.Separator>
                                <lvc:Separator Step="1" />
                            </lvc:Axis.Separator>
                        </lvc:Axis>
                    </lvc:CartesianChart.AxisX>
                    <lvc:CartesianChart.AxisY>
                        <lvc:Axis LabelFormatter="{Binding CustomFormatterY}">
                        </lvc:Axis>
                    </lvc:CartesianChart.AxisY>
                </lvc:CartesianChart>
            </StackPanel>
        </Grid>
    </Grid>
</UserControl>

后台代码如下:


using LiveCharts;
using LiveCharts.Wpf;
using Rapid.UI.IZVKernal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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 IZeroVisionVM
{
    /// <summary>
    /// LeftOutSide_UserControl.xaml 的交互逻辑
    /// </summary>
    public partial class LineChart_UserControl : UserControl , INotifyPropertyChanged
    {
        public List<string> Labels { get; set; }
        public SeriesCollection LineSeriesCollection { get; set; }
        public Func<double, string> CustomFormatterY { get; set; }

        public LineSeries lineSeries_上限;
        public LineSeries lineSeries_上警告线;
        public LineSeries lineSeries_标准;
        public LineSeries lineSeries_下警告线;
        public LineSeries lineSeries_下限;
        public LineSeries lineseries;

        //private double axisYMax;
        //public double AxisYMax
        //{
        //    get { return axisYMax; }
        //    set { axisYMax = value; this.RaisePropertyChanged("AxisYMax"); }
        //}
        //private double axisYMin;
        //public double AxisYMin
        //{
        //    get { return axisYMin; }
        //    set { axisYMin = value; this.RaisePropertyChanged("AxisYMin"); }
        //}

        public LineChart_UserControl()
        {
            InitializeComponent();
        }

        //绑定的X轴数据
        public static ChartValues<double> ValueList { get; set; }
        public void OnClick(float distance)
        {
            //向图表中添加数据
            ValueList.Add(distance);
            //这里如果移除数组,图表曲线会原地起伏,就没有X轴移动的动画效果了
            if (ValueList.Count > 11)
            {
                ValueList.RemoveAt(0);
            }
        }

        private Size initialSize;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Mode();
            this.DataContext = this;
        }

        private string CustomFormattersY(double val)
        {
            return string.Format("{0} mm", val);
        }

        private void UpdateChartAxes(int max, int min)
        {
            var yMax = max;
            var yMin = min;
            System.Windows.Application.Current.Dispatcher.BeginInvoke((Action)(() =>
            {
                MyChart.AxisY[0].MaxValue = yMax;
                MyChart.AxisY[0].MinValue = yMin;
            }));
        }

        private void UpdateChartLineSeries(int index, double item, LineSeries lineSeries0)
        {
            switch (index)
            {
                case 0:
                    try
                    {
                        LineSeriesCollection.Remove(lineSeries0);
                        //LineSeries lineSeries1 = lineSeries_限制(item, "上限");
                        //LineSeriesCollection.Insert(index, lineSeries1);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show($"aa,{e}");
                    }
                    break;
                case 1:
                    try
                    {
                        LineSeriesCollection.Remove(lineSeries0);
                        LineSeries lineSeries1 = lineSeries_预警(item);
                        LineSeriesCollection.Insert(index, lineSeries1);
                    }
                    catch
                    {
                    }
                    break;
                case 5:
                    try
                    {
                        System.Windows.Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            UpdateChartAxes(800, 200);
                            //LineSeriesCollection.Remove(lineSeries0);
                            //LineSeries lineSeries1 = lineSeries_预警(item);
                            //LineSeriesCollection.Insert(index, lineSeries1);
                        }));
                    }
                    catch(Exception e)
                    {
                        MessageBox.Show($"aa,{e}");
                    }
                    break;
                default:
                    break;
            }
            
        }

        public void Mode()
        {
            Labels = new List<string>();
            double max = 420;
            double warningMax = 400;
            double guige = 390;
            double warningMin = 380;
            double min = 370;
            LineChart_UserControl.ValueList = new ChartValues<double>();
            LineSeriesCollection = new SeriesCollection();

            CustomFormatterY = CustomFormattersY;

            lineSeries_上限 = lineSeries_限制(max, "上限");
            LineSeriesCollection.Add(lineSeries_上限);

            lineSeries_上警告线 = lineSeries_预警(warningMax);
            LineSeriesCollection.Add(lineSeries_上警告线);

            lineSeries_标准 = lineSeries_规格(guige);
            LineSeriesCollection.Add(lineSeries_标准);

            lineSeries_下警告线 = lineSeries_预警(warningMin);
            LineSeriesCollection.Add(lineSeries_下警告线);

            lineSeries_下限 = lineSeries_限制(min, "下限");
            LineSeriesCollection.Add(lineSeries_下限);

            lineseries = new LineSeries();
            lineseries.Title = "极耳中心距";
            //lineseries.DataLabels = true; // 点位置显示值
            lineseries.Values = LineChart_UserControl.ValueList;
            LineSeriesCollection.Add(lineseries);
        }

        private LineSeries lineSeries_限制(double max, string name)
        {
            LineSeries lineseries1 = new LineSeries();
            lineseries1.Title = $"{name}{max} mm";
            lineseries1.Values = new ChartValues<double> { max, max, max, max, max, max, max, max, max, max, max };
            lineseries1.Stroke = System.Windows.Media.Brushes.Red;
            lineseries1.PointGeometry = null;
            lineseries1.Fill = System.Windows.Media.Brushes.LightBlue;
            return lineseries1;
        }

        private LineSeries lineSeries_预警(double warningMax)
        {
            LineSeries lineseries2 = new LineSeries();
            lineseries2.Title = $"预警线:{warningMax} mm";
            lineseries2.StrokeDashArray = new System.Windows.Media.DoubleCollection { 2 };
            lineseries2.Values = new ChartValues<double> { warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax };
            lineseries2.Stroke = System.Windows.Media.Brushes.Blue;
            lineseries2.PointGeometry = null;
            lineseries2.Fill = System.Windows.Media.Brushes.LightBlue;
            return lineseries2;
        }

        private LineSeries lineSeries_规格(double guige)
        {
            LineSeries lineseries2 = new LineSeries();
            lineseries2.Title = $"规格:{guige} mm";
            lineseries2.Values = new ChartValues<double> { guige, guige, guige, guige, guige, guige, guige, guige, guige, guige, guige };
            lineseries2.Stroke = System.Windows.Media.Brushes.Green;
            lineseries2.PointGeometry = null;
            lineseries2.Fill = System.Windows.Media.Brushes.LightBlue;
            return lineseries2;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void BtnMinimize_Click(object sender, RoutedEventArgs e)
        {
            this.Height = 300;
            this.Width = 200;
        }

        private void BtnMaximize_Click(object sender, RoutedEventArgs e)
        {
            // 获取屏幕的工作区大小
            Rect workingArea = System.Windows.SystemParameters.WorkArea;

            // 设置用户控件的宽度和高度与屏幕工作区一致
            Width = workingArea.Width;
            Height = workingArea.Height;

            // 设置用户控件的位置与屏幕工作区的左上角一致
            Margin = new Thickness(workingArea.Left, workingArea.Top, 0, 0);

            // 将用户控件置于最前端
            BringIntoView();
        }
    }
}

MainWindow窗口添加用户控件的xaml代码:

<Grid Grid.Row="0" Grid.Column="2" Margin="0,0,1,1">
                    <local:LineChart_UserControl x:Name="LeftOutSideMargin"></local:LineChart_UserControl>
                </Grid>
  • 写回答

4条回答 默认 最新

  • 百锦再@新空间代码工作室 全栈领域优质创作者 2024-05-20 21:44
    关注

    你需要在用户控件的代码中添加一些逻辑来实现最大化和最小化按钮的功能。下面是你可以修改的代码示例:

    首先,在LineChart_UserControl类中添加一个IsMaximized属性,用于跟踪用户控件的最大化状态:

    public bool IsMaximized { get;
    set;
    }

    然后在BtnMaximize_Click事件处理程序中,根据IsMaximized属性的值来处理最大化和还原操作:

    private void BtnMaximize_Click(object sender, RoutedEventArgs e){if (IsMaximized){// 还原用户控件的原始大小this.Width = initialSize.Width;
    this.Height = initialSize.Height;
    this.Margin = new Thickness(workingArea.Left, workingArea.Top, 0, 0);
    }else{// 最大化用户控件this.Width = workingArea.Width;
    this.Height = workingArea.Height;
    this.Margin = new Thickness(0);
    }
    IsMaximized = !IsMaximized;
    }

    最后,在`BtnMin


    有问题你别着急,评论留言都可以,看到马上就回复,尽量及时补充齐
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月14日
  • 已采纳回答 8月6日
  • 创建了问题 5月20日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀