1

我有一个 Listview,其中一列包含 TextBox,另一列包含 ComboBox。

此验证适用于 TextBox:

<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 尝试相同的方法时,我的验证器永远不会被调用:

<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 的绑定路径,但此时我只是在挣扎。

谢谢!

4

1 回答 1

1

问题是您已经绑定SelectedItem并且SelectedValue两者都用于组合框。此外,它们绑定到相同的属性。

摆脱 SelectedItem 绑定,它将正常工作。(SelectedItem 优先于 SelectedValue)

<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>
于 2014-07-09T18:22:25.853 回答