【问题标题】:WPF C# DataBinding Error VisisbilityWPF C# 数据绑定错误可见性
【发布时间】:2015-12-02 22:13:00
【问题描述】:

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

我的容器的内容由属性“Editor”绑定,该属性根据所选对象的类型返回正确的用户控件。

这是我的容器代码:

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

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

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

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“GommageEditor”(名称=“”)上找不到“SelectedEntity”属性。绑定表达式:路径=选定实体; DataItem='GommageEditor' (Name='');目标元素是'网格'(名称='');目标属性是“NoTarget”(类型“对象”)

我不知道这个异常,但是当我在 usercontrol 编辑器的构造函数中注释 datacontext 声明时,它是可见的...

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

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

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

我在 MVVM 模式中使用 Galasoft MVVM ligth,在我的控件中使用 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>

【问题讨论】:

  • Editor 是依赖属性吗?例外是抱怨 SelectedEditor/Editor 的 xmal 中的绑定错误。您是否使用任何触发器?因为问题属性是NoTarget,应该和wpf中的触发器有关
  • 我将 Usercontrol 的内容添加到帖子中。编辑器只是一个属性。 依赖属性属性有区别吗?
  • UserControls 永远不应该有为他们设计的 ViewModel。那是因为就像这样的问题。删除此 ViewModel。将所有 UI 代码放在 UserControl 的代码隐藏中。想一想——为什么TextBox没有TextBoxViewModel?为什么没有 ComboBoxViewModel?现在为什么你的 UserControl 有一个?? MVVM != 没有代码隐藏。

标签: c# wpf xaml mvvm binding


【解决方案1】:

我发现了我的错误!

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

如果我删除此样式,效果很好! :)

感谢 cscmh99 !

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 2011-06-14
    • 2016-07-16
    • 2012-11-23
    • 2020-04-21
    • 2011-08-31
    • 2016-01-31
    • 2014-03-31
    相关资源
    最近更新 更多