none
Problema con WPF DataGrid RRS feed

  • Domanda

  • Buongiorno, scrivo dopo giorni che sto impazzendo per trovare una soluzione al mio problema. Ho una DataGrid WPF all' interno di una UserControl che visualizza i dati di una ObservableCollection (uso Caliburn.Micro quindi BindableCollection) che viene popolata a seguito di click su un pulsante. Il binding funziona a dovere ma la selezione delle varie righe o celle è fluida ed immediata solo se l' utente clicca su una delle celle delle prime 5 colonne mentre se clicca su una cella della colonna 6 o della colonna 7 tale riga non viene evidenziata. Nel debug non risulta nulla, non riesco a capire cosa ci sia che non va. La mia DataGrid è di 9 colonne e circa 30 righe quindi molto leggera. Ho provato di tutto ma non riesco a capire dove possa essere il problema. Posto il codice del model, del viewModel e lo XAML

    Questo è il Model:

        public class LabelModel
        {
            public string stato { get; set; }
            public string id { get; set; }
            public string commessa { get; set; }
            public string parato { get; set; }
            public string proprietario { get; set; }
            public string collezione { get; set; }
            public string disegno { get; set; }
            public string batch { get; set; }
            public int richiesta { get; set; }
        }
    }


    Questo è il ViewModel:

       public class HomeViewModel : PropertyChangedBase
        {
            private BindableCollection<LabelModel> _labels = new BindableCollection<LabelModel>();
            private LabelModel _selectedLabel;
    
            public BindableCollection<LabelModel> Labels
            {
                get { return _labels; }
                set
                {
                    _labels = value;
                    NotifyOfPropertyChange(() => Labels);
                }
            }
            public LabelModel SelectedLabel
            {
                get { return _selectedLabel; }
                set
                {
                    if (_selectedLabel != value)
                    {
                        _selectedLabel = value;
                        NotifyOfPropertyChange(() => SelectedLabel);
                    }
                }
            }
    
    }

    E questo è lo XAML della DataGrid:

    <DataGrid VerticalScrollBarVisibility="Auto" RowHeight="28" GridLinesVisibility="None" 
                      CanUserSortColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" 
                      CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" 
                      RowHeaderWidth="0" BorderThickness="1" BorderBrush="#0033CC" RowBackground="Transparent"
                      Height="836" Width="1250" IsEnabled="True" Background ="Transparent" 
                      Name="OrdersGrid" Opacity="0" HorizontalAlignment="Center" VerticalAlignment="Top" 
                      AutoGenerateColumns="False" ItemsSource="{Binding Labels, IsAsync=True}" IsReadOnly="True" 
                      Padding="10" SelectionMode="Single" SelectionUnit="FullRow" SelectedItem="{Binding SelectedLabel, IsAsync=True}">
                <DataGrid.ColumnHeaderStyle>
                    <Style BasedOn="{StaticResource MetroDataGridColumnHeader}" TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="Background" Value="Black" />
                        <Setter Property="BorderBrush" Value="White"/>
                        <Setter Property="BorderThickness" Value="0,0,0,2"/>
                        <Setter Property="AllowDrop" Value="False"/>
                        <Setter Property="BorderBrush" Value="White"/>
                        <Setter Property="FontSize" Value="14"/>
                        <Setter Property="Height" Value="30" />
                        <Setter Property="Foreground" Value="DarkGray"/>
                        <Setter Property="VerticalContentAlignment" Value="Center"/>
                        <Setter Property="HorizontalContentAlignment" Value="Left"/>
                        <Setter Property="Padding" Value="0,0,0,0"/>
                        <Setter Property="Margin" Value="0,0,0,0"/>
                        <Setter Property="Cursor" Value="Arrow"/>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="Black" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.ColumnHeaderStyle>
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="#0033CC"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="False">
                                <Setter Property="Background" Value="Transparent"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="STATO" Binding="{Binding stato, IsAsync=True, Mode=OneWay}" IsReadOnly="True" Width="60" FontFamily="Segoe UI Symbol" FontSize="24">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left" />
                                <Setter Property="Margin" Value="0,0,0,0" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="0"/>
                                <Setter Property="TextWrapping" Value="NoWrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="COMMESSA" Binding="{Binding commessa, IsAsync=True, Mode=OneWay}" IsReadOnly="True" Width="100" FontSize="14" >
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left" />
                                <Setter Property="Padding" Value="0"/>
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Margin" Value="0,0,0,0"/>
                                <Setter Property="TextWrapping" Value="NoWrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="PARATO" Binding="{Binding parato, IsAsync=True, Mode=OneWay}" IsReadOnly="True" Width="100" FontSize="14">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="0"/>
                                <Setter Property="Margin" Value="0,0,0,0"/>
                                <Setter Property="TextWrapping" Value="NoWrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="ID" Binding="{Binding id, IsAsync=True, Mode=OneWay}" IsReadOnly="True" Width="60" FontSize="14">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="0"/>
                                <Setter Property="Margin" Value="0,0,0,0"/>
                                <Setter Property="TextWrapping" Value="NoWrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="PROPIETARIO" Binding="{Binding proprietario, IsAsync=True, Mode=OneWay}" IsReadOnly="True" Width="180" FontSize="14">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="0"/>
                                <Setter Property="Margin" Value="0,0,0,0"/>
                                <Setter Property="TextWrapping" Value="NoWrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="COLLEZIONE" Binding="{Binding collezione, IsAsync=True, Mode=OneTime}" IsReadOnly="True" Width="210" FontSize="14">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="0"/>
                                <Setter Property="Margin" Value="0,0,0,0"/>
                                <Setter Property="TextWrapping" Value="NoWrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="DISEGNO" Binding="{Binding disegno, IsAsync=True, Mode=OneTime}" IsReadOnly="True" Width="210" FontSize="14">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="0"/>
                                <Setter Property="Margin" Value="0,0,0,0"/>
                                <Setter Property="TextWrapping" Value="NoWrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="BATCH" Binding="{Binding batch, IsAsync=True, Mode=OneWay}" IsReadOnly="True" Width="100" FontSize="14">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="0"/>
                                <Setter Property="Margin" Value="0,0,0,0"/>
                                <Setter Property="TextWrapping" Value="NoWrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>                
                    <DataGridTextColumn Header="PEZZI" Binding="{Binding richiesta, IsAsync=True, Mode=OneWay}" IsReadOnly="True" Width="190" FontSize="14">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="0"/>
                                <Setter Property="Margin" Value="0,0,0,0"/>
                                <Setter Property="TextWrapping" Value="NoWrap"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                </DataGrid.Columns>
                <DataGrid.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding LoadedData}" Value="true">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation
                                                    Storyboard.TargetProperty="(DataGrid.Opacity)"
                                                    From="0.0" To="1.0" Duration="0:0:0.5"  
                                                    AutoReverse="False" RepeatBehavior="1x"  />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                        </Style.Triggers>
                        
                    </Style>
                </DataGrid.Style>
            </DataGrid>

    Per favore datemi qualche dritta perché non so più cosa provare

    Grazie

     
    giovedì 8 agosto 2019 12:57