c# - 如何在 2 个 ItemsSource 列表中公开 DependencyProperties(不传播)

标签 c# wpf xaml windows-store-apps win-universal-app

在另一个 StackPanel/ItemsSource 中获取 StackPanel/ItemsSource。内部的值有一个更新的值。无法让它将更新传播到 UI。

注意:作为注册的依赖属性 - 它永远不会更新。作为一个简单的属性 {get;set;} 它更新一次,然后就不再更新。它或它们应该是什么才能传播?

已经检查了许多网站、书籍等 - 他们的示例没有演示此用例或工作。

这里缺少什么(只是在传播中)?

注意! (如果不清楚)-这是一个配对的完全运行的示例,用于说明问题。应该清楚的是,这甚至与 prod 相差甚远,更不用说 int 或 dev 了。

(更新:似乎是 Windows 应用商店应用程序特定问题,因为它在 std WPF/等中工作)

示例依赖属性代码 (.cs):

using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace TestDependency
{
    public sealed partial class MainPage : Page
    {
        private TopData topData;

        public MainPage()
        {
            this.InitializeComponent();
            this.Do();
        }

        public async void Do() {

            topData = new TopData();
            this.TopItemsControl.ItemsSource = topData;

            var dispatcher = Dispatcher;

            var action = new Action(async () =>
            {
                while (true)
                {
                    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        topData[0][1].Value = topData[0][1].Value + 1;
                    });

                    await Task.Delay(1000);
                }
            });

            await Task.Run(action);      
        }
    }

    public class TopData : ObservableCollection<MiddleData>
    {
        public TopData()
        {
            this.Add(new MiddleData("ABC", new[] {"a1", "a2", "a3"}));
            this.Add(new MiddleData("DEF", new[] {"d1", "d2", "d3"}));
            this.Add(new MiddleData("GHI", new[] {"g1", "g2", "g3"}));
        }
    }

    public class MiddleData : ObservableCollection<BottomData>
    {
        public string Name { get; set; }

        public MiddleData(string name, string[] list)
        {
            this.Name = name;

            foreach (var item in list)
            {
                this.Add(new BottomData(item, 0));
            }    
        }
    }

    public class BottomData : DependencyObject
    {
        public string Name { get; set; }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(BottomData), new PropertyMetadata(0d));

        public double Value
        {
            get { return (double)this.GetValue(ValueProperty); }
            set { base.SetValue(ValueProperty, value); }
        }

        public BottomData(string name, double value)
        {
            this.Name = name;
            this.Value = value;
        }
    }
}

以及要匹配的示例 xaml 代码:

    <Page
    x:Class="TestDependency.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestDependency"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid x:Name="TopGrid">

            <Grid.RowDefinitions>
                <RowDefinition Height="5*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>

            <Grid.Resources>
                <DataTemplate x:Key="StackItemTemplate">
                    <StackPanel x:Name="BottomStackPanel"  Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}"/>
                        <TextBlock Text=": "/>
                        <TextBlock Text="{Binding Path=Value}" />
                    </StackPanel>
                </DataTemplate>

                <DataTemplate x:Key="SensorDataTemplate">
                    <StackPanel x:Name="TopStackPanel">
                        <Grid>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="20*"/>
                                <ColumnDefinition Width="10*"/>
                            </Grid.ColumnDefinitions>

                            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Path=Name}" Grid.Column="0"/>
                            <StackPanel x:Name="MiddleStackPanel"  Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center">

                                <ItemsControl x:Name="BottomItemsControl" ItemsSource="{Binding}"  ItemTemplate="{StaticResource StackItemTemplate}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <VirtualizingStackPanel Orientation="Vertical"/>
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                </ItemsControl>

                            </StackPanel>

                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </Grid.Resources>

            <ItemsControl x:Name="TopItemsControl" ItemTemplate="{StaticResource SensorDataTemplate}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>

        </Grid>
    </Grid>
</Page>

最佳答案

几个亮点:

首先,我对 Windows Phone 开发不太熟悉:我将您的代码导入到 WPF 应用程序中。将 xmlns:local 声明为 "using:TestDependency" 给了我第一拳;我必须将其替换为 "clr-namespace:TestDependency"

第二,你在哪里:

<ItemsControl x:Name="BottomItemsControl" ItemsSource="{Binding Path=This}" ...

我把它改为:

<ItemsControl x:Name="BottomItemsControl" ItemsSource="{Binding}"...

请注意从绑定(bind)声明中删除了 Path=This 位。这样,一切对我来说都工作得很好: View 使用来自 Do( 中的 async 任务中的 while 循环的增量值按顺序更新。 ) 方法。

请尝试一下。

关于c# - 如何在 2 个 ItemsSource 列表中公开 DependencyProperties(不传播),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28975362/

相关文章:

wpf - 如何将 ViewModel 的 ObservableCollection 绑定(bind)到 MenuItem?

c# - Metro 应用程序中的自定义光标

wpf - GridViewColumnHeaders 的键盘访问?

wpf - WPF 图标的 Segoe UI 符号或矢量图形?

c# - 来自其他对象的 wpf 命令参数

c# - WPF DataTrigger for TextBlock 控制Text

c# - 屏幕截图安全桌面

c# - 在 Windows 服务中更改计时器间隔

c# - UWP - PointerEntered/PointerExited 模拟鼠标悬停在网格行上

javascript - 如何将 Razor boolean 变量传递给 Angular 指令?