1

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.

1 Answers1

1

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

<Page.Resources>
    <helpers:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</Page.Resources>
<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>
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
  • Thanks, @Andrew I tried this. seems like a simple straightforward solution but After I apply this solution it's creating a Parsing error !! :( – Mahfuzur Rahman Jul 22 '22 at 09:13
  • 1
    It works. I actually created a sample code to check it. My first guess is that you are not declaring the ``BoolToVisibilityConverter`` as a **Resource**. Take a look at this link. https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.data.binding.converter?view=winrt-22621#examples – Andrew KeepCoding Jul 22 '22 at 14:00
  • ok, I'll check it. – Mahfuzur Rahman Jul 25 '22 at 16:25
  • Thanks your Idea works. Yes, I didn't create that Resource. But after declaring the resource followed by you given link it solved – Mahfuzur Rahman Jul 27 '22 at 15:30