我试图将我的View分割成多个UserControl,然后根据某个值显示适当的View。
我在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>这在WPF中非常有用,CurrentView是ViewModel上的一个string属性,通过Button presses/etc进行更改。
由于UWP中没有DataTriggers,所以我已经读到您可以使用DataTriggerBehavior来完成相同的任务。所以我试过这个:
<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="{x:Bind ViewModel.CurrentView}" Value="Invoice">
<core:ChangePropertyAction PropertyName="ContentTemplate" Value="{StaticResource InvoiceView}" />
</core:DataTriggerBehavior>
<core:DataTriggerBehavior Binding="{x:Bind ViewModel.CurrentView}" Value="Payment">
<core:ChangePropertyAction PropertyName="ContentTemplate" Value="{StaticResource PaymentView}" />
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ContentControl>但出于某种原因,ChangePropertyAction并没有开火。我知道CurrentView正在改变,因为我在它上设置了一个断点。以防万一,这里是财产:
private string _currentView;
public string CurrentView
{
get { return _currentView; }
set { Set(ref _currentView, value); }
}我使用的是Template10,所以我的ViewModel通过ViewModelBase继承BindableBase,所以所有的OnPropertyChanged都应该正常工作(所有其他属性都正常工作)。
无论如何,我知道CurrentView正在改变,但ChangePropertyAction行为并没有触发。我遗漏了什么吗?
发布于 2016-04-19 15:11:02
我认为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>https://stackoverflow.com/questions/36706547
复制相似问题