1

I have a Community Toolkit datagrid with a button. Clicking the button fires an event that calls a method that updates the Selected property bound to the button's Text. The bound item is a property of the SelectedAssignUsersGrid property bound to the data grid's SelectedItem.

My goal is that the button's text will update in the data grid UI once the Selected property changes programmatically. The problem is that it doesn't change when the property changes in most cases, but it is quirky. If I double-click the button, it sometimes changes, but doesn't always match the actual property value. Is the data grid hopelessly quirky, or is there a way to have the UI match the bound properties as they change?

XAML Datagrid:

            <controls:DataGrid x:Name="dataGridAssignUsers_Temp" 
                    Margin="12" GridLinesVisibility="All" HorizontalGridLinesBrush="PowderBlue" VerticalGridLinesBrush="PowderBlue"
                    BorderBrush="PowderBlue" ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                    AutoGenerateColumns="False" 
                    SelectedItem="{x:Bind ViewModel.SelectedAssignUsersGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                    ItemsSource="{x:Bind ViewModel.AssignUsersGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                <controls:DataGrid.Columns>
                    
                    <controls:DataGridTemplateColumn Header="Select" Width="75">
                        <controls:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate x:DataType="viewmodels:ProjectViewModel">
                                <Grid>
                                    <Button Click="DataGridAssignUserSelect_Click"  HorizontalContentAlignment="Left" 
                                            VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                                        <TextBlock>                                                        
                                            <Run FontSize="12" Text="{Binding Selected,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                        </TextBlock>
                                    </Button>
                                </Grid>
                            </DataTemplate>
                        </controls:DataGridTemplateColumn.CellTemplate>
                    </controls:DataGridTemplateColumn>
                  
                    <controls:DataGridTextColumn   IsReadOnly="True"
                        Header="AD User Name" 
                        Width="250"
                        Binding="{Binding UserAdazureAduserName}" 
                        FontSize="14" />
                    <controls:DataGridTextColumn   IsReadOnly="False"
                        Header="User Initials" 
                        Width="250"
                        Binding="{Binding AppUserInitials}" 
                        FontSize="14" />
                </controls:DataGrid.Columns>                        
            </controls:DataGrid>

Code Behind Event:

    private void DataGridAssignUserSelect_Click(object sender, RoutedEventArgs e)
    {           
        ViewModel.SelectAssignableUser();
        dataGridAssignUsers.UpdateLayout();
    }

ViewModel Code called from above event:

public partial class ProjectViewModel : ObservableObject, IDisposable
{
    ...

    [ObservableProperty]
    private ObservableCollection<TempUser> _assignUsersGrid; //Project users datagrid source.

    [ObservableProperty]
    public TempUser _selectedAssignUsersGrid = new(); //Item selected in the datagrid.

    public void SelectAssignableUser()
    {
        if (SelectedAssignUsersGrid is null) return;
        SelectedAssignUsersGrid.Selected = !SelectedAssignUsersGrid.Selected;
    }
    ...
}

For reference, here is the underlying model class that the grid is bound to:

public class TempUser
{
    public bool Selected { get; set; }
    public string AppUserInitials { get; set; }
    public string UserAdazureAduserName { get; set; }

    public TempUser() { }
}
E. A. Bagby
  • 824
  • 9
  • 24
  • It seems that you want to change selections using ``SelectedItem`` in the ``DataGrid`` but I don't understand why you need buttons in each row. I guess you get weird behaviors because you have 2 ways to change selections. – Andrew KeepCoding Apr 18 '23 at 00:27
  • @AndrewKeepCoding I'm using a button because this was my attempt to work around the check box problem I was having that won't resolve itself (per separate post). When you say "two ways" to change selections, do you mean by picking rows and clicking buttons? I can figure that out, but I can't figure out why the data grid is not matching the values in the underlying bound `TempUser` objects for the `Selected` property. – E. A. Bagby Apr 18 '23 at 13:54
  • Essentially, I'm looking for a way to force the data in the data grid UI to match the data in the code-modified object models the UI is bound to. That's essentially my issue. I tried a check box being bound to `selected`, and it did not update either when I updated the object model. So, it seems I'm missing something when it comes to the data grid UI refreshing. I'm beginning to wonder if this data grid control is not great. Or maybe the problem is in the lack of documentation. – E. A. Bagby Apr 18 '23 at 13:56
  • 1
    If that's the case, you need to make the ``Selected`` observable using ``ObservableProperty`` or wrap ``TempUser`` like [this](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/observableobject#:~:text=is%20being%20updated.-,Wrapping%20a%20non%2Dobservable%20model,-A%20common%20scenario). – Andrew KeepCoding Apr 18 '23 at 21:18
  • I'll give it a try and follow up. – E. A. Bagby Apr 19 '23 at 21:17
  • Have you got any updates about this issue? – Jeaninez - MSFT Apr 26 '23 at 07:17
  • No. Let us know if you make any progress. – E. A. Bagby May 02 '23 at 20:09

0 Answers0