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

标签 c# wpf xaml win-universal-app

我正在尝试拆分我的 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 中效果很好,并且 CurrentViewstring属性 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/

相关文章:

WPF - 数据绑定(bind)示例

c# - 当我尝试将元素添加到 Xamarin.Forms 应用程序中的 ObservableCollection 时出现 InvalidCastException

c# - WPF 数据绑定(bind)困惑

c# - 如何将 List<T> 转换为特定的 Json 格式

C# 如何使 UWP XAML 按钮在悬停时不显示边框

c# - 为什么没有触发DataGridView的CellEndEdit事件

c# - WPF C# 触摸滚动正在拖动整个窗口

c# - WPF 应用程序,字体图形错误

c# - ViewModel 属性的异常行为,未执行 RaisePropertyChanged

c# - 如何用半径和起止角画圆弧