gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:44:41 24 4
gpt4 key购买 nike

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

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

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

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

注意! (如果不清楚)- 这是一个配对的完全运行的示例来说明问题。应该清楚这甚至不接近生产,更不用说 int 或 dev 了。

(更新:似乎是 Windows 应用商店应用的特定问题,因为它在标准 WPF/etc 中工作)

示例 DependencyProperty 代码 (.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;
}
}
}

和要匹配的 SAMPLE 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( ) 方法。

请试一试。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com