2017-02-02 7 views
1

コンテンツコントロールがあり、そのコンテンツテンプレートはデータトリガによってトリガされますが、トリガは発生しません。私は間違って何をしていますか?コンテンツコントロールとデータテンプレートdatacontextは "MainViewModel"です。WPFサブプロパティのデータトリガが機能しません

コンテンツコントロールのリソース:

<DataTemplate x:Key="TopologyConfigurationInputTemplate"> 
<ContentControl> 
    <ContentControl.Resources> 
     <conv:ObjectToStringConverter x:Key="ObjectToStringConverter"/> 
    </ContentControl.Resources> 
    <ContentControl.Style> 
     <Style TargetType="{x:Type ContentControl}"> 
      <Setter Property="ContentTemplate" Value="{StaticResource CraneTemplate123}"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=TopologyConfigViewModel.TemplateSelection}" 
         Value="{x:Static vm:TopologyConfigViewModel+TemplateSelectionEnum.CRANE}"> 
        <Setter Property="ContentTemplate" Value="{StaticResource CraneTemplate123}"/> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding Path=TopologyConfigViewModel.TemplateSelection}" 
         Value="{x:Static vm:TopologyConfigViewModel+TemplateSelectionEnum.EQUIPMENT}"> 
        <Setter Property="ContentTemplate" Value="{StaticResource EquipmentTemplate123}"/> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding Path=TopologyConfigViewModel.TemplateSelection}" 
         Value="{x:Static vm:TopologyConfigViewModel+TemplateSelectionEnum.TERMINAL}"> 
        <Setter Property="ContentTemplate" Value="{StaticResource TerminalTemplate123}"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </ContentControl.Style> 
</ContentControl> 

コンテンツコントロール:これは私の "MainViewModel"

<ContentControl ContentTemplate="{StaticResource TopologyConfigurationInputTemplate}" MinHeight="250"/> 

です:

public class MainViewModel : BaseViewModel 
{ 
    private TopologyViewModel _topologyViewModel; 
    private TopologyConfigViewModel _topologyConfigViewModel; 
    protected Dictionary<string, List<string>> _validationErrors; 

    public MainViewModel() 
    { 
     _topologyViewModel = new TopologyViewModel(GetTopology()); 
     _topologyConfigViewModel = new TopologyConfigViewModel(_topologyViewModel); 
     _validationErrors = new Dictionary<string, List<string>>(); 
    } 

    public TopologyViewModel TopologyViewModel 
    { 
     get { return _topologyViewModel; } 
     set 
     { 
      if (_topologyViewModel != value) 
      { 
       _topologyViewModel = value; 
       NotifyPropertyChanged("TopologyViewModel"); 
      } 
     } 
    } 

    public TopologyConfigViewModel TopologyConfigViewModel 
    { 
     get { return _topologyConfigViewModel; } 
     set 
     { 
      if (_topologyConfigViewModel != value) 
      { 
       _topologyConfigViewModel = value; 
       NotifyPropertyChanged("TopologyConfigViewModel"); 
      } 
     } 
    } 
} 
ここで問題のコードです

これデータトリガのプロパティが実装されているネストされたViewModelに「TopologyConfigViewModel」は、次のとおりです。

public class TopologyConfigViewModel : BaseViewModel 
{ 
    public enum TemplateSelectionEnum { CRANE, EQUIPMENT, TERMINAL } 
    private TemplateSelectionEnum _templateSelection; 
    private TopologyViewModel _topologyViewModel; 
    protected Dictionary<string, List<string>> _validationErrors; 
    private ICommand _craneConfigSelectCmd; 
    private ICommand _equipmentConfigCmd; 
    private ICommand _terminalConfigCmd; 

    public TopologyConfigViewModel(TopologyViewModel pTopologyViewModel) 
    { 
     _topologyViewModel = pTopologyViewModel; 
     _validationErrors = new Dictionary<string, List<string>>(); 
     _craneConfigSelectCmd = new DelegateCommand(param => ChangeConfigTemplate(param)); 
     _equipmentConfigCmd = new DelegateCommand(param => ChangeConfigTemplate(param)); 
     _terminalConfigCmd = new DelegateCommand(param => ChangeConfigTemplate(param)); 
    } 

    public TemplateSelectionEnum TemplateSelection 
    { 
     get { return _templateSelection; } 
     set 
     { 
      if (_templateSelection != value) 
      { 
       _templateSelection = value; 
       NotifyPropertyChanged("TemplateSelection"); 
      } 
     } 
    } 

    public ICommand CraneConfigSelectCmd 
    { 
     get { return _craneConfigSelectCmd; } 
    } 

    public ICommand EquipmentConfigSelectCmd 
    { 
     get { return _equipmentConfigCmd; } 
    } 

    public ICommand TerminalConfigSelectCmd 
    { 
     get { return _terminalConfigCmd; } 
    } 

    public void ChangeConfigTemplate(object param) 
    { 
     try 
     { 
      if (param == null) 
       return; 
      if (((string)param) == "Crane") 
       TemplateSelection = TemplateSelectionEnum.CRANE; 
      else if (((string)param) == "Equipment") 
       TemplateSelection = TemplateSelectionEnum.EQUIPMENT; 
      else 
       TemplateSelection = TemplateSelectionEnum.TERMINAL; 
      //CraneConfigSelected = true; 

     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(ex.Message); 
     } 
    } 
} 

正しく動作している以下は:列挙型の値を変更するためのコマンド、プロパティは、事前に通知

感謝を変更しました!

+0

あなたはすべての3つのトリガーにTopologyConfigViewModel.TemplateSelection' 'と結合する必要がありますが、あなたが最初のものだけで行います。 – Grx70

+0

ああ、あなたは正しい、愚かな間違いだ。私はそれを変更しましたが、トリガーはまだ発砲しません。 –

答えて

1

それはあなたのビューモデルにContentControlにのContentプロパティをバインドする場合は動作するはずです:

<ContentControl ContentTemplate="{StaticResource TopologyConfigurationInputTemplate}" Content="{Binding}" MinHeight="250"/> 
+0

あなたは正しいです、それは今働きます。コンテンツコントロールのdatacontextが空でした。見つけにくい...ありがとう! –

関連する問題