当前位置: 首页 > 知识库问答 >
问题:

UWP:在按钮单击事件时取消选中ListView中的复选框

隆功
2023-03-14

我试图在按钮单击事件上清除Popup=>ListView中的选中复选框。

我尝试过循环listview并将property设置为false,但这不起作用。

在此处输入代码x:class=“uc.mainpage”xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation”xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”xmlns:local=“using:uc”xmlns:d=“http://schemas.microsoft.com/expression/blend/2008”xmlns:mc=“http://schemas.openxmlformats.org/markup-compatibility/2006”mc:Ignorable=“d”xmlns:mvsc=“using:uc.models

Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <StackPanel>
        <Button x:Name="btn_Add" Content="Add"  Margin="420,10,10,0" Click="Btn_Add_Click"/>
    </StackPanel>
    <StackPanel Margin="10,50,10,0">
        <TextBlock Text="Selected List" Margin="10" HorizontalAlignment="Left"  VerticalAlignment="Top" />
        <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}"
                Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
                BorderThickness="1"  Margin="10,0,800,5" Height="300">
            <ListView  x:Name="lstViewSelectedData" ScrollViewer.VerticalScrollBarVisibility="Auto"
               ScrollViewer.VerticalScrollMode="Enabled">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="mvsc:SelectedDataModel">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock VerticalAlignment="Center" x:Name="txtSelectedData" Text="{x:Bind Message}"></TextBlock>
                            <AppBarButton VerticalAlignment="Bottom" Click="AppBarButton_Click" Icon="Delete" 
                                  Tag="{x:Bind Id, Mode=OneWay}" >
                            </AppBarButton>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </Border>

    </StackPanel>
    <Popup x:Name="popup1" VerticalOffset="10" HorizontalOffset="450" IsLightDismissEnabled="True" 
           HorizontalAlignment="Stretch" Margin="50" VerticalAlignment="Stretch">

        <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" 
                Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
                BorderThickness="1" Width="450" Height="400" >
            <StackPanel x:Name="stkpnl1" Orientation="Vertical">
                <TextBox x:Name="txtSearch" Header="Filter" Margin="10"  
                     PlaceholderText="Search list" TextChanging="TxtSearch_TextChanging" />


                <ListView x:Name="lstView" Margin="30" Height="200" 
                      ScrollViewer.VerticalScrollBarVisibility="Auto"
                      ScrollViewer.VerticalScrollMode="Enabled">
                    <ListView.ItemTemplate >
                        <DataTemplate x:DataType="mvsc:SelectedDataModel" >
                            <CheckBox x:Name="chkBox" 

                          FlowDirection="LeftToRight"  
                          Tag="{x:Bind Id, Mode=TwoWay}" 
                          Content="{x:Bind Message, Mode=TwoWay}" IsChecked="{x:Bind IsFavorite,Mode=TwoWay}"
                          Checked="ChkBox_Checked" Unchecked="ChkBox_Unchecked" IsHitTestVisible="True"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
                <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="1" FlowDirection="LeftToRight" >
                    <Button x:Name="btnAdd" Content="Select" Click="BtnAdd_Click" Margin="10" />
                    <Button x:Name="btnCancel" Content="Close"  Click="BtnCancel_Click"/>


                </StackPanel>
            </StackPanel>

        </Border>
    </Popup>
</Grid>

null

 public sealed partial class MainPage : Page
    {
        public SelectedDataModel SelectedData
        {
            get { return (SelectedDataModel)GetValue(SelectedDataProperty); }
            set { SetValue(SelectedDataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedDataProperty =
                        DependencyProperty.Register("SelectedData", typeof(int), typeof(SelectedDataModel), null);

        public AllDataModel AllDataList
        {
            get { return (AllDataModel)GetValue(AllDataListProperty); }
            set { SetValue(AllDataListProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AllDataListProperty =
                        DependencyProperty.Register("AllDataList", typeof(int), typeof(AllDataModel), null);

        List<SelectedDataModel> listData = new List<SelectedDataModel>();
        List<SelectedDataModel> temp = new List<SelectedDataModel>();
        List<SelectedDataModel> temp1 = new List<SelectedDataModel>();
        List<SelectedDataModel> selectedData = new List<SelectedDataModel>();

        public MainPage()
        {
            this.InitializeComponent();
            lstView.ItemsSource = GetData();
            //lstViewSelectedData.ItemsSource = GetSelectedData();
            temp = listData;
        }
        public List<SelectedDataModel> GetData()
        {
            listData.Add(new SelectedDataModel { Id = 1, Message = "One" });
            listData.Add(new SelectedDataModel { Id = 2, Message = "Two" });
            listData.Add(new SelectedDataModel { Id = 3, Message = "Three" });
            listData.Add(new SelectedDataModel { Id = 4, Message = "Four" });
            listData.Add(new SelectedDataModel { Id = 5, Message = "Five" });
            listData.Add(new SelectedDataModel { Id = 6, Message = "Six" });
            listData.Add(new SelectedDataModel { Id = 7, Message = "Seven" });
            listData.Add(new SelectedDataModel { Id = 8, Message = "Eight" });

            return listData;
        }

        private async void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (selectedData.Count == 0)
            {
                ContentDialog dialog = new ContentDialog();
                dialog.Title = "Not Selected";
                dialog.Content = "Please Select Atlease One Entry";
                dialog.CloseButtonText = "Close";
                await dialog.ShowAsync();
            }
            else
            {
                lstViewSelectedData.ItemsSource = null;
                var dt = selectedData;
                lstViewSelectedData.ItemsSource = dt;
                //this.selectedData.ForEach(x => x.IsFavorite = false);
                //this.listData.ForEach(x => x.IsFavorite = false);
                //temp1 = dt;
                popup1.IsOpen = false;



                //foreach (SelectedDataModel row in lstView.Items.ToList())
                //{
                //    //CheckBox box = row.FindName("chkSelect") as CheckBox;
                //    if (row.IsFavorite == true)
                //    {
                //        row.IsFavorite = false;
                //      //  listData.Remove(listData.First(X => X.Id == row.Id));
                //        //Logic to delete the row 
                //    }

                //}
                for (int i = 0; i < lstView.Items.Count; i++)
                {
                    ListViewItem lvi = (ListViewItem)lstView.ItemContainerGenerator.ContainerFromItem(lstView.Items[i]);

                    if (lvi != null)
                    {
                        //CheckBox c = lvi.FindChildByType<CheckBox>();
                        //c.IsChecked = true;
                    }
                }
            }

        }
        private void TxtSearch_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
        {
            listData = temp;

            if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                listData = listData.Where(x => x.Message.ToUpper().Contains(txtSearch.Text.ToUpper())).ToList();
                if (listData.Count > 0)
                {
                    lstView.ItemsSource = listData;
                }
                else
                {
                    lstView.ItemsSource = temp;
                }
            }
            else
            {
                lstView.ItemsSource = temp;
            }
        }
        private void ChkBox_Checked(object sender, RoutedEventArgs e)
        {
            var chkBox = sender as CheckBox;
            //throwing an exception System.NullReferenceException: 'Object reference not set to an instance of an object.'
            selectedData.Add(listData.First(x => x.Id == int.Parse(chkBox.Tag.ToString())));
           // listData.Remove(listData.First(X => X.Id == int.Parse(chkBox.Tag.ToString())));

            //selectedData.Clear();


        }


        private void ChkBox_Unchecked(object sender, RoutedEventArgs e)
        {
            var chkBox = sender as CheckBox;
            selectedData.Remove(listData.First(X => X.Id == int.Parse(chkBox.Tag.ToString())));

        }
        private void BtnAddData_Click(object sender, RoutedEventArgs e)
        {
            //popup2.IsOpen = false;
            popup1.IsOpen = true;
        }
        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            popup1.IsOpen = false;
        }
        private void Btn_Add_Click(object sender, RoutedEventArgs e)
        {


            popup1.IsOpen = true;
        }
        private async void AppBarButton_Click(object sender, RoutedEventArgs e)
        {
            AppBarButton delButton = sender as AppBarButton;
            ContentDialog dialog = new ContentDialog();
            dialog.Content = "Do You Want To Remove..?";
            dialog.PrimaryButtonText = "Yes";
            dialog.SecondaryButtonText = "No";
            dialog.Title = "Remove";
            ContentDialogResult result = await dialog.ShowAsync();
            if (result == ContentDialogResult.Primary)
            {
                //var isRemoved = temp.Remove(listData.First(x => x.id == int.Parse(delButton.Tag.ToString())));

                //List<Info> temp1 = new List<Info>();
                //foreach (var item in temp)
                //{
                //    temp1.Add(item);
                //}
                //lstViewSelectedData.ItemsSource = temp1;
                var temp2 = selectedData;
                temp2.Remove(temp1.First(x => x.Id == int.Parse(delButton.Tag.ToString())));
                lstViewSelectedData.ItemsSource = null;
                lstViewSelectedData.ItemsSource = temp2;
            }
        }

    }

我只想在按钮点击事件UWP上清除listview中的选中项。

共有1个答案

宦子琪
2023-03-14

我尝试过循环listview并将property设置为false,但这不起作用。

您的复选框绑定到IsFavorite属性。因此,如果要取消选中,只需更新IsFavorite属性的值。若要在属性值更改时更新UI,SelectedDataModel类需要实现INotifyPropertyChanged接口。

public class SelectedDataModel : INotifyPropertyChanged
{
    private int _Id;

    public int Id
    {
        get { return _Id; }
        set
        {
            if (_Id != value)
            {
                _Id = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Id"));
            }
        }
    }

    private string _Message;

    public string Message
    {
        get { return _Message; }
        set
        {
            if (_Message != value)
            {
                _Message = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Message"));
            }
        }
    }

    private bool _IsFavorite;

    public bool IsFavorite
    {
        get { return _IsFavorite; }
        set
        {
            if (_IsFavorite != value)
            {
                _IsFavorite = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsFavorite"));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

然后,在button click事件处理程序中,只需将ListData中每个项的IsFavorite更改为false。

foreach (var item in listData)
{
    item.IsFavorite = false;
}
 类似资料:
  • 我的uwp应用程序收到来自php服务器的toast通知。它有两个操作按钮“View”和“Dissist”。当应用程序当前处于激活状态时,这些按钮可以正常工作。(单击“查看”按钮、“重定向到新页面”和“取消”按钮将不会执行任何操作。)。但当应用处于关闭状态时--当用户点击通知的Dissist按钮时,应用的启动图标就会出现。我怎么才能阻止这一切?我想在用户单击Dissist按钮时取消通知。

  • 问题内容: 在使用jQuery提交AJAX表单后,我想取消选中单选按钮。我有以下功能: 借助此功能,我可以清除文本框中的值,但不能清除单选按钮的值。 顺便说一句,我也尝试过,但是那没用。 问题答案: 要么(普通js) 或(jQuery)

  • 问题内容: 当显示一组JRadioButton时,最初不会选择任何一个(除非您以编程方式强制执行此操作)。我希望即使在用户已经选择一个按钮之后,也可以将按钮放回该状态,即,不应选择任何按钮。 但是,使用通常的嫌疑人并不能达到预期的效果:在每个按钮上调用’setSelected(false)’不起作用。有趣的是,当按钮没有放入ButtonGroup时,它 确实可以 工作-不幸的是,对于JRadioB

  • 下面的代码是我的按钮操作的一部分。Jtable contain last row is复选框。单击“保存”按钮时,所选行必须从表行中删除 执行的操作代码

  • 问题内容: 当我单击他的图像时,我想选择单选按钮,但是它不起作用。这是我尝试过的: 我的两个属性都具有相同的 data =“” 属性:对于图像和输入,单击图像时,是否有任何方法可以检查输入(该收音机)? 谢谢 更新: 我发现一些代码有效,但是仅在图像上单击了三下,因此当单击最后一个脚本时,脚本停止了,无法再次选择第一个或第二个,我不知道为什么…我认为必须取消选中所有单选按钮,然后选中选中的一个按钮

  • 问题内容: 我正在尝试从3个按钮的列表中进行选择,但是找不到选择它们的方法。以下是我正在使用的HTML。 我可以使用以下代码找到它: 输出:SRF,COM,MOT 但我想选择ChoiceOne。(单击它)我该怎么做? 问题答案: 使用CSS选择器或XPath 直接按属性选择,然后单击它。 更正(但是OP应该学习如何在文档中查找) 在Python绑定中,它不存在,称为。一个人应该能够查看异常消息并在