首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在UWP中使用DataTriggerBehavior改变DataTriggerBehavior

在UWP中使用DataTriggerBehavior改变DataTriggerBehavior
EN

Stack Overflow用户
提问于 2016-04-19 00:37:05
回答 1查看 2.2K关注 0票数 2

我试图将我的View分割成多个UserControl,然后根据某个值显示适当的View

我在WPF中实现了这样的目标:

代码语言:javascript
运行
复制
<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 presses/etc进行更改。

由于UWP中没有DataTriggers,所以我已经读到您可以使用DataTriggerBehavior来完成相同的任务。所以我试过这个:

代码语言:javascript
运行
复制
<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正在改变,因为我在它上设置了一个断点。以防万一,这里是财产:

代码语言:javascript
运行
复制
private string _currentView;
public string CurrentView
{
    get { return _currentView; }
    set { Set(ref _currentView, value); }
}

我使用的是Template10,所以我的ViewModel通过ViewModelBase继承BindableBase,所以所有的OnPropertyChanged都应该正常工作(所有其他属性都正常工作)。

无论如何,我知道CurrentView正在改变,但ChangePropertyAction行为并没有触发。我遗漏了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-19 15:11:02

我认为ContentControl使用编译后的绑定{x:Bind ViewModel.CurrentView}可以工作,因为设计人员识别了与AutoComplete的绑定。

一旦我将其更改为使用常规绑定{Binding CurrentView},触发器就开始正常工作。以下是工作版本:

代码语言:javascript
运行
复制
<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>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36706547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档