哪种方式更好,更正确,为什么?

时间:2012-06-28 11:37:19

标签: c# wpf templates

在WPF中,有很多方法可以实现同样的功能。在这个例子中哪个更好?

<ItemsControl>
    <ItemsControl.Template>
        <ControlTemplate>
            <ScrollViewer>
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

<ScrollViewer>
    <ItemsControl />
</ScrollViewer>

2 个答案:

答案 0 :(得分:2)

这将是一个更好的解决方案

<ItemsControl>
    <ItemsControl.Template>
        <ControlTemplate>
            <ScrollViewer CanContentScroll="True">
                <StackPanel IsItemsHost="True" />
            </ScrollViewer>
        </ControlTemplate> 
    </ItemsControl.Template>
</ItemsControl>

这是因为与ItemsControlItemsPresenter不同,StackPanel支持IScrollInfo界面,因此您可以更多地控制项目的滚动方式。< / p>

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.iscrollinfo.aspx

答案 1 :(得分:2)

直到你真正需要模板,不要使用模板。 =)

那就是说,我个人的经验法则是,如果我重复同样的工作两次以上(即三次以上),我可能需要模板或实施其他可爱的DRY原则。

相关问题