我有一个内容控件,其内容模板将由数据触发器触发更改,但触发器未触发。我究竟做错了什么?内容控件'和数据模板'的datacontext是“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);
}
}
}
以下工作正常:更改枚举值的命令,属性更改通知
提前致谢!
答案 0 :(得分:1)
如果将ContentControl的Content属性绑定到视图模型,它应该可以工作:
getCountries: function()
{
options = {
headers:
{
'type' : 'GET',
'Authorization' : 'Basic c3VyZWJ1ZGR5LWFwaS11c2VyOkFwaTQzMjJTdXJlYg==',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Origin' : '*',
'dataType' : "json"
}
}
this.$http.get('http://surebuddy.azurewebsites.net/Api/Products', [options])
.then((response) => {
console.log(response.body);
}, (error) => {
console.log(error);
});
}