Привязка WPF свойства строки и Кнопки. Содержание

Я хочу связать свойство строки с Кнопкой. Содержание.

Но почему это не работало?

Класс данных:

namespace test4
{
    public class Test : INotifyPropertyChanged
    {

         string   _Text = "Begin";

        public string Text
        {
            get{return _Text;}
            protected set { _Text = value; }
        }
        public void Start()
        {
            Text = "Begin";
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Text));
        }
        public void End()
        {
            Text = "End";
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Text));
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}

Логический код:

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        test4.Test ttt = new test4.Test();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource testViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("testViewSource")));
            testViewSource.Source = new object[]{ttt};
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (ttt.Text == "Begin")
                ttt.End();
            else
                ttt.Start();
        }
    }

XAML:

<Window x:Class="test5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:test4" Loaded="Window_Loaded">
    <Window.Resources>
        <CollectionViewSource x:Key="testViewSource" d:DesignSource="{d:DesignInstance my:Test, CreateList=true}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource testViewSource}">
        <Button Content="{Binding Path=Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="158,95,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>
5
задан Begtostudy 21 July 2010 в 11:24
поделиться