question

BlackSun-5322 avatar image
0 Votes"
BlackSun-5322 asked ·

[WPF - C7] - Problem with datagridview

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-wpf
immagine-1.jpg (29.0 KiB)
immagine-2.jpg (20.6 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered ·

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


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.