1

I have ListView with one column that contains checkboxes

 <ListView Height="164" HorizontalAlignment="Left" ItemsSource="{Binding ProductList}" Name="listView1" VerticalAlignment="Top">
        <ListView.Resources>
            <Style x:Key="DataGridCheckBox" TargetType="{x:Type CheckBox}">
                <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center" />
                <Setter Property="Control.HorizontalContentAlignment" Value="Center" />
                <Setter Property="UIElement.IsEnabled" Value="True" />
                <Setter Property="FrameworkElement.Margin" Value="4" />
                <Setter Property="FrameworkElement.VerticalAlignment" Value="Center" />
                <Setter Property="Control.VerticalContentAlignment" Value="Center" />
            </Style>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn Width="40">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Style="{StaticResource DataGridCheckBox}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn DisplayMemberBinding="{Binding ProductName}" Header="Product Name" Width="120" />
            </GridView>
        </ListView.View>
    </ListView>

How can I disable checkboxes from code? Is it possible to do that with something like: window1.checkBox1.IsEnabled = false; ?

3 Answers 3

1

There are ways to access a control inside DataTemplate through code but that would not be the right way or approach. You shouldn't do this. You should instead use the bindings to disable or enable the checkbox inside ListView

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

Comments

0

You can bind the CheckBox.IsEnabled to a property from the model class.

Xaml:

...
<DataTemplate>
     <CheckBox Style="{StaticResource DataGridCheckBox}" IsEnabled="{Binding Path=IsEnabled}"/>
</DataTemplate>
...

Code behind:

//Your object used in List for ListViews ItemsSource
...
public bool IsEnabled{get;set;}
...

Comments

0

Make a binding to some Property (bool) that represents the isChecked state:

...
<DataTemplate>
  <CheckBox Style="..." IsChecked="{Binding AmIChecked}" />
</DataTemplate>
...

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.