【问题标题】:How to disable unchecked checkbox in listbox wpf?如何禁用列表框wpf中未选中的复选框?
【发布时间】:2018-04-05 16:15:54
【问题描述】:

我有 30 个复选框。如果我选中任何 6 个复选框,则应禁用剩余的 24 个复选框,如果我取消选中 6 中的任何一个复选框,则应启用所有复选框。

<Style TargetType="{x:Type ListBox}">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="4"

          CornerRadius="5" Margin="6"

          >

                        <CheckBox Uid="checkbox1" Name="checkbox1" Checked="CheckBox_Checked" Unchecked="CheckBox_UnChecked"  IsChecked="{Binding ElementName=button,Path=IsChecked,Mode=OneWay}">
                            <Image

            Source="{Binding Path=UriSource}"

            Stretch="Fill"

            Width="100" Height="120"

           />
                        </CheckBox>
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter  Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>

 <ListBox Name="List1"  ItemsSource="{Binding}"   Margin="-50,-8,93,0" RenderTransformOrigin="0.5,0.5" Height="289" VerticalAlignment="Top" >
            <ListBox.RenderTransform>

                <TransformGroup>
                    <ScaleTransform ScaleX="0.975" ScaleY="0.997"/>
                    <SkewTransform AngleY="-8.98" AngleX="9.705"/>
                    <RotateTransform Angle="9.419"/>
                    <TranslateTransform Y="76.889" X="64.258"/>
                </TransformGroup>


            </ListBox.RenderTransform>
        </ListBox>

c#:

if (iList.Count == 6) { List1.IsEnabled = false; } 

它禁用整个列表框。 ilist 包含选中复选框值的集合。

有什么方法可以做到这一点? 请帮帮我。

【问题讨论】:

  • 分享你尝试过的逻辑。
  • if (iList.Count == 6) { List1.IsEnabled = false; }
  • ilist 是选中复选框值的集合。
  • 它禁用整个列表框
  • 请用这个逻辑更新你的问题。

标签: wpf checkbox listbox


【解决方案1】:

如果您想单独禁用复选框,我建议绑定到它们的 IsEnabled 属性。您可以在每次选中或取消选中列表框时遍历列表以查看是否选中了 6 个项目。如果它们是,您可以再次遍历列表并将现在绑定的 IsEnabled 属性设置为 false 对于尚未检查的每个项目。我猜您正在编写代码,但我建议您使用视图模型进行操作。这看起来像这样:

视图模型

public class TestViewModel
{
    private bool _disabledCheckBoxes;

    public TestViewModel()
    {
        TestList = new ObservableCollection<CheckBoxItem>();
        for (int i = 0; i < 30; i++)
        {
            TestList.Add(new CheckBoxItem(true));
        }
    }

    public ObservableCollection<CheckBoxItem> TestList { get; set; }


    public RelayCommand CheckBoxChanged
    {
        get { return new RelayCommand(OnCheckBoxChanged); }
    }

    private void OnCheckBoxChanged()
    {
        if (_disabledCheckBoxes)
        {
            foreach (var item in TestList)
            {
                item.IsEnabled = true;
            }

            _disabledCheckBoxes = false;
        }
        else
        {
            int i = 0;
            foreach (var item in TestList)
            {
                if (item.IsChecked)
                    i++;
            }

            if (i >= 6)
            {
                foreach (var item in TestList)
                {
                    if (!item.IsChecked)
                        item.IsEnabled = false;
                }
                _disabledCheckBoxes = true;
            }
        }
    }
}

CheckBoxItem

public class CheckBoxItem : ObservableObject
{
    public CheckBoxItem(bool isEnabled)
    {
        IsEnabled = isEnabled;
    }

    private bool _isEnabled;
    public bool IsEnabled
    {
        get
        {
            return _isEnabled;
        }
        set
        {
            _isEnabled = value;
            RaisePropertyChanged("IsEnabled");
        }
    }

    private bool _isChecked;
    public bool IsChecked
    {
        get
        {
            return _isChecked;
        }
        set
        {
            _isChecked = value;
            RaisePropertyChanged("IsChecked");
        }
    }
}

Xaml 代码/视图

<Window x:Class="WpfAppTests.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfAppTests"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    mc:Ignorable="d"
    xmlns:modelNoMvvmLight="clr-namespace:WpfAppTests"
    xmlns:modelMvvmLight="clr-namespace:WpfAppTests.ViewModel"
    Loaded="Loaded_Window"
    Title="MainWindow" Height="350" Width="525" >
<Window.DataContext>
    <modelMvvmLight:TestViewModel/>
</Window.DataContext>

<StackPanel>
    <ListBox ItemsSource="{Binding TestList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding IsChecked}" IsEnabled="{Binding IsEnabled}" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Checked">
                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource  AncestorType=Window}, Path=DataContext.CheckBoxChanged}"/>
                        </i:EventTrigger>
                        <i:EventTrigger EventName="Unchecked">
                            <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource  AncestorType=Window}, Path=DataContext.CheckBoxChanged}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </CheckBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>
</Window>

我使用 CheckBoxChanged 命令(在视图模型中)绑定了 Checked 和 UnChecked 事件,因此每次复选框的状态发生变化时,都可以验证您的要求(选中项 == 6)。 我在示例中使用了 MvvmLight,但如果您考虑使用 mvvm 模式并且不想使用框架,您可以实现 ObservableObject 和 RelayCommand yourself

【讨论】:

  • 错误 18 找不到类型“i:InvokeCommandAction”。确认您没有丢失程序集引用并且所有引用的程序集都已构建。 C:\Users\documents\visual studio 2012\Projects\simple\simple\MainWindow.xaml 29 30 简单获取错误
  • 右键你项目的引用->添加引用->搜索System.Windows.Interactivity并添加。
  • 我没有那个参考文献
  • @suma 你在用 Visual Studio 吗?
  • @suma 嗯我想你可以做到with NuGet
猜你喜欢
  • 2011-05-21
  • 2014-08-01
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
相关资源
最近更新 更多