定义了一个依赖属性(类型是列表),修改其中某项值后怎么被这个依赖属性感知到?

tonylll 2025-08-05 13:54:32

我有一个usercontrol,定义了一个依赖属性MyText,类型是ObservableCollection<string>,但是我修改里面某一项的内容后,没法触发MyText的SetValue,也就是MyText无法感知到列表里的某项已经改变了。请问怎么才能修改数值后自动触发呢?谢谢

 

1.uc.xaml:

<UserControl x:Class="Test.uc"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Test"
             mc:Ignorable="d" d:DesignHeight="100" d:DesignWidth="100">
    <Grid>
        <ContentControl x:Name="lbl"/>
    </Grid>
</UserControl>

2.uc.xaml.cs

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Test
{
    public partial class uc : UserControl
    {
        public static readonly DependencyProperty MyTextProperty =
                    DependencyProperty.Register("MyText", typeof(ObservableCollection<string>), typeof(uc), new PropertyMetadata(new ObservableCollection<string>()));

        [Bindable(true)]
        public ObservableCollection<string> MyText
        {
            get { return (ObservableCollection<string>)GetValue(MyTextProperty); }
            set {
                SetValue(MyTextProperty, value);
                ObservableCollection<string> lst = value as ObservableCollection<string>;
                string s = "";
                foreach (string item in lst) s += item;
                lbl.Content = s;
            }
        }

        public uc()
        {
            InitializeComponent();
            this.DataContext = this;
        }
    }
}

3.MainWindow.xaml

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test"
        mc:Ignorable="d" Height="100" Width="200">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <local:uc x:Name="uc" MyText="{Binding list}" Grid.Row="0"/>
        <Button x:Name="btn" Grid.Row="1" Click="btn_Click"/>
    </Grid>
</Window>

4.MainWindow.xaml.cs

using System.Collections.ObjectModel;
using System.Windows;

namespace Test
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<string> list { get; set; } = new ObservableCollection<string>();
        public MainWindow()
        {
            InitializeComponent();
            list.Add("a"); list.Add("b");
            uc.MyText = this.list;
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            list[1] = "c";
            uc.MyText = this.list;//必须要加这句强制赋值,界面的标签才会变成ac,注释掉这行后uc就无法感知到?
        }
    }
}

 

...全文
202 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文围绕“价格型需求响应”展开,重点研究基于Logistic函数的负荷转移率模型在需求响应中的应用,并提供了完整的Matlab代码实现。通过构建数学模型,精确量化电价变动对电力负荷转移行为的影响,旨在优化电力系统供需平衡,提升电网运行效率与经济性。研究系统阐述了模型的理论基础、关键参数设定、仿真流程设计及结果分析方法,适用于电力市场环境下用户侧响应行为的建模与优化,为需求响应策略的制定提供科学依据和技术支撑。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的科研人员、高校研究生及从事能源管理、智能电网、电力市场等相关工作的技术人员。; 使用场景及目标:①应用于电力需求侧管理研究,模拟和预测不同电价政策下用户的负荷响应特性;②为电力市场机制设计、峰谷分时电价制定及电网调度优化提供模型支持与仿真工具;③作为教学案例帮助学生深入理解需求响应建模原理、非线性函数拟合方法及数值仿真技术。; 阅读建议:建议读者结合Matlab代码逐段理解模型实现细节,重点关注Logistic函数在刻画负荷转移率非线性特征方面的优势,并可根据实际研究需求调整模型参数,进行扩展性实验与验证。

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧