1

在 WPF 4 UserControl 中,我可以在 XAML 中定义子控件的绑定,如下所示......

...
    <ComboBox 
        ItemsSource="{Binding CBItems, 
                      RelativeSource={RelativeSource AncestorType=UserControl}}"
        ...              
    />
...

其中 CBItems 是在 UserControl 后面的代码中定义的属性。

但是,此格式在 Windows 8 Metro 中不可用 - RelativeSource 没有成员 AncestorType。

如何在 Windows 8 中进行这种类型的绑定?

4

2 回答 2

3

看起来 TemplatedControl 而不是 UserControl 是为 Windows 8 Metro 开发创建可重用复合组件的方法。

Tim Heuer 的这篇博客文章有一个详细的例子。

于 2012-07-06T08:00:13.890 回答
0

如果我理解正确,您想将您的 ItemsSource 绑定到ComboBox您拥有的控件的公共属性UserControl吗?

你不能这样做:

<ComboBox ItemsSource="{Binding CBItems, ElementName=MyControl}"/>
...
<controls:MyUserControl x:name="MyControl" />
于 2012-07-05T22:52:10.070 回答