gpt4 book ai didi

c# - 如何在嵌套的 DataGrid 中绑定(bind) ContextMenu 项?

转载 作者:行者123 更新时间:2023-12-03 10:23:03 25 4
gpt4 key购买 nike

我已经嵌套了 DataGrid。嵌套网格显示为所选 DataGrid 的 RowDetails。这是我与 DataGrid 绑定(bind)的类结构

public class CustomTable : INotifyPropertyChanged
{
public List<DataTable> Main { get; set; }
private List<string> _menu;
public List<string> Menu
{
get
{
return _menu;
}
set
{
_menu = value
OnPropertyChanged("Menu");
}
}

private CustomTable _child;
public CustomTable Child
{
get
{
return _child;
}
set
{
_child = value;
OnPropertyChanged("Child");
}
}
public DataRowView _selectedItem;
public DataRowView SelectedItem
{
get
{
return _selectedItem;
}
set
{
_selectedItem = value;
Child = new CustomTable();
OnPropertyChanged("SelectedItem");
}
}


public CustomTable()
{
Main = new List<DataTable>();
Main.Add(someRandomTable());

}

private DataTable someRandomTable()
{
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}



public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string caller)
{

if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
}

}

这是我将数据网格与数据表绑定(bind)的 XAML。我想将菜单列表绑定(bind)到 DataGrid。谢谢
<ScrollViewer>
<ScrollViewer.Resources>
<DataTemplate DataType="{x:Type l:CustomTable}">
<StackPanel>
<ItemsControl ItemsSource="{Binding Path=Main}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataGrid Name="dg" SelectedItem="{Binding DataContext.SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl}}"
CanUserAddRows="False" ItemsSource="{Binding}" AutoGenerateColumns="True" >
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<ContentControl Margin="10"
Content="{Binding DataContext.Child, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl,AncestorLevel=2}}"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ScrollViewer.Resources>

<ContentControl Content="{Binding TableCollection}"/>

</ScrollViewer>

最佳答案

干得好

截屏

screenshot

xml

<DataGrid Name="dg"
SelectedItem="{Binding DataContext.SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl}}"
CanUserAddRows="False"
ItemsSource="{Binding}"
AutoGenerateColumns="True"
Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl}}">
<DataGrid.ContextMenu>
<ContextMenu ItemsSource="{Binding PlacementTarget.Tag.Menu, RelativeSource={RelativeSource Self}}" />
</DataGrid.ContextMenu>

注意这两点
  • Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl}}"在数据网格和
  • ItemsSource="{Binding PlacementTarget.Tag.Menu, RelativeSource={RelativeSource Self}}"在上下文菜单中

  • 这是一种解决方法,因为上下文菜单不是数据网格可视树的一部分,因此无法直接绑定(bind)到 Menu 属性,甚至无法使用 FindAncestor ETC

    示例菜单生成
    public CustomTable()
    {
    Main = new List<DataTable>();
    Main.Add(someRandomTable());

    Menu = new List<string>();
    Menu.Add("Menu item 1");
    Menu.Add("Menu item 2");
    Menu.Add("Menu item 3");
    Menu.Add("Menu item 4");

    }

    关于c# - 如何在嵌套的 DataGrid 中绑定(bind) ContextMenu 项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24669238/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com