如何绑定嵌套DataGrid中的ContextMenu项? [英] How to bind ContextMenu items in a nested DataGrid?

查看:149
本文介绍了如何绑定嵌套DataGrid中的ContextMenu项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  public class CustomTable:INotifyPropertyChanged 
{
public列表< DataTable>主{get;组; }
private List< string> _菜单;
public List< string>菜单
{
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);
返回表;
}



公共事件PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string caller)
{

if(PropertyChanged!= null)
{
PropertyChanged(this,new PropertyChangedEventArgs (呼叫者));
}
}

}

我的XAML要用datatable绑定datagrid。我想将菜单列表绑定到DataGrid。谢谢

 < ScrollViewer> 
< ScrollViewer.Resources>
< DataTemplate DataType ={x:Type l:CustomTable}>
< StackPanel>
< ItemsControl ItemsSource ={Binding Path = Main}>
< ItemsControl.ItemTemplate>
< DataTemplate>
< DataGrid Name =dgSelectedItem ={Binding DataContext.SelectedItem,Mode = TwoWay,RelativeSource = {RelativeSource FindAncestor,AncestorType = ItemsControl}}
CanUserAddRows =FalseItemsSource ={绑定}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>


解决方案

这里你去



截图





xaml

 < 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}}在上下文菜单中



这是一个解决方法,因为上下文菜单不是数据网格的视觉树的一部分,因此不能直接或直接使用 FindAncestor etc



样本菜单生成

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

Menu = new List< string>();
Menu.Add(菜单项1);
Menu.Add(菜单项2);
Menu.Add(菜单项3);
Menu.Add(菜单项4);

}


I have nested DataGrid. The nested Grid is displayed as RowDetails of the selected DataGrid. This is my class structure that is binded with the DataGrid

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));
            }
        }

}

This is my XAML to to bind the datagrid with datatable. I want to bind Menu list to DataGrid. Thanks

<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>

解决方案

Here you go

screenshot

xaml

<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>

note these two things

  • Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl}}" in data grid and
  • ItemsSource="{Binding PlacementTarget.Tag.Menu, RelativeSource={RelativeSource Self}}" in context menu

this is a workaround as context menu is not a part of visual tree of data grid so cannot bind to the Menu property directly or even by using FindAncestor etc

sample menu generation

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");

}

这篇关于如何绑定嵌套DataGrid中的ContextMenu项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆