سؤال

I have the following scenario where I am trying to filter items based on a selected item.

Here is the sample which is listbox bound to items:

<ListBox ItemsSource="{Binding Source={StaticResource MyCollectionViewSource}, Mode=OneWay}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel Visibility="{Binding Path=., Converter={StaticResource MyVisibilityConverter}}">
                <CheckBox IsChecked="{Binding IsChecked}" DockPanel.Dock="Left" />
                <TextBlock Text="{Binding Name}" VerticalAlignment="Center" />
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The above list box has item of type users.

What I want to do is filter out the current user from the list (Set the visibility if the user currently logged in is the same as the user getting bound in the listbox)

This way the user cannot add himself for some task. He can only add users other than him.

I tried using the converter parameter to bind to the CurrentUser property in the viewmodel and I get this error

A 'Binding' cannot be set on the 'ConverterParameter' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

So I assume I can't do this. Is there a way I can achieve this by binding or I have to perform the filter in the list before it is bound?

هل كانت مفيدة؟

المحلول

Yes, the ConverterParameter is not a dependency property so you can't bind to it. You can use a MultiBinding to bind the list of users and the current user to the Visibility property. Then use a IMultiValueConverter to determine the visibility from the multiple bindings.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top