首页 > 解决方案 > 如果我有来自 LiveCharts 的图表,我无法将元素添加到我的 GUI

问题描述

现在图表不能再调整大小了,我认为这与RowDefinitions (*) 有关,我知道这会占用剩余空间,但我想在图表下方放置一些其他内容。如果我理解正确,您必须按照您想要元素的顺序放置行和列

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <StackPanel Grid.Row="0" Grid.Column="0"
          Orientation="Horizontal"
          Margin="0,0,12,0">
        <StackPanel>
            <TextBlock Text="ComboBox1 Label" />
            <ComboBox />
        </StackPanel>
        <StackPanel>
            <TextBlock Text="ComboBox2 Label" />
            <ComboBox />
        </StackPanel>
    </StackPanel>

    <StackPanel Grid.Row="0" Grid.Column="1"
          Orientation="Horizontal"
          VerticalAlignment="Bottom"
          Margin="0,0,12,0">
        <RadioButton Content="Text" />
        <RadioButton Content="Text" />
        <RadioButton Content="Text" />
    </StackPanel>

    <StackPanel Grid.Row="1" Grid.Column="2"
          HorizontalAlignment="Left">
        <TextBlock Text="Button Label" />
        <Button Content="Text" />
    </StackPanel>

    <!--CartesianChart Grid.Row="2" 
              Grid.Column="0" 
              Grid.ColumnSpan="3" /-->

    <StackPanel Grid.Row="3" Grid.Column="0">
        <TextBlock Text="Button Label" />
        <Button Content="Text" />
    </StackPanel>

    <lvc:CartesianChart Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Left" Height="100" Margin="129,136.2,0,0" VerticalAlignment="Top" Width="100"/>
</Grid>

我想要这样的东西

标签: wpflivecharts

解决方案


您必须获取相对于的屏幕点,CartesianChart否则返回的位置是相对于的MainWindow(因为您通过了this)。
阅读this以获得更详细的解释。

我还简化了您的代码:

public ObservablePoint MovingPoint { get; set; }

private void ChartOnDataClick(object sender, ChartPoint p) //click on point
{
  var observablePointIndex = this.diagram.Series[0].Values
    .GetPoints(this.diagram.Series[0].Model.View)
    .ToList()
    .IndexOf(p);
  this.MovingPoint = this.diagram.Series[0].Values[observablePointIndex] as ObservablePoint;
}

private void ChartOnMouseMove(object sender, MouseEventArgs e)
{
  if (this.MovingPoint == null
      || e.LeftButton != MouseButtonState.Pressed)
    return;

  Point newPoint = this.diagram.ConvertToChartValues(e.GetPosition(this.diagram));
  this.MovingPoint.X = newPoint.X;
  this.MovingPoint.Y = newPoint.Y;
}

private void ChartOnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
  this.MovingPoint = null; // deactivate point moving
}

例子Grid

请注意,当父级(例如 a增长)时,行/列的高度/宽度*将强制行/列拉伸以占用最大空间。是默认值。大小强制行/列根据内容调整其大小。Window*Auto

Grid.RowSpan使用行/列时,可以通过指定/让控件跨越多个行/列Grid.ColumnSpan

为了允许图表增长,包含图表的行的高度设置为*。所有其他行都设置为Auto

为了让图表延伸到整个GridGrid.ColumnSpan设置为3(因为Grid包含三列)。

以下示例基于您发布的图片:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>

  <StackPanel Grid.Row="0" Grid.Column="0"
              Orientation="Horizontal"
              Margin="0,0,12,0">
    <StackPanel>
      <TextBlock Text="ComboBox1 Label" />
      <ComboBox />
    </StackPanel>
    <StackPanel>
      <TextBlock Text="ComboBox2 Label" />
      <ComboBox />
    </StackPanel>
  </StackPanel>

  <StackPanel Grid.Row="0" Grid.Column="1"
              Orientation="Horizontal"
              VerticalAlignment="Bottom"
              Margin="0,0,12,0">
    <RadioButton Content="Text" />
    <RadioButton Content="Text" />
    <RadioButton Content="Text" />
  </StackPanel>

  <StackPanel Grid.Row="1" Grid.Column="2"
              HorizontalAlignment="Left">
    <TextBlock Text="Button Label" />
    <Button Content="Text" />
  </StackPanel>

  <CartesianChart Grid.Row="2" 
                  Grid.Column="0" 
                  Grid.ColumnSpan="3" />

  <StackPanel Grid.Row="3" Grid.Column="0">
    <TextBlock Text="Button Label" />
    <Button Content="Text" />
  </StackPanel>
</Grid>

推荐阅读