Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.8k stars 778 forks source link

How to cal:Message.Attach Menucontext of DataGrid A, DataGrid A is RowDetailsTemplate of DataGrid B #800

Open coolboy195 opened 2 years ago

coolboy195 commented 2 years ago

I am having problem sending action to parent viewmodel class Can someone help me call event ActionContextMenu with the solution?

My code xaml

DataGrid Parent have viewmodel and action


<DataGrid
            Name="TaskList"
            Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type ScrollViewer}}, Path=ActualHeight}"
            MinHeight="200"
            cal:Message.Attach="[Event SelectionChanged] = [Action SelectionChangedItem($eventArgs)];"
            AutoGenerateColumns="False"
            Background="Transparent"
            BorderThickness="0"
            CanUserAddRows="False"
            CanUserReorderColumns="False"
            CanUserSortColumns="True"
            CellStyle="{DynamicResource DataGridCellTasks}"
            ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderTasks}"
            GridLinesVisibility="None"
            HeadersVisibility="Column"
            RowDetailsVisibilityMode="Visible"
            RowStyle="{DynamicResource DataGridRowTasks}"
            SelectionMode="Extended">
            <DataGrid.Columns> 
            ...
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <DataGrid
                        cal:Message.Attach="[Event SelectionChanged] = [Action SelectionChangedJob($eventArgs)];"
                        AutoGenerateColumns="False"
                        Background="Transparent"
                        BorderThickness="0"
                        CanUserAddRows="False"
                        CanUserReorderColumns="False"
                        CanUserSortColumns="True"
                        CellStyle="{DynamicResource DataGridCellTasks}"
                        GridLinesVisibility="None"
                        HeadersVisibility="None"
                        ItemsSource="{Binding Jobs}"
                        RowStyle="{DynamicResource DataGridRowJobs}"
                        SelectionMode="Extended">
                        <DataGrid.Columns>
                        ...
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>

Style of Detail Datagrid
<Style x:Key="DataGridRowJobs" TargetType="DataGridRow">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Background" Value="White" />
        <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}" />
        <Setter Property="cal:Action.TargetWithoutContext" Value="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu ItemsSource="{Binding ContextMenu}" Style="{DynamicResource ContextMenuTasks}" />
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#f2f2f7" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Selector.IsSelectionActive" Value="False" />
                    <Condition Property="Selector.IsSelected" Value="True" />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="#f2f2f7" />
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Selector.IsSelectionActive" Value="True" />
                    <Condition Property="Selector.IsSelected" Value="True" />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="#e5e5ea" />
            </MultiTrigger>
        </Style.Triggers>
    </Style>

MenuContext
<Style x:Key="ContextMenuTasks" TargetType="ContextMenu">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContextMenu">
                    <Border
                        x:Name="Border"
                        Padding="0,3"
                        Background="White"
                        BorderBrush="#e5e5ea"
                        BorderThickness="1"
                        CornerRadius="5">
                        <StackPanel
                            ClipToBounds="True"
                            IsItemsHost="True"
                            KeyboardNavigation.DirectionalNavigation="Cycle" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Resources>
            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
            <Style TargetType="MenuItem">
                <Setter Property="cal:Action.TargetWithoutContext" Value="{Binding Path=PlacementTarget.Tag.DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
                <Setter Property="cal:Message.Attach" Value="[Event Click] = [Action ActionContextMenu($datacontext)]" />
                <Setter Property="Header" Value="{Binding Name}" />
                <Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="MenuItem">
                            <StackPanel>
                                <Separator Background="#e5e5ea" Visibility="{Binding IsSeparator, Converter={StaticResource BooleanToVisibilityConverter}}" />
                                <Border
                                    x:Name="Border"
                                    Height="30"
                                    SnapsToDevicePixels="True">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="40" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid
                                            Width="40"
                                            Height="30"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            Background="Transparent">
                                            <Image
                                                Width="10"
                                                Height="10"
                                                Source="{Binding Icon}"
                                                Stretch="Uniform" />
                                        </Grid>
                                        <Grid
                                            Grid.Column="1"
                                            Height="30"
                                            VerticalAlignment="Center"
                                            Background="Transparent">
                                            <TextBlock
                                                x:Name="TextBlock"
                                                Padding="0,0,35,0"
                                                VerticalAlignment="Center"
                                                Style="{DynamicResource TextBlock12DetailsPrimary}"
                                                Text="{Binding Name}" />
                                        </Grid>
                                    </Grid>
                                </Border>
                            </StackPanel> 
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
    </Style>```
KasperSK commented 2 years ago

When you set cal:Action.TargetWithoutContext in the style does it find the right viewmodel?

coolboy195 commented 2 years ago

When you set cal:Action.TargetWithoutContext in the style does it find the right viewmodel?

Yes bro. You can help me?

KasperSK commented 2 years ago

@coolboy195 If you can make a repro then I could look into your problem. It is a bit hard to tell what is wrong just from a XAML snippet.