使用DependencyProperties

时间:2016-04-04 19:55:56

标签: c# wpf xaml data-binding dependency-properties

TextBlock显示默认属性元数据,但在DependencyProperty更改时不会更新。

看起来该视图没有更新,因为在分配新值后读取依赖项属性会返回新值。

这里是代码,提前谢谢:

Data.cs:

using System.Windows;

    namespace RMS.Kernel
    {
        public class Data : DependencyObject
        {
            private delegate void ObjectDelegate(object obj);

            public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(Data), new PropertyMetadata("start text"));

            private string message
            {
                get { return (string)GetValue(MessageProperty); }
                set { SetValue(MessageProperty, value); }
            }

            public Data() { }

            //singleton, make every method accesible anywhere. Only one gui object exists on runtime...
            private static Data shared;
            public static Data getShared()
            {
                if (shared == null)
                {
                    shared = new Data();
                }
                return shared;
            }

            public void setProperties(string message)
            {
                this.message = message;
            }
        }
    }

XAML:

<local:CustomWindow x:Class="RMS.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:RMS"
        xmlns:control="clr-namespace:RMS.Controls"
        xmlns:kernel="clr-namespace:RMS.Kernel"
        Title="MainWindow" Height="750" Width="1200">

    <Window.DataContext>
        <kernel:Data/>
    </Window.DataContext>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <control:CustomToolBar x:Name="customToolBar" VerticalAlignment="Top" Grid.Row="0"/>
        <TextBlock Text="{Binding Path=Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="White" FontSize="12pt" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1"/>
        <control:CustomStatusBar x:Name="CustomStatusBar" VerticalAlignment="Bottom" Grid.Row="2"/>
    </Grid>
</local:CustomWindow>

0 个答案:

没有答案