0

I have a checkbox that should only appear when a menu option is selected. When the menu option is not selected, the checkbox should be hidden. I have a visibility variable setup in the code:

private Visibility _checkboxVisibility;
public Visibility CheckboxVisibility 
{ 
    get 
    { 
        return _checkboxVisibility; 
    } 
    set 
    { 
        SetProperty(ref _checkboxVisibility, value);
        OnPropertyChanged(nameof(CheckboxVisibility));
    } 
}

When I initialize the view model I make sure that this is set to Hidden. It is set to Visible when the menu option is selected.

Here is a snippet of the XAML code:

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="2*" />
                <ColumnDefinition Width="2*" />
                <ColumnDefinition Width="7*" />
                <ColumnDefinition Width="2*" />
                <ColumnDefinition Width="2*" />
            </Grid.ColumnDefinitions>
            <CheckBox Grid.Column="0"
                      IsChecked="{Binding IsSelected, Mode=TwoWay}"
                      Visibility="{Binding CheckboxVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

There are more columns but this seems like the important bit. If i set it to straight Hidden, the checkbox will be hidden, so there is something wrong with my binding, but I'm not sure what. I've tried also binding it to a Boolean and using a converter, but that didn't seem to help either. I've tried looking for an answer online, but everywhere I go I get binding visibility of another item to a checkbox. Any help would be appreciated.

1 Answer 1

0

All right, I found the answer, and it turns out I didn't give enough clarification for anyone to help me. The CheckboxVisibility property was not on the ItemSource binding for the listview. It was trying to bind to it like it was. I had to use a RelativeSource property like so:

Visibility="{Binding DataContext.MassImportModeVisibility, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

I will try and give enough info in the future.

Not the answer you're looking for? Browse other questions tagged or ask your own question.