【问题标题】:WPF, property in ValidationRule never setWPF,ValidationRule 中的属性从未设置
【发布时间】:2010-08-26 17:45:52
【问题描述】:

我正在尝试将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,以便我可以绑定到它。但是,在我的ValidationRule 上的Validate 方法中,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 属性(我绑定到CheckboxViewModelsList 属性)有内容,因为我在TextBox 上方有一个TreeView,它显示了Releases 的内容.我在CheckboxViewModels.List 绑定上的转换器什么都不做,它只是我可以设置断点的地方。有趣的是,转换器断点永远不会被命中。就好像整行 &lt;local:CheckboxViewModels List="{Binding Path=Releases, Converter={StaticResource debugConverter}}"/&gt; 永远不会被执行,所以我的 ValidationRule 中的 ValidReleases 属性永远不会被设置。怎么回事?

编辑:这是CheckboxViewModels 的样子:

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); }
    }
}

【问题讨论】:

    标签: c# wpf validation xaml dependency-properties


    【解决方案1】:

    闻起来像是某处缺少属性更改通知。很难从中看出 CheckboxModels 是关于什么的,但如果它是 ObservableCollection,您将无需任何额外工作即可更改属性。这有意义吗?

    HTH,
    浆果

    【讨论】:

    • 我更新了我的问题以包含CheckboxViewModels 代码。我不确定我应该联系哪些活动。
    • 我没有看到将列表设为 DP 的要求,因为它看起来像绑定源而不是目标。现在,我将添加 ObservableCollection 的新属性,并尝试使用它重新设计解决方案。您可以将其称为诸如 Releases_NEW 之类的聪明的东西,并发布任何给您带来麻烦的绑定。如果确定 DP 是必要的,我看到了尝试让它发挥作用的途径,你仍然会有一个起点。
    【解决方案2】:

    好吧,现在我只是觉得很傻。我在CheckboxViewModels 中有一个构造函数,它设置List = new List&lt;CheckboxViewModel&gt;()。我认为这以某种方式重置了某些东西?我删除了该构造函数,List 的初始值仅在DependencyPropertyRegister 方法中设置:new PropertyMetadata(new List&lt;CheckboxViewModel&gt;())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>
    

    编辑:毕竟不是那么傻:在CheckboxViewModels 中没有构造函数导致CheckboxViewModels 类型的两个依赖属性使用相同的列表,所以我在这两个列表中都有发布和其他数据Releases 属性和另一个属性。将构造函数添加回CheckboxViewModels 会导致ValidReleases 不再有任何项目。我想我一定是绑定不正确。

    【讨论】:

      【解决方案3】:

      我停止使用验证规则,而是遵循this tutorial about implementing IDataErrorInfo。那是当我进入this[string] 以找出错误消息时,我的类实例已完全初始化。我一直试图传入的数据只是来自同一实例上另一个属性的数据。也就是说,我想使用来自Property2 的一些数据来验证Property1。使用IDataErrorInfo,当我验证Property1 并在必要时组装错误消息时,我可以根据需要访问Property2,而无需传递任何内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-02
        • 1970-01-01
        • 1970-01-01
        • 2019-02-08
        相关资源
        最近更新 更多