未应用WPF ItemsPanelTemplate资源

时间:2013-11-07 16:51:38

标签: wpf xaml listbox

我正在学习WPF,我正在尝试通过ListBox创建一个图形,如here所述。

除了图表(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;
        }
    }
}

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

感谢。


更新

我发现这是导致问题的一种风格,特别是下面的XAML的ControlTemplate部分:

<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有明显的样式问题。

有谁知道为什么这会导致我遇到的结果?希望我能自己解决这个问题,但我认为这将是一段时间,直到我理解那种程度的风格!

由于

0 个答案:

没有答案