是不是有什么毛病我依赖属性?内容不设置与绑定 [英] Is there something wrong with my dependency property? Content not set with binding

查看:219
本文介绍了是不是有什么毛病我依赖属性?内容不设置与绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有件事错了我的依赖属性?

  //在MarkdownEditor.xaml.cs,为的DataContext MarkdownEditor.xaml
公共字符串的TextContent
{
    {返回(串)的GetValue(TextContentProperty); }
    集合{的SetValue(TextContentProperty,值); }
}公共静态只读的DependencyProperty TextContentProperty =
    DependencyProperty.Register(的TextContent的typeof(串)的typeof(MarkdownEditor),新UIPropertyMetadata());

当设置的TextContent 在XAML像

 <我:MarkdownEditor的TextContent ={结合的TextContent}选项={绑定选项}/>

据失败......当我做

 <我:MarkdownEditor的TextContent =Hello World的选项={绑定选项}/>

它的工作原理...是不是有什么错吗?类似的事情似乎是发生在选项


更新1

我注意到,与正常的文本框的结合正常工作

 <文本框的文本={结合的TextContent}/>

FYI:在 MarkdownEditor.xaml

 <文本框的文本={结合的TextContent}
        的FontFamily ={绑定路径= Options.FontFamily}
        字号={绑定路径= Options.FontSize}
        粗细={绑定路径= Options.FontWeight}
        背景={绑定路径= Options.Background}
        前景={绑定路径= Options.Foreground}/>

更新2

哦!我不知道,当我做

 <我:MarkdownEditor的TextContent ={结合的TextContent}选项={绑定选项}/>

哪里属性的TextContent &安培; 选项从何而来?的 MarkdownEditor 视图模型?

更新3

另一些意见:

准系统

 <我:MarkdownEditor />

的TextContent 将由 MarkdownEditor 设置为值的构造

 公共MarkdownEditor()
{
    的InitializeComponent();
    的DataContext =这一点;
    的TextContent =从MarkdownEditor.xaml.cs
}

静态值

 <我:MarkdownEditor的TextContent =静态值/>

字符串静态值显示

绑定

 <我:MarkdownEditor的TextContent ={绑定路径=的TextContent}/>

显示从依附财产申报值

 公共静态只读的DependencyProperty TextContentProperty =
        DependencyProperty.Register(...,新UIPropertyMetadata(默认));


解决方案

你是如何设置的MarkdownEditor.xaml在DataContext的绑定?您设置的DataContext的可以通过在你的控制,使用的是MarkDownEditor定义的DataContext的被覆盖。因此,您应该用FindAncestor MarkdownEditor.xaml,寻找用户控件(或者你有根)绑定。

编辑:
这是一个有点混乱,你所拥有的。我认为以下几点:

您定义了一个名为MarkdownEditor用户控件,与MarkdownEditor.xaml和code-背后MarkdownEditor.xaml.cs。你可以通过 this.DataContext =设置控件的DataContext的这一点; 在MarkdownEditor.xaml.cs构造函数或的DataContext ={绑定的RelativeSource = {自我的RelativeSource}}在MarkdownEditor.xaml的根元素。

其次,你有第二个用户控件/窗/不管。让我们把它叫做MyControl。它也有一个DataContext你设置的不知何故的。然后你绑定的TextContent如图所示。

所以,在MarkdownEditor, {结合的TextContent} 指的是DP在MarkdownEditor.xaml.cs。在MyControl, {结合的TextContent} 是指在一个属性你的 MyControl的DataContext的的。所以,你应该检查,如果你确实有MyControl的DataContext的这种特性。其次,你应该检查一下MarkdownEditor DataContext的是你所期望的,或者如果它得到覆盖。

I wonder if theres something wrong with my dependency property?

// In MarkdownEditor.xaml.cs, DataContext for MarkdownEditor.xaml
public string TextContent
{
    get { return (string)GetValue(TextContentProperty); }
    set { SetValue(TextContentProperty, value); }
}

public static readonly DependencyProperty TextContentProperty =
    DependencyProperty.Register("TextContent", typeof(string), typeof(MarkdownEditor), new UIPropertyMetadata(""));

When TextContent is set in XAML like

<me:MarkdownEditor TextContent="{Binding TextContent}" Options="{Binding Options}" />

It fails ... when I do

<me:MarkdownEditor TextContent="Hello world" Options="{Binding Options}" />

It works ... Is there something wrong? A similar thing seems to be happening to options


UPDATE 1

I notice that the binding with a normal text box works fine

<TextBox Text="{Binding TextContent}" />

FYI: In MarkdownEditor.xaml

<TextBox Text="{Binding TextContent}" 
        FontFamily="{Binding Path=Options.FontFamily}"
        FontSize="{Binding Path=Options.FontSize}"
        FontWeight="{Binding Path=Options.FontWeight}"
        Background="{Binding Path=Options.Background}"
        Foreground="{Binding Path=Options.Foreground}" />

UPDATE 2

Oh! I wonder if when I do

<me:MarkdownEditor TextContent="{Binding TextContent}" Options="{Binding Options}" />

Where does the properties TextContent & Options come from? MarkdownEditor's ViewModel?

UPDATE 3

Another few observations:

Barebones

<me:MarkdownEditor />

TextContent will be set to the value from MarkdownEditor's constructor

public MarkdownEditor()
{
    InitializeComponent();
    DataContext = this;
    TextContent = "From MarkdownEditor.xaml.cs";
}

Static Value

<me:MarkdownEditor TextContent="Static Value" />

The string "Static Value" is shown

Binding

<me:MarkdownEditor TextContent="{Binding Path=TextContent}" />

value from Dependency Property declaration is shown

public static readonly DependencyProperty TextContentProperty =
        DependencyProperty.Register(..., new UIPropertyMetadata("Default"));

解决方案

How are you setting the DataContext in MarkdownEditor.xaml for the bindings? The DataContext you set may get overwritten by the DataContext defined in your control which is using the MarkDownEditor. Thus, you should bind in MarkdownEditor.xaml using FindAncestor, looking for the UserControl (or whatever you have as root).

EDIT: It is a bit confusing what you have. I assume the following:

You defined a UserControl called MarkdownEditor, with MarkdownEditor.xaml and code-behind MarkdownEditor.xaml.cs. You set the DataContext of the control via this.DataContext = this; in the constructor in MarkdownEditor.xaml.cs or DataContext="{Binding RelativeSource={RelativeSource Self}}" on the root element in MarkdownEditor.xaml.

Secondly, you have a second UserControl/Window/whatever. Lets call it MyControl. It too has a DataContext which you set somehow. Then you bind TextContent as shown.

So, in MarkdownEditor, {Binding TextContent} refers to the DP in MarkdownEditor.xaml.cs. In MyControl, {Binding TextContent} refers to a property on your DataContext of MyControl. So you should check if you actually have such a property in MyControl's DataContext. Secondly, you should check if the DataContext in MarkdownEditor is what you expect, or if it got overwritten.

这篇关于是不是有什么毛病我依赖属性?内容不设置与绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆