依赖属性对象没有更新

时间:2015-03-17 09:13:23

标签: c# wpf user-interface object dependency-properties

美好的一天。我有DependencyProperty更新的问题。为什么单击按钮后它没有更新。你能救我吗?

WpfApplication5.MainWindow

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfApplication5
{

public partial class MainWindow : Window
{

    TestData TD = new TestData();
    SomeStructure SS = new SomeStructure();

    public MainWindow()
    {
        SS.Name = "OIL";
        TD.Name = "GOLD";
        TD.CompanyName=SS;
        this.DataContext = TD;
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        SS.Name = "OIL2";
        TD.CompanyName=SS;
    }
}
public class ConvertValue : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}
public class SomeStructure
{
    public int Option { get; set; }
    public string Name { get; set; }
}
public class TestData : DependencyObject
{
    public string Name { get; set; }
    public string Assettyp { get; set; }

    public static readonly DependencyProperty CompanyNameProperty = DependencyProperty.Register("CompanyName", typeof(SomeStructure), typeof(TestData), new FrameworkPropertyMetadata(default(SomeStructure), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public SomeStructure CompanyName
    {
        get { return (SomeStructure)this.GetValue(CompanyNameProperty); }
        set { this.SetValue(CompanyNameProperty, value); }
    }
}
}

xaml code

<Window x:Class="WpfApplication5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication5"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:ConvertValue x:Key="SellBuy"></local:ConvertValue>
</Window.Resources>
<Grid>
    <Label x:Name="TestLabel" Width="120" Height="25" Content="{Binding CompanyName,Converter={StaticResource SellBuy}}"></Label>
    <Button Content="Button" Height="25" HorizontalAlignment="Left" Margin="340,143,0,0" Name="button1" VerticalAlignment="Top" Width="124" Click="button1_Click"/>
</Grid>

似乎用户界面并不知道内容已更新?

1 个答案:

答案 0 :(得分:1)

没有为TD.CompanyName分配新实例,因此不会发出通知。