For ListView Control IsEnable property through ViewModel in WinUi3 dynamic binding is not happening

Uppala, Sreekanth - Dell Team 1 Reputation point
2022-11-28T04:57:09.983+00:00

[ListView] When am trying to modify the IsEnable property through the ViewModel it is not changing the value.
Instead of dynamic binding, statically if I am passing the Value like IsEnable=False is working fine.

Ex:

ListViewXaml:<ListView x:Name="TabularView" Grid.Row="0"
HorizontalAlignment="Left"
Margin="10,0,0,0"
VerticalAlignment="Top"
Height="Auto"
CanDrag="False"
CanReorderItems="False"
CanDragItems="False"
IsEnabled="True"
ItemsSource="{Binding TabItems, Mode=TwoWay}"
TabNavigation="Local"
TabIndex="0"
SelectedItem="{Binding SelectedTabItem, Mode=TwoWay}"
SingleSelectionFollowsFocus="True"
Style="{StaticResource ListView_GameTitle_TabsStyle}">
</ListView>

ListViewStyle:

![264664-image.png]2

TabViewItemModel:
265065-image.png

ViewModel.cs

private ObservableCollection<TabViewItemModel> _tabItems = new();
public ObservableCollection<TabViewItemModel> TabItems
{
get { return _tabItems; }
set
{
_tabItems = value;
OnPropertyChange();
}
}

private void TabItemsAdd()  
    {  


        if (true)  
        {  
            TabItems.Add(new TabViewItemModel  
            {  
                ID = Constants.GAMETITLE_TAB_PERFORMANCE,  
                Header = _localizationHelper.GetString("Sub_Perf_Name"),  
                PluginId = Constants.PERFORMANCE_SYSTEM_ID,  
                IsEnabled=true  
            });  
        }  

        if (true)  
        {  
            if (isLightingPresent)  
            {  
                TabItems.Add(new TabViewItemModel  
                {  
                    ID = Constants.GAMETITLE_TAB_FX,  
                    Header = _localizationHelper.GetString("Sub_FX_Name"),  
                    PluginId = Constants.FX_SYSTEM_ID,  
                    IsEnabled = false  
                });  
            }  

            if (true)  
            {  
                TabItems.Add(new TabViewItemModel  
                {  
                    ID = Constants.GAMETITLE_TAB_DEVICEFEATURES,  
                    Header = _localizationHelper.GetString("Sub_FXDeviceFeature_Name"),  
                    PluginId = Constants.FX_SYSTEM_ID,  
                    IsEnabled = true  
                });  
            }  
        }  
    }
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
749 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 16,551 Reputation points Microsoft Vendor
    2022-12-05T08:51:45.843+00:00

    Hello,
    Welcome to Microsoft Q&A!

    In document Setter Class.

    The Windows Runtime doesn't support a Binding usage for Setter.Value (the Binding won't evaluate and the Setter has no effect, you won't get errors, but you won't get the desired result either).

    You can use Binding in DateTemplate.
    Here is a sample. I use Textbox in DateTemplate for testing, you can change it according to your needs.
    <TextBox Text="{Binding Header,Mode=TwoWay}" IsEnabled="{Binding IsEnabled,Mode=TwoWay}" Width="300" Padding="0,5,0,0"/>
    267156-gif-2022-12-5-16-48-39.gif
    MainWindow.xaml

    <Grid>  
    
       <Grid.Resources>  
            <local:TabViewItems x:Key="tabViewItems"/>  
        </Grid.Resources>  
    
       <ListView Name="testListView"  ItemsSource="{StaticResource tabViewItems}" Width="350" Margin="0,5,0,10" SelectionChanged="ListView_SelectionChanged">  
            <ListView.ItemTemplate>  
                <DataTemplate>  
                    <TextBox Text="{Binding Header,Mode=TwoWay}" IsEnabled="{Binding IsEnabled,Mode=TwoWay}" Width="300" Padding="0,5,0,0"/>  
                </DataTemplate>  
            </ListView.ItemTemplate>  
        </ListView>  
          
    </Grid>  
    

    MainWindow.xaml.cs

    private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)  
        {  
              
            if (e.AddedItems.Count >= 1)  
            {  
                TabViewItem SelectedItem;  
    
               SelectedItem = e.AddedItems[0] as TabViewItem;  
    
               if (SelectedItem.IsEnabled == true)  
                {  
                    SelectedItem.IsEnabled = false;  
                    SelectedItem.Header = "SelectionChanged IsEnabled = false";  
                }  
                else  
                {  
                    SelectedItem.IsEnabled = true;  
                    SelectedItem.Header = "SelectionChanged IsEnabled = true";  
                }  
                                 
            }  
        }  
    }  
    
    public class TabViewItems : ObservableCollection<TabViewItem>  
    {  
        public TabViewItems()  
        {  
            Add(new TabViewItem("1", "Anderberg",  
                    "1233" , false));  
            Add(new TabViewItem("2", "Ashton",  
                    "23323", false));  
            Add(new TabViewItem("3", "Jun",  
                    "325435", true));  
            Add(new TabViewItem("4", "Pica",  
                    "756546", true));  
        }  
    }  
    
    public class TabViewItem : INotifyPropertyChanged  
    {  
        private bool isEnabled;  
        private String header;  
    
        public event PropertyChangedEventHandler PropertyChanged = delegate { };  
        public String Id { get; set; }  
        public String Header  
        {  
            get { return this.header; }  
            set  
            {  
                this.header = value;  
                this.OnPropertyChanged();  
            }  
        }  
        public String PluginId { get; set; }  
    
       public bool IsEnabled  
        {  
            get { return this.isEnabled; }  
            set  
            {  
                this.isEnabled = value;  
                this.OnPropertyChanged();  
            }  
        }  
    
       public TabViewItem(String Id, String Header, String PluginId, bool IsEnabled)  
        {  
            this.Id = Id;  
            this.Header = Header;  
            this.PluginId = PluginId;  
            this.IsEnabled = IsEnabled;  
        }  
    
       public void OnPropertyChanged([CallerMemberName] string propertyName = null)  
        {  
            // Raise the PropertyChanged event, passing the name of the property whose value has changed.  
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
        }  
     }  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments