【问题标题】:How can I select the right ListBox Item automatically when I press the Button in the Item I want to Check to delete this Item?当我按下要检查的项目中的按钮以删除此项目时,如何自动选择正确的列表框项目?
【发布时间】:2021-09-17 22:10:43
【问题描述】:

所以我在 WPF 中获得了一个 ListBox,我通过 DataTemplate 在我的 ListBoxItem 中获得了一个按钮。我将像这样的按钮添加到我的项目中:

 <ListBox Grid.Column="1" BorderBrush="Black" Margin="15,20,10,15" MinHeight="25" Name="tbxFiles"
                 VerticalAlignment="Stretch"
             ItemsSource="{Binding Items}"
             SelectionMode="Multiple">
                    <ListBox.Resources>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="OverridesDefaultStyle" Value="true" />
                            <Setter Property="SnapsToDevicePixels" Value="true" />
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListBoxItem">
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="250"/>
                                                <ColumnDefinition Width="50"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>

                                            <TextBlock x:Name="ListText" Text="{Binding}" Grid.Column="0"/>
                                            <RadioButton Grid.Column="1"  Content="TF"  />
                                            <RadioButton Grid.Column="2"  Content="AF" />
                                            <ComboBox Grid.Column="3" Text="Periode"  />
                                            <Button Grid.Column="4"  Click="RemoveMark_Click"  Content="Delete" />
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.Resources>
                </ListBox>

现在要创建的每个 ListBox 项都有一个 deleteButton,当我按下此按钮时,此代码将开始删除此项:

        PeriodeCombo.Items.Clear();
        string required = tbxFiles.SelectedItems.Cast<string>().Aggregate((a, b) => a + b);

         required = tbxFiles.SelectedItems.Cast<string>().Distinct()
                         .Aggregate((a, b) => a + b);

         required = tbxFiles.SelectedItems.Cast<string>().Distinct()
                          .Aggregate((a, b) => a + ";" + b);

        string[] words = required.Split(';');
        foreach (var word in words)
        {
            temp1.Add(word);
        }

        for (int i = 0; i < temp1.Count; i++)
        {
            path.Remove(temp1[i]);
        }

            path.Remove(required);
        tbxFiles.Items.Remove(tbxFiles.SelectedItem);

        while (tbxFiles.SelectedItems.Count > 0)
        {
            tbxFiles.Items.Remove(tbxFiles.SelectedItems[0]);
        }

并且此代码仅删除我单击的这些项目,它们已被选中。但是我现在想要做的是,我可以通过单击项目所在的删除按钮来删除项目而无需选择。因此,当我单击删除按钮时,该项目将被删除,而无需单击该项目。我是如何跪地更改我的代码以使其正常工作的?

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:

    在RemoveMark_Click 中,第一个参数(通常称为sender 和object 类型)应该由您单击的Button 提供,并且它的DataContext 应该是您要删除的集合项。

    所以将 RemoveMark_Click 更改为类似:

    void RemoveMark_Click(object sender, RoutedEventArg args)
    {
       //TODO add null checks a/o exception handling
       var uiElement = (Button)sender;
       var dataItem = uiElement.DataContext;
    
       tbxFiles.Items.Remove(dataItem);
    }
    

    一般来说,对于一个按钮,使用命令来执行所需的操作是更常见和更好的做法,因为这样你就可以将魔法放在 ViewModel 中。 此外,为了让 listboxitem 有一个特殊的布局,设置 datatemplate a/o listbox 的 itemcontainerstyle 通常就足够了,您不需要 覆盖控件模板,但您当然可以它会起作用的。

    【讨论】:

    • 他不知道“collection.Remove”
    • 也许你需要在cs文件的顶部添加一个using:using System.Collections;
    • 我已经使用了这个,错误信息是:“ICollection has no definition for Remove”
    • 傻瓜,把ICollection改成IList(我也编辑我的帖子...)
    • 所以我现在尝试了这个,但是当我点击删除时,程序是否在此时崩溃“collection.Remove(dataItem)”并出现错误“对象引用未设置为对象实例”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多