如何在WPF中删除LineGraph周围的边框?

如何删除图片中所示的边框?

如何在WPF中删除LineGraph周围的边框?

我正在尝试删除图像周围的边框 并尝试了一些解决方案,例如 BorderBrush="Transparent" BorderThickness="0" 但是这些解决方案不起作用。 我只希望窗口中有图形部分。 我正在提供XAMLCS代码。请帮我解决问题。

XAML:

<Window x:Class="WpfToolkitChart.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="308.796" Width="436.419" Background="White"
        xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">


    <Window.Resources>
        <Style x:Key="DataPointStyle1" TargetType="{x:Type chartingToolkit:LineDataPoint}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="chartingToolkit:LineDataPoint">
                        <Grid Margin="0,0">
                            <Ellipse Fill="#617D99" ToolTip="{Binding Y}"/>
                            <Canvas>
                                <Image Source="/Images/marker.png" Cursor="Hand" Height="40" Width="20" Margin="-6,-33,0" Visibility="{Binding BindingInfo}" ToolTip="You!!" />
                                <Ellipse  Fill="{Binding Info}" Height="10px" Width="10px" Margin="0,-20,0" />
                            </Canvas>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="#33557A"></Setter>
        </Style>
    </Window.Resources>

    <Grid Margin="0,0">
        <chartingToolkit:Chart  Name="lineChart" Title=""  VerticalAlignment="Top" Margin="0,0" FontSize="10px" Height="269" Foreground="Black"  Background="Transparent" 
                                BorderBrush="Transparent">
            <chartingToolkit:Lineseries Name="lp"
                                        DependentvaluePath="Y" Margin="0,0"  IndependentvaluePath="X" 
                                        ItemsSource="{Binding}" IsSelectionEnabled="True"
                                        DataPointStyle="{StaticResource DataPointStyle1}">
            </chartingToolkit:Lineseries>
            <chartingToolkit:Chart.Axes>
                <chartingToolkit:LinearAxis Orientation="Y" Visibility="Hidden"/>
            </chartingToolkit:Chart.Axes>
            <chartingToolkit:Chart.LegendStyle>
                <Style x:Name="LegendHideStyle1" TargetType="Control">
                    <Setter Property="Width" Value="0"/>
                    <Setter Property="Height" Value="0"/>
                </Style>
            </chartingToolkit:Chart.LegendStyle>
            <chartingToolkit:Chart.PlotAreaStyle>
                <Style TargetType="Grid">
                    <Setter Property="Background" Value="Transparent" />
                </Style>
            </chartingToolkit:Chart.PlotAreaStyle>
        </chartingToolkit:Chart>
    </Grid>
</Window>

CS:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            showColumnChart();
        }

        private void showColumnChart()
        {
            double y1 = 5,y2 = 1,y3 = 3,y4 =2;
            ObservableCollection<MyDataModelClass> data = new ObservableCollection<MyDataModelClass>{
            new MyDataModelClass {X = "A",Y = y1,BindingInfo = "Hidden" },new MyDataModelClass {X = "B",Y = y2,new MyDataModelClass {X = "C",Y = y3,new MyDataModelClass {X = "D",Y = y4,BindingInfo = "Visible" }
            };

            double max = data.Max(m => m.Y);
            double min = data.Min(m => m.Y);
            double Percentage = 15;

            double diff = max - min;
            double percetage = (Math.Abs(diff) / 100) * Percentage;
            double minVal = min - percetage;
            double maxVal = max + percetage;
            LinearAxis axis = new LinearAxis();
            axis.Orientation = AxisOrientation.Y;
            axis.Maximum = maxVal;
            axis.Minimum = minVal;
            axis.Visibility = Visibility.Hidden;

            //and use it:
            Lineseries ls = lineChart.Series.First() as Lineseries;
            ls.DependentRangeAxis = axis;

            //lineChart.Visibility = Visibility.Hidden;
            ls.BorderBrush = brushes.Transparent;


            lineChart.DataContext = data;

        }
    }
}
emheiyan 回答:如何在WPF中删除LineGraph周围的边框?

经过大量搜索和实验,我找到了一个简单的解决方案。我们只需在XAML文件中添加以下代码和一个参考即可更改图表模板:

xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"


<chartingToolkit:Chart.Template>
                <ControlTemplate TargetType="chartingToolkit:Chart">
                    <Border 
                BorderBrush="Transparent"
                BorderThickness="0">
                        <Grid>
                            <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                                <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                            </chartingprimitives:EdgePanel>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </chartingToolkit:Chart.Template>
,

此控件没有提供便捷的方法。但是,即使这段代码更加脆弱,总有一种方法可以做到。

首先,您可以使用Visual Studio调试器探索控件的结构。在VS 2019中有Live Visual Tree: Live Visual Tree 在以前的版本中,您可以在调试器中找到它: enter image description here enter image description here 第三党也有很多其他类似的工具。 一旦在结构中找到需要更改的控件(在本例中为EdgePanel内部的Border)。然后,您可以从后面的代码中更改此控件。您无法在构造函数中执行此操作,因为可视树尚未在构造时加载。您可以在“加载”中执行此操作。因此,您需要在图表控件上订阅Loaded事件(也许还可以订阅LayoutUpdate事件,因为在布局更新时它可以恢复其原始值)。函数FindVisualChild帮助在视觉树中按类型和名称查找控件。找到边框后,我们可以使边框透明(安全选项),也可以使其厚度为0,但可能会影响布局。我们也可以删除它,但是依赖它的某些内容可能会损坏。 请注意,如果您更改主题,此代码可能会停止工作,因为它可能具有不同的可视树。

    void LineChart_OnLoaded(object sender,RoutedEventArgs e) {
        if (sender is Chart chart) {
            RemoveBorder(chart);
        }
    }

    void RemoveBorder(Chart chart) {
        EdgePanel ep = FindVisualChild<EdgePanel>(chart,"ChartArea");
        Border border = FindVisualChild<Border>(ep);
        border.BorderBrush = Brushes.Transparent;
    }

    static T FindVisualChild<T>(DependencyObject obj,string name = null) where T : FrameworkElement {
        int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
        for (int i = 0; i < childrenCount; i++) {
            DependencyObject child = VisualTreeHelper.GetChild(obj,i);
            if (child is T element && (name == null || name == element.Name)) {
                return element;
            }
            T childOfChild = FindVisualChild<T>(child,name);
            if (childOfChild != null) {
                return childOfChild;
            }
        }
        return null;
    }
本文链接:https://www.f2er.com/3146556.html

大家都在问