WPF ValueConverter Binding - something is wrong

Imagine you have a list of user settings. Depending on what choices the user made, other options may not be available.

I have a ValueConverter that can potentially handle this. If he knows what choices will be made, he will return a value indicating whether the item will still be selected.

The problem is that the only place where you can find out what user settings are in the ViewModel for this screen. But no problem. I will make the converter a property in ViewModel, and in the constructor for the converter I will pass a link to the ViewModel so that the converter can check the list of selected elements at any time.

The problem that I am currently facing is that it does not actually perform any transformations.

To weld it: ViewModel has a MySelectionConverter property of type IValueConverter. ViewModel has a list of selected items. The view (such as MyScreen, which inherits from UserControl) has a list with elements.

The ItemTemplate element looks something like this:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid Height="Auto" Width="100" VerticalAlignment="Top"  Visibility="{Binding Path=DataContext.MySelectionConverter, RelativeSource={RelativeSource AncestorType={x:Type MyScreen}}}">
            <TextBlock Text="The user might want to select me." />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

I suspect something is wrong with my binding. But maybe you just can't do it. Any help is appreciated.

Edit: Based on the information I received so far, my binding was incorrect. But it turns out that I cannot do what I am trying to do. Essentially, I tried to install the converter using bindings using:

Visibility="{Binding Converter={Binding Path=DataContext.StyleOptionConverter, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:StyleSelectionScreen}}}}

Visual studio tells me:

"" "" 'Binding'. "" DependencyProperty DependencyObject.

, , , : ? , ViewModel , .

+3
2

, MultiBinding IMultiValueConverter.

XAML :

<Grid.Visibility>
    <MultiBinding Converter="{StaticResource styleOptionConverter}">
        <Binding />
        <Binding ElementName="UserControl" Path="DataContext" />
    </MultiBinding>
</Grid.Visibility>

, , RelativeSource, ElementName, , .

- , :

{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:StyleSelectionScreen}}}

, MultiBinding, ! , , .

+2

. , Path

:

<local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>

:

<DataGrid ItemsSource="{Binding Path=Tracks, IsAsync=True}" AutoGenerateColumns="False" Height="130" HorizontalAlignment="Left" Name="dataGrid2" 
                                  Visibility="{Binding Path=ShowSongs, Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay}" GridLinesVisibility="Vertical" 
                                  AlternatingRowBackground="{StaticResource Background}">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Song" Width="*" Binding="{Binding Name}" />
                                <DataGridTextColumn Header="Artist"  Width="*" Binding="{Binding Artist}" />
                                <DataGridTextColumn Header="Album" Width="*" Binding="{Binding Album}" />
                            </DataGrid.Columns>
                        </DataGrid>

,

public class BooleanToVisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (true.Equals(value)) ? Visibility.Visible : Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
+3

All Articles