2017-07-13 156 views
0

我想重用控件,但其中一個方案需要上下文菜單,而其他方案則不需要。這是我的嘗試。綁定到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'

+1

你說得對,我覺得我打了普通話propdp的d。我的第一個實現。 RelativeSource,然後通過祖先鏈? – markokstate

+0

'' –

+1

無法找到,我的嘗試更新了這個問題 – markokstate

回答

2

A disabled ContextMenu打開,但其項目被禁用。那麼很難讓它結束。如果這就是你想要的,我懷疑,binding proxy會讓你在那裏(見下面的完整版本)。

如果你想要的是剛剛擺脫ContextMenu的時代,這將工作:

<DataGrid 
    > 
    <DataGrid.Style> 
     <Style TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}"> 
      <Style.Triggers> 
       <DataTrigger 
        Binding="{Binding UseContextMenu, RelativeSource={RelativeSource AncestorType=UserControl}}" 
        Value="True" 
        > 
        <Setter Property="ContextMenu"> 
         <Setter.Value> 
          <ContextMenu 
           > 
           <MenuItem Header="Test Item" /> 
           <MenuItem Header="Test Item" /> 
           <MenuItem Header="Test Item" /> 
           <MenuItem Header="Test Item" /> 
          </ContextMenu> 
         </Setter.Value> 
        </Setter> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.Style> 
</DataGrid> 

這裏的綁定代理版本:

public class BindingProxy : Freezable 
{ 
    #region Overrides of Freezable 

    protected override Freezable CreateInstanceCore() 
    { 
     return new BindingProxy(); 
    } 

    #endregion 

    public object Data 
    { 
     get { return (object)GetValue(DataProperty); } 
     set { SetValue(DataProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty DataProperty = 
     DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); 
} 

XAML:

<DataGrid 
    > 
    <DataGrid.Resources> 
     <local:BindingProxy 
      x:Key="UserControlBindingProxy" 
      Data="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" 
      /> 
    </DataGrid.Resources> 
    <DataGrid.ContextMenu> 
     <ContextMenu 
      IsEnabled="{Binding Data.UseContextMenu, Source={StaticResource UserControlBindingProxy}}" 
      > 
      <MenuItem Header="Test Item" /> 
      <MenuItem Header="Test Item" /> 
      <MenuItem Header="Test Item" /> 
      <MenuItem Header="Test Item" /> 
     </ContextMenu> 
    </DataGrid.ContextMenu> 
</DataGrid> 
+0

Yessir,觸發魔法!非常感謝你。 – markokstate