This is a migrated thread and some comments may be shown as answers.

OverlayBrushesNeeded not working in EventToCommandBehavior

4 Answers 122 Views
VirtualGrid
This is a migrated thread and some comments may be shown as answers.
Sergey
Top achievements
Rank 2
Sergey asked on 28 Jan 2020, 09:33 AM

Hello.

Sorry for my "English" :)

VirtualGrid was added in xaml markup and it was subscribed to OverlayBrushesNeeded using EventToCommandBehavior.

<telerik:RadVirtualGrid
                    Name="VirtualGrid"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    telerik:StyleManager.Theme="Office2016"
                    CanUserInsertRows="False"
                    CanUserEdit="True"
                    InitialRowCount="10"
                    InitialColumnCount="10"
                    MeasureTextOnRender="True"
                    DataProvider="{Binding DataProvider}"
                    LinesVisibility="Both"
                    CanUserSortColumns="False"
                    CanUserFilterColumns="False"
                    RowHeaderWidth="0"
                    ColumnWidth="150"
                    Grid.Column="2">
                    <telerik:RadVirtualGrid.ContextMenu>
                        <ContextMenu ItemsSource="{Binding ContextMenuModel.Items}" ></ContextMenu>
                    </telerik:RadVirtualGrid.ContextMenu>
                    <telerik:EventToCommandBehavior.EventBindings>
                        <telerik:EventBinding PassEventArgsToCommand="True" Command="{Binding VirtualGridOverlayBrushesNeeded}"
                                              EventName="OverlayBrushesNeeded"/>
                        <telerik:EventBinding PassEventArgsToCommand="True" Command="{Binding VirtualGridCellDecorationsNeeded}"
                                              EventName="CellDecorationsNeeded" />
                    </telerik:EventToCommandBehavior.EventBindings>
                </telerik:RadVirtualGrid>     

 

 

There are 2 commands in my ViewModel.

public ICommand VirtualGridCellDecorationsNeeded
{
    get => _virtualGridCellDecorationsNeeded;
}
 
public ICommand VirtualGridOverlayBrushesNeeded
{
    get => _virtualGridOverlayBrushesNeeded;
}

 

_virtualGridCellDecorationsNeeded = new Telerik.Windows.Controls.DelegateCommand(OnCellDecorationsNeeded);
_virtualGridOverlayBrushesNeeded = new Telerik.Windows.Controls.DelegateCommand(OnOverlayBrushesNeeded);
public void OnCellDecorationsNeeded(object sender)
 {
     if(sender is CellDecorationEventArgs args)
     {
         if(args.ColumnIndex % 2 == 0)
         {
             args.Background = Brushes.Red;
             args.Foreground = Brushes.Black;
             args.CellTextAlignment = TextAlignment.Left;
         }
         else
         {
             args.Background = Brushes.Yellow;
             args.Foreground = Brushes.Red;
             args.CellTextAlignment = TextAlignment.Left;
         }
     }
 }
 
 public void OnOverlayBrushesNeeded(object sender)
 {
     if (sender is OverlayBrushesEventArgs args)
     {
         args.Brushes.Add(Brushes.Red);
         args.Brushes.Add(Brushes.Yellow);
     }
 }

 

CellDecorationsNeeded is triggered, but OverlayBrushesNeeded is not, which makes it impossible to set colors for cells. Please tell me what I'm doing wrong.

4 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 31 Jan 2020, 08:22 AM

Hello Sergey,

I've tested this behavior with the VirtualGrid MVVM SDK example, but I wasn't able to reproduce the issue. The command bound to the OverlayBrushesNeeded event is fired. I've attached my test project here. Can you try it and see if I am missing anything?

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sergey
Top achievements
Rank 2
answered on 31 Jan 2020, 12:21 PM

Hello Martin,
Thank you for your answer, I realized what the problem is. If you fill in the DataContex field through xaml everything will work, but if this field is filled in programmatically, then it does not work.

this does not work:

<Window x:Class="MVVM_VirtualGrid.MainWindow"
        x:ClassModifier="internal" 
        Title="MainWindow" Height="600" Width="525">
    <Grid Name="Grid">
        <telerik:RadVirtualGrid DataProvider="{Binding DataProvider}"
                                x:Name="vg"
                                telerik:StyleManager.Theme="Office2016"
                                CanUserInsertRows="False"
                    CanUserEdit="True"
                    InitialRowCount="10"
                                LinesVisibility="Both"
                    CanUserSortColumns="False"
                    CanUserFilterColumns="False"
                                ColumnWidth="150"
                    RowHeaderWidth="0"
                    MeasureTextOnRender="True"
                                Margin="5" >
            <telerik:EventToCommandBehavior.EventBindings>
                <telerik:EventBinding PassEventArgsToCommand="True" Command="{Binding VirtualGridOverlayBrushesNeeded}"
                                              EventName="OverlayBrushesNeeded"/>
                <telerik:EventBinding PassEventArgsToCommand="True" Command="{Binding VirtualGridCellDecorationsNeeded}"
                                              EventName="CellDecorationsNeeded" />
            </telerik:EventToCommandBehavior.EventBindings>
        </telerik:RadVirtualGrid>
    </Grid>
</Window>

 

namespace MVVM_VirtualGrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    internal partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var model = new MyViewModel();
            this.Grid.DataContext = model;
        }
    }
}
0
Martin Ivanov
Telerik team
answered on 03 Feb 2020, 02:46 PM

Hello Sergey,

You are right. It seems that the command attached to the OverlayBrushesNeeded event is not fired if the DataContext is set after the control is initialized. This happens because the event is fired very early, just before the evaluation of the Command property binding is finished. So, when the event is fired, there is no Command value and this is why the command is not executed.

To work this around, set the DataContext before the InitializeComponent() call.

internal partial class MainWindow : Window
{
	public MainWindow()
	{
		var model = new MyViewModel();
		this.Grid.DataContext = model;
		InitializeComponent();		
	}
}

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sergey
Top achievements
Rank 2
answered on 04 Feb 2020, 06:13 AM

Hello Martin,

Thank you, I found this method later but did it differently. I signed up for the Loaded event and called Reset on VirtualGrid.

For example:     

private void OnGridLoaded(object obj)
        {
            if (obj is RoutedEventArgs routedEvent)
            {
                if (routedEvent.Source is RadVirtualGrid grid)
                {
                    grid.Reset();
                }
            }
        }

 

This solved my problem.

Tags
VirtualGrid
Asked by
Sergey
Top achievements
Rank 2
Answers by
Martin Ivanov
Telerik team
Sergey
Top achievements
Rank 2
Share this question
or