【问题标题】:How to make WPF Toolkit charts expand with to fit content?如何使 WPF Toolkit 图表扩展以适应内容?
【发布时间】:2019-03-28 16:40:21
【问题描述】:

我正在尝试从DotNetProjects.Wpf.Toolkit 创建一些条形图,以显示一些数据总计,尽管数据可能会在运行时发生变化。问题是我无法让图表适合内容的大小 - 我总是必须提供一个固定值(并且设置 Height="Auto" 不起作用),但我不知道需要什么大小在跑步的时候。我可以做些什么来使我的图表适合内容而不是相反?

我的 XAML 代码如下所示:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Movie_Vault"
        xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=DotNetProjects.Layout.Toolkit" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="Movie_Vault.frmStatistics"
        mc:Ignorable="d"
        Title="frmStatistics" Style="{StaticResource RoundedFormStyle}" WindowStartupLocation="CenterOwner">
    <Border Style="{StaticResource WindowBorderStyle}">
        <DockPanel x:Name="OuterPanel" >
            <Border Style="{StaticResource PanelBorderStyle}" DockPanel.Dock="Top">
                <DockPanel x:Name="TopPanel" HorizontalAlignment="Stretch" VerticalAlignment="Top">
                    <Image Height="16" Margin="8,4,8,4" Source="Images/process.png"/>
                    <TextBlock Style="{StaticResource MediumFont}"><Run Text="Statistics"/></TextBlock>
                    <DockPanel DockPanel.Dock="Right" HorizontalAlignment="Right" VerticalAlignment="Center">
                        <Button x:Name="btnClose" Content="X" Style="{StaticResource MicroButtonStyle}" Margin="0,0,8,0" Click="btnClose_Click"/>
                    </DockPanel>
                </DockPanel>
            </Border>
            <Border Style="{StaticResource PanelBorderStyle}" DockPanel.Dock="Left" Margin="0,8,0,0">
                <DockPanel VerticalAlignment="Top"  >
                    <ScrollViewer VerticalScrollBarVisibility="Auto" Height="400" Width="500">
                        <Grid Name="panelTopGenres">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <toolkit:Chart Name="chartTopGenres" Title="Top Genres" VerticalAlignment="Top" 
                   Margin="16,16,16,8" Padding="8" Background="White" >
                                <toolkit:BarSeries DependentValuePath="Value" IndependentValuePath="Key" 
                           ItemsSource="{Binding}" 
                           IsSelectionEnabled="False" Background="White"/>
                            </toolkit:Chart>
                            <Button Grid.Row="1" x:Name="btnTopGenresToggle" Style="{StaticResource SmallButtonStyle}" 
            Content="Show All Genres" Width="120" 
            Margin="16,0,4,4" HorizontalAlignment="Left" Click="btnToggle_Click" />
                        </Grid>
                    </ScrollViewer>
                </DockPanel>
            </Border>
        </DockPanel>
    </Border>
</Window>

【问题讨论】:

    标签: c# wpf charts wpftoolkit


    【解决方案1】:

    我可以做些什么来使我的图表适合内容而不是相反?

    摆脱任何StackPanels:

    <Grid Name="panelTopGenres">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <toolkit:Chart Name="chartTopGenres" Title="Top Genres" VerticalAlignment="Top" 
                       Margin="16,16,16,8" Padding="8" Background="White" >
            <toolkit:BarSeries DependentValuePath="Value" IndependentValuePath="Key" 
                               ItemsSource="{Binding}" 
                               IsSelectionEnabled="False" Background="White"/>
        </toolkit:Chart>
        <Button Grid.Row="1" x:Name="btnTopGenresToggle" Style="{StaticResource SmallButtonStyle}" 
                Content="Show All Genres" Width="120" 
                Margin="16,0,4,4" HorizontalAlignment="Left" Click="btnToggle_Click" />
    </Grid>
    

    StackPanel 不会调整其子级的大小。

    【讨论】:

    • 对我不起作用,它仍然会将所有数据压碎到默认高度而不展开。
    • Grid 的父面板是什么?请记住在提问时始终包含MCVE
    • 好的,我已经更新了我的原始帖子以显示整个窗口的代码。但是,尽管我构建了我的代码,但我似乎仍然无法让 Chart 正确调整大小。也许我需要一个自定义的DataTemplate,但是我看到的例子看起来很复杂,我不知道要改变什么。
    猜你喜欢
    • 2020-02-12
    • 2015-09-19
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 2014-10-25
    相关资源
    最近更新 更多