从SurfaceListBox中的类访问公共方法

时间:2015-03-12 14:12:34

标签: c# wpf listbox selectionchanged

我已按https://msdn.microsoft.com/en-us/library/ff727730.aspx所述创建了WPF解决方案。此解决方案将使用SurfaceListBox为我提供连续列表。这没关系,没有问题。

现在,我要点击图片,向任意方向移动X像素。

因此,我创建了活动MainSurfaceListBox_OnSelectionChanged并将其添加到MainWindow.xaml

<Window x:Class="ContinuousList.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="http://schemas.microsoft.com/surface/2008"
        xmlns:l="clr-namespace:ContinuousList"
        Title="ContinuousList" Height="640" Width="800">
    <Grid>

        <s:SurfaceListBox Name="MainSurfaceListBox"
                          SelectionChanged="MainSurfaceListBox_OnSelectionChanged">
            <s:SurfaceListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding}" Width="270"/>
                </DataTemplate>
            </s:SurfaceListBox.ItemTemplate>
            <s:SurfaceListBox.Template>
                <ControlTemplate>
                    <s:SurfaceScrollViewer
                        VerticalScrollBarVisibility="Disabled"
                        HorizontalScrollBarVisibility="Hidden"
                        CanContentScroll="true">
                        <l:LoopPanelVertical IsItemsHost="True"/>
                    </s:SurfaceScrollViewer>
                </ControlTemplate>
            </s:SurfaceListBox.Template>
        </s:SurfaceListBox>

    </Grid>
</Window>

MainWindow.xaml.cs

private void MainSurfaceListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //trying to call a public method from LoopPanelVertical
}

现在,从MainWindow.xaml.cs我尝试运行方法LoopPanelVertical.LineUp()。我的问题是我找不到从LoopPanelVertical访问此方法或任何公共方法的方法。

namespace ContinuousList
{
    public class LoopPanelVertical : Panel, ISurfaceScrollInfo
    {
        ...
        public void LineUp()
        {
             ScrollContent(1);
        }
    }
}

请帮助我了解实现这一目标的必要性吗?谢谢!

2 个答案:

答案 0 :(得分:0)

为了调用(非静态)类的方法,您需要引用该类的实例。您可以设置Name类的x:NameLoopPanelVertical属性,然后使用该名称来引用它:

<ControlTemplate>
    <s:SurfaceScrollViewer
        VerticalScrollBarVisibility="Disabled"
        HorizontalScrollBarVisibility="Hidden"
        CanContentScroll="true">
        <l:LoopPanelVertical x:Name="Panel" IsItemsHost="True"/>
    </s:SurfaceScrollViewer>
</ControlTemplate>

...

private void MainSurfaceListBox_OnSelectionChanged(object sender, 
    SelectionChangedEventArgs e)
{
    Panel.LineUp();
}

答案 1 :(得分:0)

刚刚使用子元素找到答案:

 private void MainSurfaceListBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listBox = sender as SurfaceListBox;
            if (listBox == null) return;
            var childElement = FindChild(listBox, i => i as LoopPanelVertical);
            childElement.LineUp();
        }

        static T FindChild<T>(DependencyObject obj, Func<DependencyObject, T> pred) where T : class
        {
            var childrenCount = VisualTreeHelper.GetChildrenCount(obj);
            for (var i = 0; i < childrenCount; i++)
            {
                var dependencyObject = VisualTreeHelper.GetChild(obj, i);
                var foo = pred(dependencyObject);
                return foo ?? FindChild(dependencyObject, pred);
            }
            return null;
        }