我想重用控件,但其中一个方案需要上下文菜单,而其他方案则不需要。这是我的尝试。绑定到UserControl中的依赖项属性XAML
public partial class RP8Grid : UserControl {
public bool UseContextMenu {
get { return (bool)GetValue(UseContextMenuProperty); }
set { SetValue(UseContextMenuProperty, value); }
}
// Using a DependencyProperty as the backing store for UseContextMenu. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UseContextMenuProperty =
DependencyProperty.Register("UseContextMenu", typeof(bool), typeof(RP8Grid), new PropertyMetadata(false));
public RP8Grid() {
InitializeComponent();
}
}
而在XAML使用属性:
<ctls:RP8Grid UseContextMenu="False"/>
现在我不能离开广场的一部分,我怎么访问UseContextMenu的用户控件里面? 我曾尝试以下:
<DataGrid>
<DataGrid.ContextMenu>
<ContextMenu IsEnabled="{Binding UseContextMenu,RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}}">
</DataGrid.ContextMenu>
</DataGrid>
与结果:
无法为参照 '的RelativeSource FindAncestor,AncestorType =' System.Windows.Controls.UserControl结合找到源”, AncestorLevel ='1'
你说得对,我觉得我打了普通话propdp的d。我的第一个实现。 RelativeSource,然后通过祖先链? – markokstate
'' –
无法找到,我的尝试更新了这个问题 – markokstate