代码之家  ›  专栏  ›  技术社区  ›  Sarah Vessels

从不设置validationrule中的wpf属性

  •  0
  • Sarah Vessels  · 技术社区  · 15 年前

    我正试图在我的 DataContext 在一个 ValidationRule :

    public class ReleaseValidationRule : ValidationRule
    {
        // I want to bind a value from my DataContext to this property:
        public CheckboxViewModels ValidReleases { get; set; }
        ...
    }
    

    基于 this thread 我创造了 CheckboxViewModels 类作为 List<CheckboxViewModel> 以便列表可以是 DependencyProperty 这样我就可以把它绑起来了。然而,在我 Validate 我的方法 验证规则 , the ValidReleases 列表总是空的。这是我的XAML:

    <TextBox>
        <TextBox.Text>
            <Binding Path="Release" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:ReleaseValidationRule>
                        <local:ReleaseValidationRule.ValidReleases>
                            <local:CheckboxViewModels List="{Binding Path=Releases,
                                Converter={StaticResource debugConverter}}"/>
                        </local:ReleaseValidationRule.ValidReleases>
                    </local:ReleaseValidationRule>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    

    我知道 Releases 财产(我对 List 性质 复选框视图模型 )有内容因为我有 TreeView 就在上面 TextBox 它显示了 发行版 . 我的转换器 CheckboxViewModels.List 绑定不起作用,它只是一个我可以设置断点的地方。有趣的是,转换器断点永远不会被击中。就像整条线 <local:CheckboxViewModels List="{Binding Path=Releases, Converter={StaticResource debugConverter}}"/> 永远不会被处决,所以 验证版本 我的财产 验证规则 永远不会固定。发生什么事?

    编辑: 这里是什么 复选框视图模型 看起来像:

    public class CheckboxViewModels : DependencyObject, IList<CheckboxViewModel>,
        IEnumerable<CheckboxViewModel>
    {
        ...members necessary to implement IList, IEnumerable...
    
        public static readonly DependencyProperty ListProperty =
            DependencyProperty.Register(
                "List",
                typeof(List<CheckboxViewModel>),
                typeof(CheckboxViewModels),
                new PropertyMetadata(new List<CheckboxViewModel>())
            );
    
        public List<CheckboxViewModel> List
        {
            get { return (List<CheckboxViewModel>)GetValue(ListProperty); }
            set { SetValue(ListProperty, value); }
        }
    }
    
    3 回复  |  直到 15 年前
        1
  •  0
  •   Berryl    15 年前

    闻起来像是某个地方丢失的属性更改通知。很难从中看出checkboxmodels是关于什么的,但是如果它是ObservableCollection,您将在不做任何额外工作的情况下更改属性。这有道理吗?

    HTH
    贝里尔

        2
  •  0
  •   Sarah Vessels    15 年前

    好吧,现在我觉得很傻。我有个建筑工人 CheckboxViewModels 那套 List = new List<CheckboxViewModel>() . 我觉得这是在重置什么?我删除了构造函数和 List 仅设置在 Register 方法 DependencyProperty : new PropertyMetadata(new List<CheckboxViewModel>()) . ValidReleases 现在按预期使用以下XAML填充:

    <TextBox>
        <TextBox.Text>
            <Binding Path="Release" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:ReleaseValidationRule ValidatesOnTargetUpdated="True">
                        <local:ReleaseValidationRule.ValidReleases>
                            <local:CheckboxViewModels
                                List="{Binding Path=Releases, Mode=OneWay}"/>
                        </local:ReleaseValidationRule.ValidReleases>
                    </local:ReleaseValidationRule>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    

    编辑: 毕竟没那么傻:没有一个构造函数 复选框视图模型 导致类型的两个依赖属性 复选框视图模型 使用相同的列表,因此我在 Releases 财产和其他财产。将构造函数添加回 复选框视图模型 结果在 验证版本 不再有任何物品。我想我一定是把什么东西捆错了。

        3
  •  0
  •   Sarah Vessels    15 年前

    我停止使用验证规则,而是按照 this tutorial about implementing IDataErrorInfo . 那是我的类实例在我进入 this[string] 找出错误信息。我试图传递的数据只是来自同一实例上另一个属性的数据。也就是说,我想验证一下 Property1 使用来自 Property2 . 用 IDATA错误信息 ,当我验证时 属性1 如果有必要,我可以汇编一条错误消息 特性2 根据需要,无需传递任何信息。