C# 在UWP中使用DataTriggerBehavior更改ContentTemplate
我试图将我的C# 在UWP中使用DataTriggerBehavior更改ContentTemplate,c#,wpf,xaml,win-universal-app,C#,Wpf,Xaml,Win Universal App,我试图将我的视图拆分为多个用户控件s,然后根据特定值显示相应的视图 我在WPF中实现了以下目标: <Window.Resources> <DataTemplate x:Key="CardView"> <views:CardView /> </DataTemplate> <DataTemplate x:Key="OtherView"> <views:OtherView />
视图
拆分为多个用户控件
s,然后根据特定值显示相应的视图
我在WPF中实现了以下目标:
<Window.Resources>
<DataTemplate x:Key="CardView">
<views:CardView />
</DataTemplate>
<DataTemplate x:Key="OtherView">
<views:OtherView />
</DataTemplate>
<DataTemplate x:Key="MainView">
<views:MainView />
</DataTemplate>
</Window.Resources>
...
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource MainView}" />
<Style.Triggers>
<DataTrigger Binding="{Binding CurrentView}" Value="Other">
<Setter Property="ContentTemplate" Value="{StaticResource OtherView}" />
</DataTrigger>
<DataTrigger Binding="{Binding CurrentView}" Value="Card">
<Setter Property="ContentTemplate" Value="{StaticResource CardView}" />
</DataTrigger>
<DataTrigger Binding="{Binding CurrentView}" Value="Main">
<Setter Property="ContentTemplate" Value="{StaticResource MainView}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
但是由于某种原因,changepropertyaAction
没有启动。我知道CurrentView
正在更改,因为我已经在它上面设置了一个断点。以防万一,这里是财产:
private string _currentView;
public string CurrentView
{
get { return _currentView; }
set { Set(ref _currentView, value); }
}
我使用的是Template10,因此我所有的ViewModel
都通过ViewModelBase
继承BindableBase
,因此所有OnPropertyChanged
内容也应该正常工作(所有其他属性都工作正常)
无论如何,我知道
CurrentView
正在更改,但是changepropertyaAction
行为没有触发。有什么我遗漏的吗?我假设ContentControl
将使用编译后的绑定{x:Bind ViewModel.CurrentView}
工作,因为设计器使用AutoComplete识别该绑定
一旦我将其更改为使用常规绑定{binding CurrentView}
,触发器就开始正常工作。以下是工作版本:
<Page.Resources>
<DataTemplate x:Key="PaymentView">
<local:PaymentView />
</DataTemplate>
<DataTemplate x:Key="InvoiceView">
<local:InvoiceView />
</DataTemplate>
</Page.Resources>
...
<ContentControl Content="{Binding}"
ContentTemplate="{StaticResource InvoiceView}"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.Below="PageHeader">
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior Binding="{Binding CurrentView}" Value="Invoice">
<core:ChangePropertyAction PropertyName="ContentTemplate" Value="{StaticResource InvoiceView}" />
</core:DataTriggerBehavior>
<core:DataTriggerBehavior Binding="{Binding CurrentView}" Value="Payment">
<core:ChangePropertyAction PropertyName="ContentTemplate" Value="{StaticResource PaymentView}" />
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ContentControl>
...
Viewmodel对象是否在ContentControl的DataContext下?我假设它继承了它。我最终将绑定
更改为{Binding CurrentView}
,而不是编译的绑定{x:Bind ViewModel.CurrentView},现在它可以工作了。我以为我已经试过了,但显然没有。谢谢。真希望我在花了一个小时左右的时间试图弄明白为什么它不起作用之前找到了这个。
<Page.Resources>
<DataTemplate x:Key="PaymentView">
<local:PaymentView />
</DataTemplate>
<DataTemplate x:Key="InvoiceView">
<local:InvoiceView />
</DataTemplate>
</Page.Resources>
...
<ContentControl Content="{Binding}"
ContentTemplate="{StaticResource InvoiceView}"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.Below="PageHeader">
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior Binding="{Binding CurrentView}" Value="Invoice">
<core:ChangePropertyAction PropertyName="ContentTemplate" Value="{StaticResource InvoiceView}" />
</core:DataTriggerBehavior>
<core:DataTriggerBehavior Binding="{Binding CurrentView}" Value="Payment">
<core:ChangePropertyAction PropertyName="ContentTemplate" Value="{StaticResource PaymentView}" />
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ContentControl>