2017-04-17 17 views
1

コンテンツは、モジュールという名前のクラス(モデル)のプロパティにバインドされたテキストボックスを持つユーザーコントロール(mUserControlという名前)を持っています。ユーザーコントロールには、モジュールタイプの1つのカスタム依存プロパティ(ItemSource)があります。そこで、テキストボックスの内容をモジュールのプロパティにバインドすることができます。WPF ContextMenuにユーザーコントロールの依存プロパティが表示されない

このユーザーコントロールは、大きなビュー(HomeScreenView)の一部です。私はHomeScreenViewmodelからItemsSourceに簡単にアクセスできます。テキストボックスのContextMenu以外はすべて正常に動作します。私はバインディングエラーを取得します。 ContextMenuには、ユーザーコントロールのItemsSourceプロパティは表示されませんが、他のすべての要素はそれを行います。私は、ContextMenuは別のビジュアルツリー上にあることを知っています。私はこの仕事を成功させるためにさまざまな方法を試みました。どんな提案も歓迎です! (明確にするために簡略化)

ユーザーコントロールのXAML:

<UserControl x:Class="xxx.Views.ModuleFrameView" 
x:Name="mUserControl"> 
<Grid> 
<TextBox x:Name="txt5" Text="{Binding ItemSource.Ch1SET, 
ElementName=mUserControl}" IsEnabled="{Binding ItemSource.IsEnbl_5, 
ElementName=mUserControl}" IsReadOnly="True"       
TextAlignment="Center" ContextMenuService.ShowOnDisabled="True" 
Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"> 
<TextBox.ContextMenu > 
    <ContextMenu Name="cm"> 
      <MenuItem Header="Enable" cal:Message.Attach="cmEnable($source)" IsCheckable="True" IsChecked=" 
{Binding Path=PlacementTarget.Tag.DataContext.ItemSource.IsEnbl_5, 
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}}"/> 
    </ContextMenu> 
</TextBox.ContextMenu> 
<!--...--> 

ModuleFrameViewコードビハインドファイルに定義されたItemSource性を有する:

public Module ItemSource 
    { 
     get { return (Module)GetValue(ItemSourceProperty); } 
     set { SetValue(ItemSourceProperty, value); } 
    } 

    public static readonly DependencyProperty ItemSourceProperty = 
    DependencyProperty.Register("ItemSource", typeof(Module), typeof(ModuleFrameView), new PropertyMetadata(default(Module))); 

型モジュールのこのDP:

public class Module : PropertyChangedBase 
{ 
    private string _ch1SET; 
    public string Ch1SET 
    { 
     get { return _ch1SET; } 
     set 
     { 
      if (_ch1SET == value) return; 
      _ch1SET = value; 
      NotifyOfPropertyChange(() => Ch1SET); 
     } 
    } 
private bool _isEnbl_5; 
    public bool IsEnbl_5 
    { 
     get { return _isEnbl_5; } 
     set 
     { 
      if (_isEnbl_5 == value) return; 
      _isEnbl_5 = value; 
      NotifyOfPropertyChange(() => IsEnbl_5); 
     } 
    } 
//... 
//...lot of properties 

2番目のユーザーコントロール(上記の大きなView)のxaml:

012もしあれば
<UserControl x:Class="xxx.Views.HomeScreenView"> 
<Grid> 
    <ContentControl> 
<loc:ModuleFrameView Grid.Row="0" Grid.Column="0" ItemSource="{Binding ModuleArr[0]}"/> 
<loc:ModuleFrameView Grid.Row="0" Grid.Column="1" ItemSource="{Binding ModuleArr[1]}"/> 
<!--...--> 

答えて

0

ビットラテしかし、あなたのDataContextが正しく設定されている場合、あなたが名を必要としない、あなたは以下のように簡略化することができます(テストしていない)が、DPは+直接アクセスすることができ、「ItemSourceは、」間違ったプロパティ名です「ModuleFrameViewをして:ただ一つの要素を持っている=>「CurrentModule」次のものに

<UserControl x:Class="xxx.Views.ModuleFrameView"> 
    <Grid x:Name="ROOT"> 
     <TextBox Text="{Binding CurrentModule.Ch1SET}" IsEnabled="{Binding CurrentModule.IsEnbl_5}" IsReadOnly="True"       
TextAlignment="Center" ContextMenuService.ShowOnDisabled="True"> 
<TextBox.ContextMenu > 
    <ContextMenu> 
     <MenuItem Header="Enable" cal:Message.Attach="cmEnable($source)" IsCheckable="True" IsChecked="{Binding Path=CurrentModule.IsEnbl_5, ElementName="ROOT"}"/> 
    </ContextMenu> 
</TextBox.ContextMenu> 

に変更し、あなたのHomeScreenViewは、モジュールのコレクションにバインドさリストで交換する必要がある、とあなたのLOCに項目テンプレートを定義しますList ItemSource = "{Binding ModuleArr}"

0

PlacementTargetはプロパティです

<MenuItem Header="Enable" cal:Message.Attach="cmEnable($source)" IsCheckable="True" 
      IsChecked="{Binding Path=PlacementTarget.Tag.DataContext.ItemSource.IsEnbl_5, 
      RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/> 
:親 ContextMenuのあなたは AncestorTypeの代わり TextBoxとして ContextMenuを使用する必要があります
関連する問題