我正在尝试拆分我的 View
分成多个UserControl
s,然后显示相应的 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
是 string
属性 ViewModel
通过 Button
更改压力机/等。
因为没有DataTrigger
在 UWP 中,我读到您可以使用 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
s继承BindableBase
通过ViewModelBase
,所以所有 OnPropertyChanged
东西也应该正常工作(所有其他属性都工作正常)。
无论如何,我知道CurrentView
正在改变,但是ChangePropertyAction
行为不是开火。我有什么遗漏的吗?
最佳答案
我假设 ContentControl
可以使用已编译的绑定(bind) {x:Bind ViewModel.CurrentView}
来工作,因为设计者通过 AutoComplete 识别出了该绑定(bind)。
一旦我将其更改为使用常规绑定(bind){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>
关于c# - 在 UWP 中使用 DataTriggerBehavior 更改 ContentTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36706547/