【发布时间】:2013-02-18 20:09:35
【问题描述】:
如何绑定到内容控件的内容属性?
我创建了自定义控件:
public class CustomControl
{
// Dependency Properties
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(MainViewModel), new PropertyMetadata(0));
}
在 ViewModel 中,我创建了此自定义控件类型的属性:
public CustomControl CustomControl { get; set; }
在视图中我将此属性绑定到内容控件:
<ContentControl x:Name="Custom" Content="{Binding CustomControl}"></ContentControl>
现在如何绑定到内容控件的内容属性?
【问题讨论】:
-
我不明白你的问题,你只是确实绑定到 ContentControl 的 Content 属性。请注意,如果您想更改 INotifyPropertyChanged 在您的 ViewModel 和您的 CustomControl 属性中实现它并在您的 UI 中查看更改,这可能会有所帮助。
-
@nvoigt 我想从视图模型中访问视图元素,这就是我尝试这样做的原因
-
您的 ViewModel 不应该知道任何关于视图的信息,包括任何 UI 元素。你能改写你的问题吗,因为你已经做了你要求的。您确实“绑定到 ContentControls 内容”。也许我只是误解了你的问题。