In attempting to bind a IsSelectable
INPC Property via a setter in a Listview based on this answer.
The said answer is for a ComboBoxItem which works great, im trying to refactor for a ListViewItem which is not working.
Setting IsSelectable
true or false on any item in the ViewModel.ItemsOC doenst work as the list view item is still selectable. What am I missing?
XAML
<ListView
ItemsSource="{x:Bind ViewModel.ItemsOC, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="LV_Item_SelectionChanged">
<ListView.Resources>
<Style BasedOn="{StaticResource DefaultListViewItemStyle}" TargetType="ListViewItem">
<Setter Property="cus_ctrls:LVI_BindingHelper.IsEnable" Value="IsSelectable" />
</Style>
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Binding Helper
public class LVI_BindingHelper
{
public static string GetIsEnable(DependencyObject obj)
{
return (string)obj.GetValue(IsEnableProperty);
}
public static void SetIsEnable(DependencyObject obj, string value)
{
obj.SetValue(IsEnableProperty, value);
}
// Using a DependencyProperty as the backing store for IsEnable. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsEnableProperty =
DependencyProperty.RegisterAttached("IsEnable", typeof(string), typeof(LVI_BindingHelper), new PropertyMetadata(null, GridBindingPathPropertyChanged));
private static void GridBindingPathPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var propertyPath = e.NewValue as string;
if (propertyPath != null)
{
var bindingProperty =
e.Property == IsEnableProperty
? ListViewItem.IsEnabledProperty
: null;
BindingOperations.SetBinding(
obj,
bindingProperty,
new Binding { Path = new PropertyPath(propertyPath) });
}
}
}
ViewModel
bool _isSelectable;
public bool IsSelectable
{
get => _isSelectable;
set => Set(ref _isSelectable, value);
}