2016-12-27 1 views
0

명령 CanExecute* 메서드의 상태가 변경 될 때 컨텍스트 MenuItem을 업데이트하는 방법입니다.WPF MVVM 동적 MenuItem에 대한 가시성 바인딩

DataTemplates에 기초하여 visibility을 동적으로 생성하기 위해 MenuItems을 바인딩하는 것과 관련된 문제가 있습니다. ContextMenu은 일부 사용자 지정 매개 변수를 바인딩하는 GridControl 용으로 만들어집니다. Freezable 프록시를 통해 해당 매개 변수를 ContextMenu에 바인딩 할 수있었습니다. CanExecute*을 제외한 모든 항목이 정상적으로 작동하므로 MenuItem 가시성이 변경되지 않습니다. 괜찮아요보다 CanExecute* 경우, 일정 e.CanExecute = true 있습니다 (메뉴 항목이 활성화됩니다)하지만, CanExecute* 어떤 논리를 가지고 MenuItem보다 두 상태를 가질 수있을 때 항상 어떤 코드 거짓

-IsEnabled 세트를 가지고 :
ContextCommands는 ICommand의의 확장

IEnumerable<ICommand<SomeClass>> ContextCommands 

CustomMenuItem

//CustomMenuItem is just extension of MenuItem 
public class CustomMenuItem : MenuItem 

날엔 aTemplates

<DataTemplate DataType="{x:Type.... 
<controls:CustomMenuItem Command="{Binding Path=WrappedCommand}" Visibility="{Binding Path=IsVisible, Converter={StaticResource VisibilityConverter}}"> 
<commandparameters ... (parameters works OK, so i skip that)> 

그리드 선언

<controls:CustomGridControl Grid.Row="1" x:Name="MyGrid" 
            ColumnDescriptions="{Binding Source.ColumnDescriptions}" 
            QueryableSource="{Binding Source.Query}" 
            Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=Self}}" 
            > 
    <controls:CustomGridControl.Resources> 
     <helpers:BindingProxyHelper x:Key="DataProxy" Data="{Binding ElementName=MyGrid, Path=., Mode=TwoWay}" /> 
    </controls:CustomGridControl.Resources> 
    <controls:CustomGridControl.ContextMenu> 
     <contextmenu:CustomContextMenu 
      DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" 
      ItemsSource="{Binding ContextCommands}" 
      BindableProperties="{Binding Data, Source={StaticResource DataProxy}, Mode=TwoWay}"> 
     </contextmenu:CustomContextMenu> 
    </controls:CustomGridControl.ContextMenu> 
</controls:CustomGridControl> 

어떻게 동적의 MenuItem에 때 CanExecute 변경 가시성을 호출 할? DataContext를 전달하려고했지만 아무런 효과가 없습니다. UI가 변경되지 않습니다. 이 바인딩을 디버깅하면 Visibility가 올바르게 설정되었지만 효과가없는 것으로 나타납니다.

CAN EDIT FALSE 
System.Windows.Data Warning: 56 : Created BindingExpression (hash=2852357) for Binding (hash=35737921) 
System.Windows.Data Warning: 58 : Path: 'IsVisible' 
System.Windows.Data Warning: 60 : BindingExpression (hash=2852357): Default mode resolved to OneWay 
System.Windows.Data Warning: 61 : BindingExpression (hash=2852357): Default update trigger resolved to PropertyChanged 
System.Windows.Data Warning: 62 : BindingExpression (hash=2852357): Attach to Controls.CustomMenuItem.Visibility (hash=36410781) 
System.Windows.Data Warning: 67 : BindingExpression (hash=2852357): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=2852357): Found data context element: CustomMenuItem (hash=36410781) (OK) 
System.Windows.Data Warning: 78 : BindingExpression (hash=2852357): Activate with root item CommandWithParameterPlugin`1 (hash=19041329) 
System.Windows.Data Warning: 108 : BindingExpression (hash=2852357): At level 0 - for CommandWithParameterPlugin`1.IsVisible found accessor ReflectPropertyDescriptor(IsVisible) 
System.Windows.Data Warning: 104 : BindingExpression (hash=2852357): Replace item at level 0 with CommandWithParameterPlugin`1 (hash=19041329), using accessor ReflectPropertyDescriptor(IsVisible) 
System.Windows.Data Warning: 101 : BindingExpression (hash=2852357): GetValue at level 0 from CommandWithParameterPlugin`1 (hash=19041329) using ReflectPropertyDescriptor(IsVisible): 'True' 
System.Windows.Data Warning: 80 : BindingExpression (hash=2852357): TransferValue - got raw value 'True' 
System.Windows.Data Warning: 82 : BindingExpression (hash=2852357): TransferValue - user's converter produced 'Visible' 
System.Windows.Data Warning: 89 : BindingExpression (hash=2852357): TransferValue - using final value 'Visible' 
CAN EDIT TRUE 
CAN EDIT TRUE 

많은 답변을 찾았지만 내 문제는 여전히 행운입니다.

VisualContext 외부에있을 수 있으므로 ContextCommands를 CustomContextMenu에 바인딩하는 데 문제가 있다고 가정합니다. 솔루션 아마도 프록시에 대한 일종의 수도 있지만 그것을 구현하는 방법을 잘 모르겠습니다.

+0

RaiseCanExecuteChanged(); 당신이 찾고있는 것일 수 있습니까? –

+0

CanExecute를 디버깅 할 때 문제가 발생한다고 생각하지 않습니다. Raised가 표시되지만 UI에 아무런 영향이 없습니다. 내 마지막 단락을 참조하십시오. – cichy

+0

[BooleanToVisibilityConverter] (https://msdn.microsoft.com/ko) -us/library/system.windows.controls.booleantovisibilityconverter (v = vs.110) .aspx)? – mechanic

답변

0

이 명령을 통해이 menuItem 가시성을 액세스하고 명령 및 매개 변수를 바인딩 문제를 해결하려면 프로젝트 선택에서 여러 명령 유형이 도움이

<Style.Triggers> 
    <DataTrigger Binding="{Binding Converter={StaticResource TypeNameConverter}}" Value="CommandType`1"> 
     <Setter Property="CommandParameter" Value="{Binding SomeParametersFromParent, RelativeSource={RelativeSource AncestorType={x:Type local:CustomContextMenu}}}"/> 
     <Setter Property="Command" Value="{Binding Path=Command}"/> 
     <Setter Property="Header" Value="{Binding Path=HeaderInfo}"/> 
    </DataTrigger> 

희망 TypeNameConverter을 통해 만들어가 있기 때문에 MenuItem의 스타일 데이터 템플릿을 설정했다 당신도 비슷한 문제가있는 사람