gpt4 book ai didi

c# - 在 UWP 中使用 DataTriggerBehavior 更改 ContentTemplate

转载 作者:行者123 更新时间:2023-11-30 21:49:52 24 4
gpt4 key购买 nike

我正在尝试拆分我的 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 上的属性(property)通过 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继承BindableBase通过ViewModelBase , 所以所有的 OnPropertyChanged东西也应该正常工作(所有其他属性都工作正常)。

无论如何,我知道 CurrentView正在改变,但是 ChangePropertyAction行为不开火。有什么我想念的吗?

最佳答案

我曾假设 ContentControl 可以使用编译后的绑定(bind) {x:Bind ViewModel.CurrentView} 工作,因为设计者认识到该绑定(bind)与 AutoComplete。

一旦我将其更改为使用常规绑定(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com