【问题标题】:Associate checkbox with grid将复选框与网格关联
【发布时间】:2013-06-19 20:32:41
【问题描述】:

我有一个带有布尔列的网格。我正在尝试与另一个复选框控件相关联,因此当我选中此复选框时,我的窗口会显示网格中的元素,其中布尔列为 true(选中)。

这是我的 IsChecked 属性

      private bool _isChecked;

      public bool IsChecked
    {
        set
        {
            _isChecked = value;

            InitializeMappedElements();

        }


        get { return _isChecked; }

    }

这是一种仅从网格中选择映射元素的方法 公共 ObservableCollection 映射列表 { 得到 { 返回 _mappedList; } 设置 { _mappedList = 值; } }

    private ObservableCollection<MessageFieldViewModel> _mappedList = new ObservableCollection<MessageFieldViewModel>();

     private void InitializeMappedElements()
    {

        if (_isChecked)
        {
            var transactionRuleList =
                MessageFieldVModel.GetAllMessageField().Where(ame => ame.IsMapped == _isChecked);

            _mappedList = new ObservableCollection<MessageFieldViewModel>(transactionRuleList);
            messageFields = _mappedList;
            NotifyPropertyChanged("MessageFields");

        }
    }

在选择时与网格关联

    public ObservableCollection<MessageFieldViewModel> MessageFields
       {
        set
        {
            if (_isChecked)
            {
                InitializeMappedElements();
            }

            NotifyPropertyChanged("MessageField");
        }
        get { return messageFields; }
    }

XAML

    <CheckBox  Foreground="WhiteSmoke" Content="Display only Mapped Fields" HorizontalAlignment="Center" 
                   Margin="198,35,473,74" FontFamily="Trebuchet MS" FontWeight="Bold" Grid.Column="1" 
                   IsChecked="{Binding Path= IsChecked,Mode=TwoWay}">
    </CheckBox>

当我选择什么都没有显示。为什么这不起作用?谢谢。

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    你的逻辑很难遵循,而且更难维护。不要直接设置支持字段,而是使用MessageFields 属性并在设置器中正常设置支持字段。

    话虽如此,您应该考虑改用CollectionViewSource,因为这听起来只是视图的问题。

    【讨论】:

    • 我发现问题是我将网格与另一个控件相关联,以便网格仅在我从该控件中选择项目时显示。你说得对,我需要想办法让我的代码易于管理
    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 2021-05-21
    • 2014-08-16
    • 1970-01-01
    • 2011-09-03
    • 2013-08-21
    • 1970-01-01
    • 2017-03-24
    相关资源
    最近更新 更多