Search code examples
c++xamlwinrt-xamluwp-xamlc++-winrt

UWP C++WinRT How to disable a single item in a GridView


I have a <GridView/> with ItemsSource bind to IObservableVector<DeviceViewModel>. I want to disable only few of the items in this GridView. How can I do that? I saw other similar questions but they seem not be complete and not in C++/WinRT.

Following is my GridView

<GridView x:Name="DeviceTargets"
    x:Uid="SDeviceTargets"
    Grid.Row="1"
    x:DeferLoadStrategy="Lazy"
    Visibility="Collapsed"
    Margin="12 14 12 10"
    IsItemClickEnabled="True"
    ItemClick="OnDeviceTargetsItemClicked"
    ItemContainerStyle="{StaticResource GridViewItemStyleSV}"
    ItemsSource="{x:Bind ViewModel.DeviceList, Mode=OneWay}"
    Padding="0"
    SelectionMode="None"
    ScrollViewer.HorizontalScrollMode="Disabled"
    ScrollViewer.VerticalScrollMode="Disabled">
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="viewModels:DeviceViewModel">
            <local:DeviceItem ViewModel="{x:Bind}"/>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

Solution

  • Please try the following code:

    // TestOrder is the data model you used
    auto container = m_GridView().ContainerFromItem(TestOrder);
    Windows::UI::Xaml::Controls::GridViewItem item = container.as<Windows::UI::Xaml::Controls::GridViewItem>();
    item.IsEnabled(false);
    

    Make sure to call this when the target item is loaded.

    The result looks like this:

    enter image description here