WPF C# 数据绑定错误可见性

WPF C# DataBinding Error Visisbility

我是个绝望的人... 这是我的问题。我有一个包含用户控件的容器(我的参考编辑器)。

我的容器的内容由 属性 "Editor" 绑定,根据 selected 对象的类型 return 正确的用户控件。

这是我的容器代码:

<Border BorderThickness="1" BorderBrush="LightGray" Grid.Row="1">
    <UserControl Content="{Binding Editor, Mode=OneWay}"/>
</Border>

这是我的 selectedobject 属性 :

public object SelectedEntity
{
    get { return _SelectedEntity; }
    set
    {
        Set("SelectedEntity", ref _SelectedEntity, value);
        Editor = (value != null) ? Editors[value.GetType()] : null;

        if (Editor != null)
        {
             Editor.SetEditable(true);
             Editor.SetValue(SelectedEntity);
        }
    }
}

当我 select 我的对象时,会找到并应用正确的编辑器。 但是在视图中,编辑器的内部控件不可见(标签、文本框和按钮)。

在输出中我发现了这个异常:

System.Windows.Data Error: 40 : BindingExpression path error: 'SelectedEntity' property not found on 'object' ''GommageEditor' (Name='')'. BindingExpression:Path=SelectedEntity; DataItem='GommageEditor' (Name=''); target element is 'Grid' (Name=''); target property is 'NoTarget' (type 'Object')

我不确定这个异常,但是当我在用户控件编辑器的构造函数中注释数据上下文声明时,它是可见的...

/// <summary>
///     Constructeur par défaut
/// </summary>
public UserControlEditor()
{
    InitializeComponent();

    /* When i comment this line innercontrols of usercontrol are visible */
    DataContext = new UserControlEditorViewModel();
}

有想法吗?解决方案 ? 提前致谢...

我将 Galasoft MVVM ligth 用于 MVVM 模式,并将 Mahapps 模板用于我的控件。

编辑: 我在我的用户控件上使用一个触发器,以在我处于非编辑模式时覆盖文本框样式。

<UserControl.Resources>
    <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Editing}" Value="False">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="Foreground" Value="DarkGray"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Label Content="Type de gommage" Style="{StaticResource Title5}" />
    <TextBox Grid.Column="1" Text="{Binding Gommage.Type}"/>

    <Label Content="Acronyme du gommage" Style="{StaticResource Title5}" Grid.Row="1"/>
    <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Gommage.Acronyme}"/>

    <Button Grid.Row="2" Grid.ColumnSpan="2" 
            HorizontalAlignment="Center" 
            Command="{Binding SaveCommand}"
            Content="Enregistrer"
            IsEnabled="{Binding Editing}"/>
</Grid>

我发现了我的错误!

在我的编辑器容器中,我在 SelectedEntity 上使用 datatrigger 设置样式。

如果我删除这个样式,它工作得很好! :)

感谢 cscmh99 !