I tried searching through a lot of threads and I've found several that's pretty close but not exactly what I want. After a day of searching, I just decided to ask. Apologies if I missed something, I feel like this would be common enough but I can't seem to quite get it.
I have UserControl that bounded to a ViewModel, and it has a Listbox with ItemsSource=ObservableCollection which is a property of the ViewModel like below:
Whenever I select an item, I call SomeObject.IsEnabled = false on several other items depending on certain conditions. I'd like to bind the listbox items to that IsEnabled property so I can grayout whichever items whenever I do a selection.
ViewModel:
Class ViewModel : PropertyNotificationObject
{
private ObservableCollection<SomeObject> m_list;
public ObservableCollection<SomeObject> List {get; set;} //notifying properly
private void selectedItem()
{
//several in SomeObjects in List sets IsEnabled = false
}
}
The Object Class
class SomeObject : PropertyNotificationObject
{
private bool m_isEnabled;
public IsEnabled { get; set; } //notifying properly
}
XAML
<DataTemplate x:Key="ListTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ., Converter={someConverterObjectToString}}"/>
</Grid>
</DataTemplate>
<ListBox ItemsSource="{Binding List}" ItemTemplate="{StaticResource ListTemplate}"/>
I've tried using StyleTriggers in the ListBox.ItemContainerStyle and the DataTemplate like below but I couldn't figure out how to get to the SomeOject.IsEnabled property.
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding={???????? I can't get to my SomeObject properties.}
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
Sorry for the lack of colors, I'm new here and don't really know how use the editor very well.
Thanks in advance.
{Binding IsEnabled}
in your ItemContainerStyle
should do the work. Look at VS debug window for binding errors
Edit or directly bind the IsEnabled property of the ListBoxItem:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
</Style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With