首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ComboBox中使用带有ValidationRules的ListView

在ComboBox中使用带有ValidationRules的ListView
EN

Stack Overflow用户
提问于 2014-07-09 14:42:28
回答 1查看 631关注 0票数 1

我有一个清单视图,其中一个列包含一个TextBox,另一个列包含一个ComboBox。

此验证对于TextBox非常有效:

代码语言:javascript
复制
<GridViewColumn Header="SQL Server">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBox Width="140">
                <TextBox.Text>
                    <Binding Path="Server" Mode="TwoWay" NotifyOnValidationError="True" 
                             ValidatesOnExceptions="True" ValidatesOnDataErrors="True" 
                             UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <helpers:DatabaseServerNameValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

但是,当我在ComboBox中尝试相同的方法时,我的验证器从未被调用过:

代码语言:javascript
复制
<GridViewColumn Header="Database Type">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Width="140" ItemsSource="{Binding Source={StaticResource DatabaseTypeFromEnum}}" SelectedItem="{Binding DatabaseType, Mode=TwoWay}">
                <ComboBox.SelectedValue>
                    <Binding Path="DatabaseType" 
                             NotifyOnValidationError="True" 
                             ValidatesOnExceptions="True" 
                             ValidatesOnDataErrors="True" 
                             UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <helpers:DatabaseTypeValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </ComboBox.SelectedValue>
            </ComboBox>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

我也尝试过用<ComboBox.SelectedValue>代替<ComboBox.SelectedValuePath>,但没有效果。

我的问题可能在于ComboBox的绑定路径,但我只是在讨论这个问题。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-09 18:22:25

问题是您已经为SelectedItemSelectedValue绑定了comboBox。此外,它们被绑定在同一财产上。

摆脱了SelectedItem绑定,它就可以正常工作了。(SelectedItem比SelectedValue有更高的偏好)

代码语言:javascript
复制
<GridViewColumn Header="Database Type">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Width="140"
                      ItemsSource="{Binding Source={StaticResource 
                                                    DatabaseTypeFromEnum}}">
                <ComboBox.SelectedValue>
                    <Binding Path="DatabaseType" 
                             NotifyOnValidationError="True" 
                             ValidatesOnExceptions="True" 
                             ValidatesOnDataErrors="True" 
                             UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <helpers:DatabaseTypeValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </ComboBox.SelectedValue>
            </ComboBox>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24656769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档