UWP 等效于 uwp 中 FindAncestor 的函数 [英] UWP equivalent function to FindAncestor in uwp

查看:27
本文介绍了UWP 等效于 uwp 中 FindAncestor 的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个订单列表,当订单状态为已取消时,我想使文本闪烁.到目前为止,我的代码有效.但是,有时它会抛出异常:

I have a list of orders and when the order status is Cancelled, I want to blink the text. So far, my code works. However, sometimes it will throws exception:

WinRT 信息:无法解析 TargetName lblOrderStatus

WinRT information: Cannot resolve TargetName lblOrderStatus

出于某种原因,可以找到 lblOrderStatus.所以,我想使用FindAncestor",但 UWP 中不存在 FindAncestor.uwp 中是否有与 FindAncestor 等效的功能?

For some reason lblOrderStatus can be found. So, I want to use "FindAncestor", but FindAncestor doesn't exists in UWP. Is there any equivalent function to FindAncestor in uwp?

这是我的代码:

<ItemsControl x:Name="Orders" Grid.Row="1" Background="Transparent">
    ...
    ...
    ...
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                ...
                ...
                ...
                <Viewbox Grid.Column="3" StretchDirection="DownOnly" HorizontalAlignment="Right">
                    <TextBlock x:Name="lblOrderStatus" Text="{Binding Path=OrderItemStatus, Mode=OneWay}" FontSize="18">
                        <TextBlock.Resources>
                            <Storyboard x:Name="sbBlinking">
                                <DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Opacity)"
                                                 Storyboard.TargetName="lblOrderStatus"
                                                 From="1" To="0" AutoReverse="True" Duration="0:0:0.5" RepeatBehavior="Forever" />
                            </Storyboard>
                        </TextBlock.Resources>
                        <interactive:Interaction.Behaviors>
                            <core:DataTriggerBehavior Binding="{Binding OrderItemStatus, Converter={StaticResource EnumToStringConverter}}" ComparisonCondition="Equal" Value="Cancelled">
                                <media:ControlStoryboardAction Storyboard="{StaticResource sbBlinking}" />
                            </core:DataTriggerBehavior>
                        </interactive:Interaction.Behaviors>
                    </TextBlock>
                </Viewbox>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

推荐答案

考虑到我看到的所有解决方案,我觉得使用 ElementName 绑定是 UWP 没有 RelativeSource AncestorType 的最简单的解决方法绑定选项.

Considering all the solutions I've seen, I feel that using ElementName binding is the simplest workaround to UWP not having a RelativeSource AncestorType binding option.

假设您有一个 Page,其 DataContext 设置为带有命令 MyCommand 的视图模型,并且您希望在您的单击其按钮时执行它的列表:

Assuming you've got a Page with its DataContext set to a viewmodel with a command MyCommand, and you want each item in your list to execute it when its button is clicked:

<Page Name="thisPage">
    ...
    <ListView ...>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Button Command="{Binding ElementName=thisPage, Path=DataContext.MyCommand}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Page>

我对这个解决方案的最初问题是你不能将 DataTemplate 作为资源提取出来在多个屏幕(甚至对话框)上使用它;thisPage 可能不存在于每个地方,或者将根元素命名为thisPage"可能不合适.

My initial problem with this solution is that you can't extract the DataTemplate out as a resource to use it on multiple screens (or even dialog boxes); thisPage might not exist in each of those places, or it might not be appropriate to name the root element "thisPage".

但是,如果您使用约定在每个使用该 DataTemplate 的屏幕中包含一个令牌 UI 元素,并通过一致的名称引用它,它将起作用.默认情况下,此元素的 DataContext 将是您的 ViewModel(假设您的根元素也是如此)

But if you use a convention where you include a token UI element in every screen that uses that DataTemplate, and refer to it by a consistent name, it will work. By default this element's DataContext will be your ViewModel (assuming your root element does too)

<Rectangle Name="VmDcHelper" Visibility="Collapsed"/> 

...然后在您的独立资源 XAML 文件中,您可以像这样编写 DataTemplate:

...then in your standalone resources XAML file you can write your DataTemplate like this:

<DataTemplate x:Key="MyDataTemplate">
    <Button Command="{Binding ElementName=VmDcHelper, Path=DataContext.MyCommand}" />
</DataTemplate>

然后,在您使用该模板资源的每个页面/屏幕/对话框上,只需放入该矩形(或其他任何内容)的副本,所有内容都会在运行时正确绑定

Then, on every page/screen/dialog that you use that template resource, just drop in a copy of that Rectangle (or whatever) and everything will bind correctly at run-time

这显然是一个 hack 解决方案,但在仔细考虑之后,它并不比首先使用 WPF 的 AncestorType 更像是一个 hack(必须确保您的祖先类型始终保持一致)您使用数据模板的地方).

This is clearly a hack solution, but after thinking about it some more, it doesn't feel like any more of a hack than using WPF's AncestorType in the first place (having to ensure that your ancestor type is always consistent in all the places you use your DataTemplate).

这篇关于UWP 等效于 uwp 中 FindAncestor 的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆