【发布时间】:2018-01-15 19:35:49
【问题描述】:
我有一个动态列表框,如何在单击 selectAll 按钮时选择列表框中的所有复选框。
Xaml:
StackPanel Width="400" Orientation="Horizontal" Grid.Row="0">
<!--<CheckBox IsThreeState="True" x:Name="cbAllFeatures" Width="111" Content="Select all" Height="78"/>-->
<AppBarButton x:Name="Btn_SelectAll" Margin="20,0,0,0" Label="Select All" Icon="SelectAll" Click="Btn_SelectAll_Click" FontWeight="Bold" />
<AppBarButton x:Name="Btn_Delete" Margin="100,0,0,0" Label="Delete All" Icon="Delete" Click="DeleteAll_Click" FontWeight="Bold" />
</StackPanel>
<ListBox x:Name="listBoxobj" ItemsSource="{Binding}" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" SelectionChanged="listBoxobj_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="330" Height="100" >
<Border Margin="5" BorderBrush="White" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<CheckBox Name="Option1CheckBox" Grid.Row="0" Grid.Column="0" IsChecked="{Binding Checked,Mode=TwoWay}" VerticalAlignment="Center" Margin="5,0,0,0" />
<TextBlock x:Name="NameTxt" Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="22" Foreground="White"/>
<TextBlock x:Name="Age" Grid.Row="1" Grid.Column="1" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" FontSize="22" Text="{Binding Age}" />
<Button Grid.Row="0" Grid.Column="2" x:Name="deleteTaskButton" BorderThickness="0" Margin="0" Click="deleteTaskButton_Click" Height="18" IsEnabled="False">
<Image Source="/Assets/delete.png"/>
</Button>
</Grid>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我试着用这种方式做,但它似乎不起作用。
class SelectAll : INotifyPropertyChanged
{
public bool _Checked = false;
public bool Checked
{
get
{
return _Checked;
}
set
{
if (value != _Checked)
{
_Checked = value;
NotifyPropertyChanged("Checked");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
单击 select_all 时的代码。
public void Btn_SelectAll_Click(object sender, RoutedEventArgs e)
{
obj_check._Checked = true;
}
我的想法是使用 for 循环。它必须检查每个项目。但我不知道如何检查,因为我无法选择复选框。感谢任何帮助。
【问题讨论】:
标签: c# xaml windows-phone-8.1