Canvas.Top无法渲染

时间:2012-04-10 21:45:42

标签: wpf xaml itemtemplate

我在itemControl中有一个画布,对于数据模板我使用省略号。每个椭圆的位置表示上午6点到晚上11点之间的时间。我根据画布的大小和分配给椭圆的时间落在该值上。 ellispse顶部根本没有在画布中移动。我试图删除绑定并使用硬值,它仍然在同一个地方。

这是容器的XAML

        <ItemsControl Grid.Column="1" ItemsSource="{Binding AngerData}" VerticalAlignment="Stretch" Canvas.ZIndex="1">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas VerticalAlignment="Stretch"></Canvas>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Ellipse Height="10" Width="10" Stroke="White" StrokeThickness="1" 
                             Canvas.Top="{Binding Top, Converter={StaticResource ResourceKey=ellipsePositionConverter}, ConverterParameter=Month}" >"
                             Fill="{Binding AngerRating, Converter={StaticResource angerRatingConverter}}"
                             Canvas.ZIndex="100">
                    </Ellipse>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

这是转换器,我不认为这是一个问题,因为删除它不会移动椭圆的位置

public class CalendarDayEllipsePositionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double gridPosition = 0;

        if (parameter.ToString().Equals("Month", StringComparison.InvariantCultureIgnoreCase))
        {
            gridPosition = double.Parse(value.ToString()) * 110;
        }

        return gridPosition;
    }

    public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

Canvas控件是否设置了高度?

你可以在Converter的第一行放置一个断点来检查它是否被调用?

...试试这个:

<Canvas VerticalAlignment="Stretch">
            <ItemsControl Grid.Column="1"
                      VerticalAlignment="Stretch"
                      Canvas.ZIndex="1"
                      ItemsSource="{Binding AngerData}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Ellipse Canvas.Top="{Binding 
                                                  Converter={StaticResource ResourceKey=ellipsePositionConverter},
                                                  ConverterParameter=Month}"
                             Width="10"
                             Height="10"
                             Canvas.ZIndex="100"
                             Fill="Red"
                             Stroke="White"
                             StrokeThickness="1" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>

            </ItemsControl>
        </Canvas>