开发者

Finding checkboxes inside a gridview in wpf

I have the following code:

<ListView Name="lvwYears"
          Margin="0,32,191,29"
          ItemsSource="{Binding Path=YearCollection}">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumnHeader>
                    <CheckBox Click="CheckBox_Checked"
                              HorizontalAlignment="Left"
                              Content="Year"
                              Padding="50,0,0,0"
                              Margin="20,0,0,0"></CheckBox>
                </GridViewColumnHeader>
                <GridViewColumn.CellTemplate&g开发者_高级运维t;
                    <DataTemplate>
                        <CheckBox Click="CheckBoxIndividual_Click"
                                  Name="cbxYears"
                                  VerticalAlignment="Center"
                                  Content="{Binding}"
                                  HorizontalAlignment="Left"
                                  Padding="50,0,0,0"
                                  Margin="20,0,0,0" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>

I want to iterate through the checkboxes and check them whenever the CheckBox in the GridViewcolumnHeader is checked. I've tried using the VisualTree and have failed, I get errors in that the element must be Visual or Visual3D.

Any help will be greatly appreciated. Thanks!


It doesn't make sense to check the checkboxes without their IsChecked being bound to something. Change your ItemsSource to a collection of structs with your integer and a bool. When the checkbox is checked, iterate through and set all the bools to true.

If you really insist on leaving your integer list in place, then as a total hack you could use the Loaded event in each checkbox to record it in a list and then check everything in the list when the top checkbox was checked, but I would not recommend this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜