简体   繁体   中英

How to perform Row update in WinUI3 Datagrid

I am new to WinUI and using CommunityToolkit.WinUI.UI.Controls for my WinUI3 application. Where I am using Datagrid. One of the columns is generating like follows:

 <controls:DataGridTextColumn Binding="{Binding MessageId}" Header="Id" />

I also have a Button column generated for the action in the grid as follows:

 <controls:DataGridTemplateColumn Header="Action">
                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Click="SendAsync" CommandParameter="{Binding Path=MessageId}">Send</Button>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>
                </controls:DataGridTemplateColumn>
            </controls:DataGrid.Columns>

My problem is that this button should only be visible based on the column "Status" from the Source. After searching on the internet I could not find any solution. I used to use row_update on the WinForm application where we can use conditional visibility based on any cell value.

Please suggest to me how to overcome this kind of problem for conditional visibility/ Row_update like functionality in WinUI3.

You can create a Converter and control the Button 's Visibility property like this.

BoolToVisibilityConverter.cs

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return ((bool)value) is true
            ? Visibility.Visible
            : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language) => throw new NotImplementedException();
}

Your DataGrid

<controls:DataGridTemplateColumn Header="Action">                    
    <controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button
                Click="SendAsync"
                CommandParameter="{Binding Path=MessageId}"
                Visibility="{Binding Status, Converter={StaticResource BoolToVisibilityConverter}}"/>>
                Send
            </Button>
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM