Is there something wrong with my dependency property? Content not set with binding
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&q开发者_如何转开发uot;, 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.
have you tried like this ?
<me:MarkdownEditor TextContent="{Binding Path=TextContent}" Options="{Binding Options}" />
(adding the "Path=")
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...
just try with elementname binding
eg. Give the Window x:name=MyWindow and use the elementbinding
<TextBlock Text="{Binding Path=Problem,ElementName=MyWindow}" />
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.
精彩评论