【问题标题】:Add vertical scroll in legend box (ChartPlotter) WPF在图例框中添加垂直滚动(ChartPlotter)WPF
【发布时间】:2012-05-07 08:59:05
【问题描述】:

我是 WPF 应用程序的新手。我正在开发一个读取 WPF 应用程序的传感器。在此应用程序中,我必须从 170 个传感器中选择传感器。选择传感器后,当我单击查看报告时,图表绘图仪会绘制选定的图形传感器。它对我来说很好用。

问题:

这里由于页面大小限制,我有固定的图形高度。但是当传感器的数量超过图形的高度时,所选传感器的图例会隐藏那些未调整图形高度的传感器。我如何添加滚动到图例框,以便用户可以滚动并检查所有传感器图例。

请给点意见。

提前致谢。

【问题讨论】:

    标签: c# wpf dynamic-data-display


    【解决方案1】:

    我在网上搜索了很多这个问题,但我没有解决这个问题。所以要解决这个问题,我按照以下步骤操作。

    1.)首先我通过以下方式使绘图仪图例可见性为假 plotter.LegendVisible = false;

    2.)其次,我在出现绘图仪图例的图形上添加了一个列表视图控件。

      <ListView Height="Auto" HorizontalAlignment="Center" Margin="1100,139,0,0" Name="listview" ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" Width="75" Grid.RowSpan="2" MaxHeight="260">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Text}" Foreground="{Binding BackgroundColor}">
                                        </TextBlock>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                                <ListView.BorderBrush>
                                    <SolidColorBrush />
                                </ListView.BorderBrush>
                            </ListView>
    

    3.) 然后我在后端做一些工作 -

    -添加 ItemVM.cs :

     class ItemVM : INotifyPropertyChanged
    {
        private string TextValue = String.Empty;
        private Brush BackgroundColorValue = null;
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    
        public ItemVM(Brush color, string objectData)
        {
            BackgroundColor = color;
            Text = objectData;
        }
        public string Text
        {
            get
            {
                return this.TextValue;
            }
    
            set
            {
                if (value != this.TextValue)
                {
                    this.TextValue = value;
                    NotifyPropertyChanged("Text");
                }
            }
        }
        public Brush BackgroundColor
        {
            get
            {
                return this.BackgroundColorValue;
            }
    
            set
            {
                if (value != this.BackgroundColorValue)
                {
                    this.BackgroundColorValue = value;
                    NotifyPropertyChanged("BackgroundColor");
                }
            }
        }
    
    }
    

    -在 mainform.xaml.cs 中:

     List<ItemVM> Items;
                   List<string> lst = new List<string> {"item1","item2","item3" };
                   var converter = new System.Windows.Media.BrushConverter();
      Color[] colors = ColorHelper.CreateRandomColors(3);
      Items = new List<ItemVM>();
     for(int i=0;i<lst.count;i++)
     {
     Items.Add(new ItemVM((Brush)converter.ConvertFromString(colors[i].ToString()), SelectedItems[i]));
    }
     plotter.LegendVisible = false;
     listview.ItemsSource = Items;
    

    现在我在图表绘图仪上获得了带有滚动和重新生成前景色的图例框也反映了图表线条颜色。

    【讨论】:

      【解决方案2】:

      使用 ScrollViewer:

      <ScrollViewer Height="whatever your graph height is">
        <ListView>
                ...Dynamically place all your sensors here from your Presenter or code-behind
        </ListView>
      </ScrollViewer>
      

      这应该会为您处理好。

      【讨论】:

      • 感谢您的回复。您曾经在 WPF 中使用过 DynamicDataDisplay 程序集吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多