在没有任何绑定集的属性上绑定错误

时间:2015-04-03 23:28:21

标签: c# xaml

Visual Studio在其输出中显示以下错误:

System.Windows.Data Error: 40 :
    BindingExpression path error:
    'Text' property not found on 'object' ''PropertyAppraisalWorkflowViewModel' (HashCode=35281714)'. BindingExpression:Path=Text;
DataItem='PropertyAppraisalWorkflowViewModel' (HashCode=35281714);
target element is 'ComboBox' (Name='cmbWorkflowView');
target property is 'Text' (type 'String')

奇怪的是,我没有为cmbWorkflowView控件绑定(甚至使用)属性“Text”。

以下是错误所说的XAML的截断片段有问题:

    <Grid Name="grdWorkflow">
        <Grid.Resources>
            <Style TargetType="ComboBox" BasedOn="{StaticResource DefaultComboBox}" />
        </Grid.Resources>

        <ComboBox DockPanel.Dock="Right"
                  Name="cmbWorkflowView"
                  ItemsSource="{Binding ViewOptions}"
                  SelectedItem="{Binding SelectedView}"
                  props:ComboBoxProperties.SelectionChangedCommand="{Binding SelectWorkflowView}" />
    </Grid>

正如您所看到的,有问题的ComboBox甚至没有使用Text字段,更不用说绑定它了。是什么导致错误? (见下文。我解决了这个问题,但由于我找不到其他任何有这个问题的解决方案,我决定在这里发布解决方案,以防它可以帮助其他人。)

1 个答案:

答案 0 :(得分:0)

解决方案的关键是风格。在代码中,样式由更多XAML分隔。我忘记了ComboBox有一个与之相关的风格。当我按照风格来源时,我发现了以下内容。 (为了便于阅读,这是截断的,实际的风格更复杂。)

    <Style x:Key="DefaultComboBox" TargetType="ComboBox">
        <Setter Property="Height" Value="{StaticResource DefaultControlHeight}" />
        <Setter Property="Text" Value="{Binding Path=Text}" />
        <Setter Property="Background" Value="{StaticResource ComboBoxEditableFieldBrush}" />
    </Style>

该样式绑定了“Text”属性。一旦我删除了Setter的“Text”属性,错误就消失了。

    <Style x:Key="DefaultComboBox" TargetType="ComboBox">
        <Setter Property="Height" Value="{StaticResource DefaultControlHeight}" />
        <Setter Property="Background" Value="{StaticResource ComboBoxEditableFieldBrush}" />
    </Style>
相关问题