我正在尝试为GridView
实施自定义ListView
,但我需要找到一种方法来获取与ListView
相关联的GridView
因为我需要访问一些ListView
属性(Width
,Items
,Template
...)。
我找到了一个问过同一个问题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更喜欢这个解决方案,因为它需要(如果我没有弄错的话)ListView
要Items
才能工作。
感谢您的建议:)
答案 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");
}