簡體   English   中英

在UWP中使用DataTriggerBehavior更改ContentTemplate

[英]Changing ContentTemplate using DataTriggerBehavior in UWP

我試圖將我的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中效果很好,並且CurrentViewViewModel上的一個string屬性,可以通過Button presss / etc進行更改。

由於UWP中沒有DataTrigger ,因此我讀到可以使用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行為並未觸發。 有什么我想念的嗎?

我已經假定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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM