数据绑定部分工作到UserControl中的自定义依赖项属性

时间:2015-08-16 17:27:29

标签: c# data-binding user-controls dependency-properties win-universal-app

你好,谢谢你的时间。

我有一个特殊的问题。我创建了一个usercontol,它有一些自定义依赖项属性。

如果我实现usercontrol,并绑定到静态文本。一切都很好。

但是,如果我尝试将其设置为所选对象属性的值。它不起作用。

这是我在输出窗口中收到的错误:

  

错误:BindingExpression路径错误:' SelectedUseCase'财产没有   发现于' Helper.UserControls.UseCasePropertyDisplay'。   BindingExpression:Path =' SelectedUseCase.Name'   的DataItem =' Helper.UserControls.UseCasePropertyDisplay&#39 ;;目标要素   是' Helper.UserControls.UseCasePropertyDisplay' (名称='空&#39);目标   财产是' Text' (键入' String')

用户控件:
https://github.com/Toudahl/SoftwareDesignHelper/blob/master/Helper/UserControls/DisplayAndEditControl.xaml

  var start_seconds = ("00" + (start_date.getSeconds().toString())).slice(-2);

代码隐藏中依赖属性的清除:
https://github.com/Toudahl/SoftwareDesignHelper/blob/master/Helper/UserControls/DisplayAndEditControl.xaml.cs

<UserControl
    x:Class="Helper.UserControls.UseCasePropertyDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Helper.UserControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <StackPanel Orientation="Horizontal"
                Margin="260,0,0,0">
        <TextBlock Text="{Binding Label}"
                   Style="{StaticResource UseCaseTextBlock}"
                   x:Name="textblock_label"/>
        <TextBlock x:Name="textblock_propertyContent"
                   Text="{Binding Text}"
                   Style="{StaticResource UseCaseFrameWorkTextElement}"
                   DoubleTapped="textblock_DoubleTapped" />
        <TextBox x:Name="textbox_propertyContent"
                 Text="{Binding Text}"
                 Visibility="Collapsed"
                 Style="{StaticResource UseCaseTextBox}"
                 LostFocus="textbox_LostFocus" />
    </StackPanel>
</UserControl>

这是我在视图中实现它的方式:
https://github.com/Toudahl/SoftwareDesignHelper/blob/master/Helper/ViewsAndViewModels/ViewUseCases.xaml

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text",
            typeof(string),
            typeof(UseCasePropertyDisplay),
            new PropertyMetadata(null));

    public static readonly  DependencyProperty LabelProperty = DependencyProperty.Register(
        "Label",
        typeof(string),
        typeof(UseCasePropertyDisplay),
        new PropertyMetadata(null));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set {SetValue(TextProperty, value);}
    }

    public string Label
    {
        get { return (string)GetValue(LabelProperty); }
        set { SetValue(LabelProperty, value);}
    }

通过阅读非常相似的问题,我猜测它与我设置上下文的方式有关。但是,提供给人们的解决方案(设置祖先的相对来源)对我来说不起作用。由于它不在我的平台上。 我不确定从哪里开始,因为这是我第一次尝试使用usercontrols,并且第一次使用依赖项属性。 学校并没有开始,直到几个星期,所以我不能抓住我的老师。

1 个答案:

答案 0 :(得分:0)

不是将DataContext设置为global,而是将第一个ui元素设置为继承的DataContext: ScrollView

正如您所写,您不能使用FindAncestor,因此您可以尝试使用名称和引用它:

DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
相关问题