2

我正在学习 WPF,我正在尝试通过此处所述的 ListBox 创建图形。

一切都按预期工作,除了图表(ItemsPanel)没有按应有的方式旋转 90 度。

DataContext 只是一个ObservableCollection<KeyValuePair<double, int>>.

这是我的 XAML:

<Window x:Class="XNM.GUI.Graph"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="XNM" MinHeight="400" MinWidth="600" Height="400" Width="600"
    Icon="..\Resources\XNM2.ico"
    Style="{DynamicResource SkinWindowStyle}">   
<Border BorderBrush="Black" BorderThickness="5" Margin="10" Padding="5"
        CornerRadius="5">
    <Grid>
        <ListBox ItemsSource="{Binding}"
             ItemTemplate="{DynamicResource BarChartItemTemplate}"
             ItemsPanel="{DynamicResource BarChartItemsPanel}" />

    </Grid>
</Border>
<Window.Resources>
    <DataTemplate x:Key="BarChartItemTemplate">
        <Border Width="200" Height="50">
            <Grid>
                <Rectangle Fill="Red" StrokeThickness="0" 
                           Height="40" Width="{Binding Value}" Margin="3" 
                           HorizontalAlignment="Right" 
                           VerticalAlignment="Bottom">
                    <Rectangle.LayoutTransform>
                        <ScaleTransform ScaleX="20" />
                    </Rectangle.LayoutTransform>
                </Rectangle>
                <TextBlock Margin="20" FontWeight="Bold" 
                    HorizontalAlignment="Right" VerticalAlignment="Center" 
                    Text="{Binding Key}">
                    <TextBlock.LayoutTransform>
                        <ScaleTransform ScaleX="-1" ScaleY="1" />
                    </TextBlock.LayoutTransform>
                </TextBlock>
            </Grid>
        </Border>
    </DataTemplate>
    <ItemsPanelTemplate x:Key="BarChartItemsPanel">
        <VirtualizingStackPanel IsItemsHost="True">
            <VirtualizingStackPanel.LayoutTransform>
                <TransformGroup>
                    <RotateTransform Angle="90" />
                    <ScaleTransform ScaleX="-1" ScaleY="1" />
                </TransformGroup>
            </VirtualizingStackPanel.LayoutTransform>
        </VirtualizingStackPanel>
    </ItemsPanelTemplate>
</Window.Resources>

这是完整性背后的代码:

public partial class Graph : Window
{
    private ObservableCollection<SensorData> _sensorData;

    public Graph(ObservableCollection<SensorData> sensorData)
    {
        InitializeComponent();

        this._sensorData = sensorData;
        CreateGraph();
    }

    private void CreateGraph()
    {
        if (this._sensorData.Any())
        {
            this.DataContext = this._sensorData[0].CollatedResults;
        }
    }
}

有人可以在这里指出可能导致问题的原因。每当我以前使用模板时,我都没有遇到过这种问题,所以我有点难过。

谢谢。


更新

我发现这是导致问题的一种样式,尤其是ControlTemplate下面的 XAML 部分:

<Style TargetType="{x:Type ListBox}">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="Background" Value="{StaticResource ControlBackgroundBrush}" />
    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFA3AEB9" Offset="0" />
                <GradientStop Color="#FF8399A9" Offset="0.375" />
                <GradientStop Color="#FF718597" Offset="0.375" />
                <GradientStop Color="#FF617584" Offset="1" />
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Grid>
                    <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            CornerRadius="2" 
                            Background="{DynamicResource ControlBackgroundBrush}" />
                    <ScrollViewer Margin="1" 
                            Style="{DynamicResource NuclearScrollViewer}" 
                            Focusable="false" Background="{x:Null}">
                        <StackPanel Margin="1,1,1,1" IsItemsHost="true" />
                    </ScrollViewer>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" 
                             Value="{DynamicResource DisabledBackgroundBrush}" 
                             TargetName="Border" />
                        <Setter Property="BorderBrush" 
                             Value="{DynamicResource DisabledBorderBrush}" 
                             TargetName="Border" />
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在由于我对样式缺乏了解,我不确定为什么会产生这种效果,但是当我注释掉该ControlTemplate部分时,它可以按预期工作,诚然其他 ListBox 存在明显的样式问题。

有谁知道为什么这会导致我遇到的结果?希望我自己能弄明白,但我认为我需要一段时间才能理解这种程度的风格!

谢谢

4

0 回答 0