我想知道我的依赖属性是否有问题?
// 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(""));
在XAML中设置TextContent
时
<me:MarkdownEditor TextContent="{Binding TextContent}" Options="{Binding Options}" />
失败......当我做
时<me:MarkdownEditor TextContent="Hello world" Options="{Binding Options}" />
它有效......有什么不对吗?类似的事情似乎发生在选项上
我注意到与普通文本框的绑定工作正常
<TextBox Text="{Binding TextContent}" />
仅供参考: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}" />
哦!我想知道我什么时候做的
<me:MarkdownEditor TextContent="{Binding TextContent}" Options="{Binding Options}" />
属性TextContent
&amp; Options
来自哪里? MarkdownEditor
的ViewModel?
另外几点意见:
准系统
<me:MarkdownEditor />
TextContent
将设置为MarkdownEditor
的构造函数
public MarkdownEditor()
{
InitializeComponent();
DataContext = this;
TextContent = "From MarkdownEditor.xaml.cs";
}
静态值
<me:MarkdownEditor TextContent="Static Value" />
显示字符串“静态值”
装订
<me:MarkdownEditor TextContent="{Binding Path=TextContent}" />
显示来自依赖属性声明的值
public static readonly DependencyProperty TextContentProperty =
DependencyProperty.Register(..., new UIPropertyMetadata("Default"));
答案 0 :(得分:3)
如何在MarkdownEditor.xaml中为绑定设置DataContext?您设置的DataContext可能会被控件中定义的DataContext覆盖,该控件使用MarkDownEditor。因此,您应该使用FindAncestor绑定MarkdownEditor.xaml,查找UserControl(或以root身份获得的任何内容)。
编辑: 你拥有的东西有点令人困惑。我假设如下:
您定义了一个名为MarkdownEditor的UserControl,其中包含MarkdownEditor.xaml和代码隐藏MarkdownEditor.xaml.cs。您可以通过MarkdownEditor.xaml.cs中的构造函数中的this.DataContext = this;
或MarkdownEditor.xaml中根元素上的DataContext="{Binding RelativeSource={RelativeSource Self}}"
设置控件的DataContext。
其次,你有第二个UserControl / Window /等等。让我们称之为MyControl。它也有一个DataContext,您可以设置以某种方式。然后绑定TextContent,如图所示。
因此,在MarkdownEditor中,{Binding TextContent}
指的是MarkdownEditor.xaml.cs中的DP。在MyControl中,{Binding TextContent}
指的是MyControl 的 DataContext上的属性。因此,您应该检查MyControl的DataContext中是否确实存在这样的属性。其次,您应该检查MarkdownEditor中的DataContext是否符合您的预期,或者是否被覆盖。
答案 1 :(得分:1)
<me:MarkdownEditor TextContent="{Binding Path=TextContent}" Options="{Binding Options}" />
(添加“Path =”)
答案 2 :(得分:1)
我会被殴打但是:
你试过这个:
<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}" />
(这次在模板中添加“Path =”) 我知道我听起来像是一个破纪录,但这条“Path =”不久前给了我一些headeache,我真的很想知道你的问题是否与此有关......
答案 3 :(得分:0)
尝试使用elementname绑定
例如。给窗口x:name = MyWindow并使用elementbinding
<TextBlock Text="{Binding Path=Problem,ElementName=MyWindow}" />
答案 4 :(得分:0)
我不知道MarkdownEditor
是什么或者是什么,但我认为MarkdownEditor
默认情况下不支持双向数据绑定。在标记中明确指定:
<me:MarkdownEditor TextContent="{Binding TextContent, Mode=TwoWay}" Options="{Binding Options}" />
某些XAML控件(例如TextBox
)默认使用双向绑定 - 换句话说,数据上下文中所做的更改会反映在控件中,而控件中所做的更改会反映在数据中上下文。其他XAML控件默认使用单向绑定。我不确定MarkdownEditor
默认情况下做了什么,但是,如果它默认使用单向绑定,我可以看到控件中所做的更改不会反映在数据上下文中。如果明确将绑定模式设置为TwoWay
可以帮助您处理您的情况,我会很感兴趣。