如何获取ListView CheckBox列状态

时间:2017-10-19 12:19:48

标签: wpf listview checkbox

所以我if let someView = array.first as? SomeView { print(someView.foo2) } ListView CheckBox

Column

我的模型有多个<GridViewColumn Header="Name"> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Margin="0,0,0,0"> <CheckBox Margin="0,0,0,0"/> <TextBlock x:Name="textBlock" Text="{Binding Name}" Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}" Style="{StaticResource TextBlockDefaultStyle}" Margin="0,2,0,0"/> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> ,例如propertiesName等,而我的Description也包含简单的ListView

我的Checkbox填充了几个对象,我希望实现两件事:

  1. 如果我检查具体的ListView,我希望所有其他CheckBox成为CheckBox(如果我有另一个Uncheck处于Checked状态)

  2. 如何确定特定的CheckBox对象

1 个答案:

答案 0 :(得分:1)

XAML:

<ListView x:Name="MyListView" SelectionChanged="MyListView_OnSelectionChanged" SelectionMode="Single">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" Width="100">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox>
                                <CheckBox.IsChecked>
                                    <Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
                                </CheckBox.IsChecked>
                            </CheckBox>
                            <TextBlock Text="{Binding Name}"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Description" Width="100">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Description}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // some data for testing
        MyListView.ItemsSource = new List<MyData>
        {
            new MyData {Name = "123", Description = "ABC"},
            new MyData {Name = "456", Description = "EFG"},
            new MyData {Name = "789", Description = "HIJ"},
        };
    }

    private void MyListView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems != null && e.AddedItems.Count > 0)
        {
            var selectedItem = (MyData)e.AddedItems[0];
            // do something
        }
    }
}

public class MyData
{
    public string Name { get; set; }

    public string Description { get; set; }
}

添加:在检查其他CheckBox时取消选中其他<CheckBox Checked="ToggleButton_OnChecked"> <CheckBox.IsChecked> <Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/> </CheckBox.IsChecked> </CheckBox> private void ToggleButton_OnChecked(object sender, RoutedEventArgs e) { foreach (var checkBox in FindAllCheckBoxes(MyListView).Where(x => !ReferenceEquals(x, sender))) { checkBox.IsChecked = false; } } private static IEnumerable<CheckBox> FindAllCheckBoxes(Visual element) { if (element == null) { return null; } var checkBoxes = new List<CheckBox>(); for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { var visual = VisualTreeHelper.GetChild(element, i) as Visual; var checkBox = visual as CheckBox; if (checkBox != null) { checkBoxes.Add(checkBox); } else { checkBoxes.AddRange(FindAllCheckBoxes(visual)); } } return checkBoxes; } 的笨拙方式。

/**
 * Product tag trail.
 */
private function add_crumbs_product_tag() {
    $current_term = $GLOBALS['wp_query']->get_queried_object();

    $this->prepend_shop_page();


    //$this->add_crumb( sprintf( __( 'Products tagged &ldquo;%s&rdquo;', 'woocommerce' ), $current_term->name ) );

    $this->add_crumb( sprintf('%s', $current_term->name ) );
}