我该如何修改以下代码实现,我按用户控件的最大化按钮时,用户控件占满整个主窗口,点击最小化按钮时回归原来的大小;
遇到的问题:点击最大化按钮,用户控件只能在网格grid里面最大化,无法突破网格,这该如何解决呀
用户控件的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代码:
Height="760" Width="1360" WindowStyle="None" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="2" Margin="0,0,1,1">
<local:LineChart_UserControl x:Name="LeftOutSideMargin"></local:LineChart_UserControl>
</Grid>
主窗口的后台代码中设置了主窗口的大小:
// 窗口工作区域大小
this.Height = SystemParameters.WorkArea.Height-20;
this.Width = SystemParameters.WorkArea.Width-20;