2

I have a ListBox with a DataTemplate like this:

        <ListBox  ItemsSource="{Binding Reportes,Mode=TwoWay}"  >
        <ListBox.ItemTemplate>
            <DataTemplate >
                <StackPanel   IsEnabled="{Binding PantallaActiva}">
                    <CheckBox FontWeight="Bold" HorizontalAlignment="Left" 
                              Content="{Binding NORepo,Mode=TwoWay }" 
                              IsChecked="{Binding Path=EmitirReporte,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                    <TextBlock FontSize="9" Foreground="Gray" TextWrapping="Wrap" Text="{Binding DSRepo,Mode=TwoWay}" MaxWidth="140"  />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>

The property "PantallaActiva" is a true/false property in my ViewModel that is set to False when some process starts. I need to disable the checkBoxes when that process starts. My problem is that never happens the checkboxes or the stack panel ,can`t see the PantallaActiva property or any other property in my viewModel, they only see the properties in the items collection Reportes[i] and that's, I guess, my problem, so how do I have to do the binding ?

thanks in advance

2 Answers 2

1

If I understand your question correctly, the PantallaActiva property isn't part of your Reporte class (the elements in your collection). It's probably a property on your view model instead, right?

The scope of your ItemTemplate is bound to the type Reporte since you bound that collection as the ItemsSource. That's why XAML binding cannot find the property on that type. You need to bind the IsEnabled property to the property on your ViewModel.

<CheckBox IsEnabled="{Binding Path=DataContext.PantallaActiva, RelativeSource={RelativeSource AncestorType=MyControlType}}" .../>

For this to work, you need to set MyControlType to an element up your visual tree whose DataContext is bound to your view model. (probably the parent of your ListBox)

It would be helpful to see the code in your view model.

Sign up to request clarification or add additional context in comments.

1 Comment

Excelent!!!! thanks, your interpretation was right. !!! Works as spected!!! thanks a lot my friend!!! MyControlType was ListBox, and everything works now!
0

replace with this. You only need to assign value as false, as by default, it was true.

IsChecked="{Binding Path=EmitirReporte,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,Value=False}"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.