Is there something wrong with my dependency property? Content not set with binding

霸气de小男生 提交于 2019-12-08 11:09:13

问题


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

回答1:


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.




回答2:


have you tried like this ?

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

(adding the "Path=")




回答3:


I'm gonna get beaten up for this but :

Have-you tried this:

<TextBox Text="{Binding Path=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}" />

(adding the "Path=" in your template this time) I know I'm sounding like a broken record, but this "Path=" gave me some headeache not so long ago, and I'm really wondering if your problem does not have something to do with this...




回答4:


just try with elementname binding

eg. Give the Window x:name=MyWindow and use the elementbinding

<TextBlock Text="{Binding Path=Problem,ElementName=MyWindow}" />



回答5:


I don't know what the MarkdownEditor is or does, but I suppose that it's possible that the MarkdownEditor doesn't support two-way data binding by default. Specify that explicitly in your markup:

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

Some XAML controls (such as TextBox) use two-way binding by default -- in other words, changes made in the data context are reflected in the control and changes made in the control are reflected in the data context. Other XAML controls use one-way binding by default. I am not sure what MarkdownEditor does by default, but, if it uses one-way binding by default, I could see how changes made in the control are not reflected in your data context. I would be interested to hear if explicitly setting the binding mode to TwoWay helps your situation.



来源:https://stackoverflow.com/questions/4225331/is-there-something-wrong-with-my-dependency-property-content-not-set-with-bindi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!