Home Forums WPF controls Xceed DataGrid for WPF Context Menu

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #22206 |

    how to create a context menu to Fixed Header. when i click on header column context menu will be different and when i click on DataGrid rows Context menu will be shown differently.

    Imported from legacy forums. Posted by rajendernag (had 3041 views)

    Xceed Support
    Member
    Post count: 5658

    Context Menu in the DataGridControl works exactly the same way is for other WPF controls… I advise reading the Windows SDK articles on the subject for the “How” questions specific to ContextMenu creation.

    As for how to specify a different context menu for the ColumnManagerCell and the DataCell, its as simple as providing a different context menu for each of then in a implicit style created for each of them.

    Example:

    <code>
    <Window.Resources>

    <Style TargetType=”{x:Type xcdg:DataCell}”>
    <Setter Property=”ContextMenu”>
    <Setter.Value>
    <!– Create your Context Menu here –>
    <ContextMenu/>
    </Setter.Value>
    </Setter>
    </Style>

    <Style TargetType=”{x:Type xcdg:ColumnManagerCell}”>
    <Setter Property=”ContextMenu”>
    <Setter.Value>
    <!– Create your other Context Menu here –>
    <ContextMenu/>
    </Setter.Value>
    </Setter>
    </Style>

    </Window.Resources>

    Imported from legacy forums. Posted by Marcus [Xceed] (had 762 views)

    User (Old forums)
    Member
    Post count: 23064

    In the example that was posted…

    <Style TargetType=”{x:Type xcdg:DataCell}”>
    <Setter Property=”ContextMenu”>
    <Setter.Value>
    <!– Create your Context Menu here –>
    <ContextMenu/>
    </Setter.Value>
    </Setter>
    </Style>

    Rather than set the value off to a StaticResource as in
    <Setter Property=”ContextMenu” Value=”{StaticResource StandardContextMenu}”>

    with a Context Menu declared above it…

    <ContextMenu x:Key=”Standard_ContextMenu”>
    <MenuItem Header=”_New” Click=”AddRecord_Click”/>
    <MenuItem Header=”_Edit” Click=”EditRecord_Click”/>
    <MenuItem Header=”_Delete” Click=”DeleteRecord_Click”/>
    </ContextMenu>

    What is the exact Syntax in including ALL the code in one structure…i.e. where Marcus says..

    <!– Create your Context Menu here –>

    Imported from legacy forums. Posted by Desmond (had 3183 views)

    User (Old forums)
    Member
    Post count: 23064

    So why would this work:

                <Style TargetType=”{x:Type xcdg:DataCell}”>
     
                    <Style.Triggers>
                        
                        <DataTrigger                        
                            Binding=”{Binding
                                Converter={StaticResource isTypeOfConverter},
                                ConverterParameter={x:Type Presentation:TargetModel},
                                Mode=OneWay}”
                            Value=”True”>
                            <Setter Property=”Background” Value=”Pink”/>
                        </DataTrigger>

     

    But this does not work:

                <Style TargetType=”{x:Type xcdg:DataCell}”>
                    <Style.Triggers>                   
                        <DataTrigger                       
                            Binding=”{Binding
                                Converter={StaticResource isTypeOfConverter},
                                ConverterParameter={x:Type Presentation:TargetModel},
                                Mode=OneWay}”
                            Value=”True”>
                            <Setter
                                Property=”ContextMenu”>
                                <Setter.Value>
                                    <ContextMenu />
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>

     

    I get the following error:

    Cannot add content of type ‘System.Windows.Controls.ContextMenu’ to an object of type ‘System.Object’.

    Imported from legacy forums. Posted by chris (had 1720 views)

    Xceed Support
    Member
    Post count: 5658

    Because WPF does not support setting Visual-derived objects (e.g., ContextMenu) as property values through styles.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 392 views)

    User (Old forums)
    Member
    Post count: 23064

    So, since you deleted my other thread – which was my core question… which is not answered herein…

    #1 – does this mean that the strategy suggested by Marc Laroche above is just wrong?

    #2 – what is the way to assign a different context menu to a different detail view in a master/ detail grid? THIS is my core question. The suggestion from Marc above had me going for ~2 hours yesterday, so I tried to ‘reframe’ my question in terms of EXACTLY what I am trying to do – set a different context menu on different detail views.

    Imported from legacy forums. Posted by chris (had 811 views)

    Xceed Support
    Member
    Post count: 5658

    1- Yes and no. Basically, you can’t create the context menu in the style; however you can set an instance of it.

    2- Here is quick example:

      <ContextMenu x:Key=”masterMenu”>

                <TextBlock Text=”MASTER_FirstItem” />

                <TextBlock Text=”MASTER_SecondItem” />

             </ContextMenu>

             <ContextMenu x:Key=”detailMenu”>

                <TextBlock Text=”DETAIL_FirstItem” />

                <TextBlock Text=”DETAIL_SecondItem” />

             </ContextMenu>

             <Style TargetType=”{x:Type xcdg:DataCell}”>

                <Setter Property=”ContextMenu”

                        Value=”{StaticResource detailMenu}” />

                <Style.Triggers>

                   <DataTrigger Binding=”{Binding RelativeSource={RelativeSource Self}, Path=(xcdg:DataGridControl.DataGridContext).SourceDetailConfiguration}”

                                Value=”{x:Null}”>

                      <Setter Property=”ContextMenu”

                              Value=”{StaticResource masterMenu}” />

                   </DataTrigger>

                </Style.Triggers>            

             </Style> 

    Now if you want the context menus to be different from one detail to another, then you will need to change the binding to something more appropriate to your situation.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 385 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you – that was very helpful. My setup now looks essentially like this:

                <ContextMenu
                    x:Key=”targetsMenu”>
                    <MenuItem>Here is my menu for a target</MenuItem>
                </ContextMenu>

                <ContextMenu               
                    x:Key=”ordersMenu”>
                    <MenuItem>Here is my menu for an order</MenuItem>
                </ContextMenu>
                

                <Style TargetType=”{x:Type xcdg:DataCell}”>
     
                    <Style.Triggers>                    

                        <DataTrigger                       
                            Binding=”{Binding
                                Converter={StaticResource isTypeOfConverter},
                                ConverterParameter={x:Type Presentation:TargetModel},
                                Mode=OneWay}”
                            Value=”True”>
                            <Setter
                                Property=”ContextMenu”
                                Value=”{StaticResource targetsMenu}”>
                            </Setter>
                        </DataTrigger>

                        <DataTrigger
                            Binding=”{Binding
                                Converter={StaticResource isTypeOfConverter},
                                ConverterParameter={x:Type Presentation:MarketOrderModel},
                                Mode=OneWay}”
                            Value=”True”>
                            <Setter
                                Property=”ContextMenu”
                                Value=”{StaticResource ordersMenu}”>
                            </Setter>
                        </DataTrigger>

     The use of a converter to tell me what type the items in the view are is how I select what context menu to show:

        public class IsTypeOfConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var type = parameter as Type;
                if (null == type || null == value) return false;
                return type.IsAssignableFrom(value.GetType());
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

     

    The effect is that if I rt click on a view with items of type X in it, I get an appropriate context menu for that type. If I rt click on an grid view with items of type Y, then I get the proper context menu for it.

    That is some arcane WPF knowledge no – you can set it but not create it there??? Where did you come to understand that and what concept am I missing that I still do not understand that rule?

    Imported from legacy forums. Posted by chris (had 2323 views)

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.