WPF在GridView中获取关联的ListView

时间:2018-06-17 14:08:43

标签: wpf listview gridview

我正在尝试为GridView实施自定义ListView,但我需要找到一种方法来获取与ListView相关联的GridView因为我需要访问一些ListView属性(WidthItemsTemplate ...)。

我找到了一个问过同一个问题Get the parent listview from a gridview object的旧帖子,但它从来没有得到答案......

如果有人有想法,我会很高兴:)

编辑:这里有一些来自自定义GridView

的基本代码
public class GridViewEx : GridView
{
    public ListView Owner {get; set;}     // This is what I need to get

    public GridViewEx()
    {

    }

}

EDIT2:我找到了另一个解决方案,而不是mm8提供的解决方案。由于我还需要在GridViewHeaderRowPresenter中使用的自定义ListView Scrollviewer Style,因此我提出了这个问题(就像现在一样):

public class GridViewHeaderRowPresenterEx : GridViewHeaderRowPresenter
{
    private GridViewEx _GridView;

    public GridViewHeaderRowPresenterEx()
    {
        Loaded += OnLoaded;
        Unloaded += OnUnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        if (this.GetVisualParent<ListView>() is ListView lv && lv.View is GridViewEx gridView)
        {
            _GridView = gridView;
            _GridView.Owner = lv;
        }
    }

    private void OnUnLoaded(object sender, RoutedEventArgs e)
    {
        if (_GridView != null)
            _GridView.Owner = null;
    }
}

以下是从自定义ListView获取GridViewHeaderRowPresenter的扩展方法:

public static class DependencyObjectExtensions
{
    public static T GetVisualParent<T>(this DependencyObject depObj) where T : DependencyObject
    {
        if (VisualTreeHelper.GetParent(depObj) is DependencyObject parent)
        {
            var result = (parent as T) ?? GetVisualParent<T>(parent);
            if (result != null)
                return result;
        }

        return null;
    }
}

GridViewHeaderRowPresenter添加到Loaded时调用GridView ListView事件,Unloaded事件在GridView时被调用}已从ListView中删除。

我比mm8更喜欢这个解决方案,因为它需要(如果我没有弄错的话)ListViewItems才能工作。

感谢您的建议:)

2 个答案:

答案 0 :(得分:1)

您可以覆盖PrepareItem方法并使用ItemsControl.ItemsControlFromItemContainer方法获取对父ListView的引用:

public class GridViewEx : GridView
{
    public ListView Owner { get; set; }

    public GridViewEx()
    {

    }

    protected override void PrepareItem(ListViewItem item)
    {
        base.PrepareItem(item);
        Owner = Owner ?? ItemsControl.ItemsControlFromItemContainer(item) as ListView;
    }
}

答案 1 :(得分:0)

您可以将绑定 RelativeSource AncestorType 一起使用,请尝试以下代码:

XAML:

<Grid>
    <ListView ItemsSource="{Binding}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
                <GridViewColumn Header="Test">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button
                                DataContext="{Binding Path=ItemsSource, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"
                                Content="Get ListView ItemsSource Count"
                                Click="Test_Click" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

C#:

class User
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Mail { get; set; }
}

public MainWindow()
{
    InitializeComponent();
    DataContext = new List<User>
    {
        new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" },
        new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" },
        new User() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe@gmail.com" }
    };
}

private void Test_Click(object sender, RoutedEventArgs e)
{
    if (sender is FrameworkElement fe)
        if (fe.DataContext is List<User> users)
            MessageBox.Show($"ItemsSource items count: {users.Count()}.", "ListView Test");
}