子属性上的 WPF Datatrigger 不起作用
WPF Datatrigger on subproperty not working
我有一个内容控件,它的内容模板应由数据触发器触发更改,但触发器未触发。我究竟做错了什么?内容控件和数据模板的数据上下文是 "MainViewModel"。这是有问题的代码:
内容控件的资源:
<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>
内容控制:
<ContentControl ContentTemplate="{StaticResource TopologyConfigurationInputTemplate}" MinHeight="250"/>
这是我的 "MainViewModel":
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);
}
}
}
以下工作正常:更改枚举值的命令,属性 更改通知
提前致谢!
如果您将 ContentControl 的内容 属性 绑定到视图模型,它应该可以工作:
<ContentControl ContentTemplate="{StaticResource TopologyConfigurationInputTemplate}" Content="{Binding}" MinHeight="250"/>
我有一个内容控件,它的内容模板应由数据触发器触发更改,但触发器未触发。我究竟做错了什么?内容控件和数据模板的数据上下文是 "MainViewModel"。这是有问题的代码:
内容控件的资源:
<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>
内容控制:
<ContentControl ContentTemplate="{StaticResource TopologyConfigurationInputTemplate}" MinHeight="250"/>
这是我的 "MainViewModel":
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);
}
}
}
以下工作正常:更改枚举值的命令,属性 更改通知
提前致谢!
如果您将 ContentControl 的内容 属性 绑定到视图模型,它应该可以工作:
<ContentControl ContentTemplate="{StaticResource TopologyConfigurationInputTemplate}" Content="{Binding}" MinHeight="250"/>