[WPF - C7] - Problem with datagridview

BlackSun 21 Reputation points
2020-04-30T15:51:54.417+00:00

Hi,
I have a datagrid and when I drag fastly the vertical scroll bar to the bottom it shows like that
7902-immagine-1.jpg

If I click on "Install" (it's a datagrid cell with a button inside), it doesn't work and I get that
7836-immagine-2.jpg

If now I click on "Install" it works... but it's not what i want!

How can I fix this problem?
I would like the button start immediatly.

Thank you very much

Cheers, Blacksun

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-04-30T17:11:46.9+00:00

    Hi, I cannot reproduce your problem without additional information. Try following Demo:

    <Window x:Class="WpfApp1.Window34"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp34"
            mc:Ignorable="d"
            Title="Window34" Height="450" Width="800">
      <Window.Resources>
        <local:ViewModel x:Key="vm"/>
      </Window.Resources>
      <Grid DataContext="{StaticResource vm}">
        <DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False">
          <DataGrid.Columns>
            <DataGridTemplateColumn Header="Button">
              <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                  <Button Content="{Binding Col1}" 
                          Command="{Binding Cmd, Source={StaticResource vm}}" 
                          CommandParameter="{Binding Col2}"/>
                </DataTemplate>
              </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Col1" Binding="{Binding Col1}"/>
            <DataGridTextColumn Header="Col2" Binding="{Binding Col2}"/>
          </DataGrid.Columns>
        </DataGrid>
      </Grid>
    </Window>
    

    Classes:

    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Input;
    
    namespace WpfApp34
    {
      public class ViewModel
      {
        public ViewModel()
        {
          ObservableCollection<Data> col = new ObservableCollection<Data>();
          for (int i = 0; i < 10; i++) col.Add(new Data() { Col1=$"Row {i}", Col2=$"Data {i}"});
          cvs.Source = col;
        }
        public ICollectionView View { get => cvs.View; }
        private CollectionViewSource cvs = new CollectionViewSource();
    
        public ICommand Cmd { get => new RelayCommand((state) => Debug.WriteLine(state.ToString())); }
      }
    
      public class Data
      {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
    
      }
    
      public class RelayCommand : ICommand
      {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _action;
        public RelayCommand(Action<object> action) : this(action, null) { }
        public RelayCommand(Action<object> action, Predicate<object> canExecute) { _action = action; _canExecute = canExecute; }
        public void Execute(object o) => _action(o);
        public bool CanExecute(object o) => _canExecute == null ? true : _canExecute(o);
        public event EventHandler CanExecuteChanged
        {
          add { CommandManager.RequerySuggested += value; }
          remove { CommandManager.RequerySuggested -= value; }
        }
      }
    }
    
    0 comments No comments