我有一个自定义WPF控件绑定一个DependencyProperty MyString的WPF从FindAncestor到依赖项属性自定义控件的
我使用的是控制一个ItemsControl内对我的观想从出鱼的值ViewModel。
由于控件的DataContext成为ItemsControl的ItemsSource中的每个Item,我认为我只能使用FindAncestor,但dosnt似乎可以工作......任何人都可以看到我要出错的地方吗?
继承人的XAML的查看...
<Grid>
<ItemsControl ItemsSource="{Binding MyItems}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Name="myStack">
<ImportExceptions:ControlStrip MyString="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.MyStringOnViewModel}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
和继承人的背后我的自定义控制代码,我已经建立了我的依赖属性...
public partial class ControlStrip
{
public ControlStrip()
{
InitializeComponent();
}
public string MyString
{
get
{
return GetValue(MyStringProperty).ToString();
}
set
{
SetValue(MyStringProperty, value);
}
}
public static readonly DependencyProperty MyStringProperty =
DependencyProperty.RegisterAttached("MyString", typeof (string), typeof (ControlStrip));
}
我以为这是一个领带? ;) – kiwipom 2009-12-17 18:03:39
正如您怀疑我在DataContext参考中找到了错误。谢谢你的帮助 – 2009-12-21 16:12:44